@@ -149,50 +149,63 @@ 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- float floatValue ;
159- double doubleValue ;
160-
161- decimal decimalValue ;
162- if ( decimal . TryParse ( value , NumberStyles . Number , CultureInfo . InvariantCulture , out decimalValue ) )
163- {
164- if ( ! JsConfig . TryToParseNumericType )
165- return decimalValue ;
166-
167- if ( decimalValue == decimal . Truncate ( decimalValue ) )
168- {
169- if ( decimalValue <= byte . MaxValue && decimalValue >= byte . MinValue ) return ( byte ) decimalValue ;
170- if ( decimalValue <= sbyte . MaxValue && decimalValue >= sbyte . MinValue ) return ( sbyte ) decimalValue ;
171- if ( decimalValue <= Int16 . MaxValue && decimalValue >= Int16 . MinValue ) return ( Int16 ) decimalValue ;
172- if ( decimalValue <= UInt16 . MaxValue && decimalValue >= UInt16 . MinValue ) return ( UInt16 ) decimalValue ;
173- if ( decimalValue <= Int32 . MaxValue && decimalValue >= Int32 . MinValue ) return ( Int32 ) decimalValue ;
174- if ( decimalValue <= UInt32 . MaxValue && decimalValue >= UInt32 . MinValue ) return ( UInt32 ) decimalValue ;
175- if ( decimalValue <= Int64 . MaxValue && decimalValue >= Int64 . MinValue ) return ( Int64 ) decimalValue ;
176- if ( decimalValue <= UInt64 . MaxValue && decimalValue >= UInt64 . MinValue ) return ( UInt64 ) decimalValue ;
177- }
178-
179- if ( float . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out floatValue ) )
180- return floatValue ;
181-
182- if ( double . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out doubleValue ) )
183- return doubleValue ;
184-
185- return decimalValue ;
186- }
187-
188- if ( float . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out floatValue ) )
189- return floatValue ;
190-
191- if ( double . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out doubleValue ) )
192- return doubleValue ;
193-
194- return null ;
195- }
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+ }
196209
197210 internal static object ParsePrimitive ( string value , char firstChar )
198211 {
0 commit comments