@@ -149,63 +149,61 @@ public static object ParseQuotedPrimitive(string value)
149149 return Serializer . UnescapeString ( value ) ;
150150 }
151151
152- public static object ParsePrimitive ( string value )
153- {
154- if ( string . IsNullOrEmpty ( value ) ) return null ;
155-
156- bool boolValue ;
157- if ( bool . TryParse ( value , out boolValue ) ) return boolValue ;
158-
159- // Parse as decimal
160- decimal decimalValue ;
161- var acceptDecimal = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Decimal ) ;
162- var hasDecimal = decimal . TryParse ( value , NumberStyles . Number , CultureInfo . InvariantCulture , out decimalValue ) ;
163-
164- // Check if the number is an Primitive Integer type given that we have a decimal
165- if ( hasDecimal && decimalValue == decimal . Truncate ( decimalValue ) )
166- {
167- // Value is a whole number
168- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . Byte ) && decimalValue <= byte . MaxValue && decimalValue >= byte . MinValue ) return ( byte ) decimalValue ;
169- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . SByte ) && decimalValue <= sbyte . MaxValue && decimalValue >= sbyte . MinValue ) return ( sbyte ) decimalValue ;
170- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . Int16 ) && decimalValue <= Int16 . MaxValue && decimalValue >= Int16 . MinValue ) return ( Int16 ) decimalValue ;
171- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . UInt16 ) && decimalValue <= UInt16 . MaxValue && decimalValue >= UInt16 . MinValue ) return ( UInt16 ) decimalValue ;
172- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . Int32 ) && decimalValue <= Int32 . MaxValue && decimalValue >= Int32 . MinValue ) return ( Int32 ) decimalValue ;
173- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . UInt32 ) && decimalValue <= UInt32 . MaxValue && decimalValue >= UInt32 . MinValue ) return ( UInt32 ) decimalValue ;
174- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . Int64 ) && decimalValue <= Int64 . MaxValue && decimalValue >= Int64 . MinValue ) return ( Int64 ) decimalValue ;
175- if ( JsConfig . ParsePrimitiveIntegerTypes . HasFlag ( ParseAsType . UInt64 ) && decimalValue <= UInt64 . MaxValue && decimalValue >= UInt64 . MinValue ) return ( UInt64 ) decimalValue ;
176- return null ;
177- }
178-
179- // Value is a floating point number
180-
181- // Return a decimal if the user accepts a decimal
182- if ( hasDecimal && acceptDecimal )
183- return decimalValue ;
184-
185- // Parse as double if decimal failed or user wants a double
186- double doubleValue = 0 ;
187- var acceptDouble = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Double ) ;
188- var hasDouble = ( ! hasDecimal || acceptDouble ) && double . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out doubleValue ) ;
189-
190- // Return a double if the user accepts a double
191- if ( acceptDouble && hasDouble )
192- return doubleValue ;
193-
194- // Parse as float
195- float floatValue ;
196- var acceptFloat = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Single ) ;
197- var hasFloat = float . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out floatValue ) ;
198-
199- // Return a float if the user accepts a float
200- if ( acceptFloat && hasFloat )
201- return floatValue ;
202-
203- // Default to decimal, then double , then float or null
204- if ( hasDecimal ) return decimalValue ;
205- if ( hasDouble ) return doubleValue ;
206- if ( hasFloat ) return floatValue ;
207- return null ;
208- }
152+ public static object ParsePrimitive ( string value )
153+ {
154+ if ( string . IsNullOrEmpty ( value ) ) return null ;
155+
156+ bool boolValue ;
157+ if ( bool . TryParse ( value , out boolValue ) ) return boolValue ;
158+
159+ // Parse as decimal
160+ decimal decimalValue ;
161+ var acceptDecimal = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Decimal ) ;
162+ var isDecimal = decimal . TryParse ( value , NumberStyles . Number , CultureInfo . InvariantCulture , out decimalValue ) ;
163+
164+ // Check if the number is an Primitive Integer type given that we have a decimal
165+ if ( isDecimal && decimalValue == decimal . Truncate ( decimalValue ) )
166+ {
167+ // Value is a whole number
168+ var parseAs = JsConfig . ParsePrimitiveIntegerTypes ;
169+ if ( parseAs . HasFlag ( ParseAsType . Byte ) && decimalValue <= byte . MaxValue && decimalValue >= byte . MinValue ) return ( byte ) decimalValue ;
170+ if ( parseAs . HasFlag ( ParseAsType . SByte ) && decimalValue <= sbyte . MaxValue && decimalValue >= sbyte . MinValue ) return ( sbyte ) decimalValue ;
171+ if ( parseAs . HasFlag ( ParseAsType . Int16 ) && decimalValue <= Int16 . MaxValue && decimalValue >= Int16 . MinValue ) return ( Int16 ) decimalValue ;
172+ if ( parseAs . HasFlag ( ParseAsType . UInt16 ) && decimalValue <= UInt16 . MaxValue && decimalValue >= UInt16 . MinValue ) return ( UInt16 ) decimalValue ;
173+ if ( parseAs . HasFlag ( ParseAsType . Int32 ) && decimalValue <= Int32 . MaxValue && decimalValue >= Int32 . MinValue ) return ( Int32 ) decimalValue ;
174+ if ( parseAs . HasFlag ( ParseAsType . UInt32 ) && decimalValue <= UInt32 . MaxValue && decimalValue >= UInt32 . MinValue ) return ( UInt32 ) decimalValue ;
175+ if ( parseAs . HasFlag ( ParseAsType . Int64 ) && decimalValue <= Int64 . MaxValue && decimalValue >= Int64 . MinValue ) return ( Int64 ) decimalValue ;
176+ if ( parseAs . HasFlag ( ParseAsType . UInt64 ) && decimalValue <= UInt64 . MaxValue && decimalValue >= UInt64 . MinValue ) return ( UInt64 ) decimalValue ;
177+ return decimalValue ;
178+ }
179+
180+ // Value is a floating point number
181+
182+ // Return a decimal if the user accepts a decimal
183+ if ( isDecimal && acceptDecimal )
184+ return decimalValue ;
185+
186+ float floatValue ;
187+ var acceptFloat = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Single ) ;
188+ var isFloat = float . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out floatValue ) ;
189+ if ( acceptFloat && isFloat )
190+ return floatValue ;
191+
192+ double doubleValue ;
193+ var acceptDouble = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Double ) ;
194+ var isDouble = double . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out doubleValue ) ;
195+ if ( acceptDouble && isDouble )
196+ return doubleValue ;
197+
198+ if ( isDecimal )
199+ return decimalValue ;
200+ if ( isFloat )
201+ return floatValue ;
202+ if ( isDouble )
203+ return doubleValue ;
204+
205+ return null ;
206+ }
209207
210208 internal static object ParsePrimitive ( string value , char firstChar )
211209 {
0 commit comments