@@ -192,50 +192,7 @@ public static object ParsePrimitive(StringSegment value)
192
192
if ( value . TryParseBoolean ( out bool boolValue ) )
193
193
return boolValue ;
194
194
195
- // Parse as decimal
196
- var acceptDecimal = JsConfig . ParsePrimitiveFloatingPointTypes . Has ( ParseAsType . Decimal ) ;
197
- var isDecimal = value . TryParseDecimal ( out decimal decimalValue ) ;
198
-
199
- // Check if the number is an Primitive Integer type given that we have a decimal
200
- if ( isDecimal && decimalValue == decimal . Truncate ( decimalValue ) )
201
- {
202
- // Value is a whole number
203
- var parseAs = JsConfig . ParsePrimitiveIntegerTypes ;
204
- if ( parseAs . Has ( ParseAsType . Byte ) && decimalValue <= byte . MaxValue && decimalValue >= byte . MinValue ) return ( byte ) decimalValue ;
205
- if ( parseAs . Has ( ParseAsType . SByte ) && decimalValue <= sbyte . MaxValue && decimalValue >= sbyte . MinValue ) return ( sbyte ) decimalValue ;
206
- if ( parseAs . Has ( ParseAsType . Int16 ) && decimalValue <= Int16 . MaxValue && decimalValue >= Int16 . MinValue ) return ( Int16 ) decimalValue ;
207
- if ( parseAs . Has ( ParseAsType . UInt16 ) && decimalValue <= UInt16 . MaxValue && decimalValue >= UInt16 . MinValue ) return ( UInt16 ) decimalValue ;
208
- if ( parseAs . Has ( ParseAsType . Int32 ) && decimalValue <= Int32 . MaxValue && decimalValue >= Int32 . MinValue ) return ( Int32 ) decimalValue ;
209
- if ( parseAs . Has ( ParseAsType . UInt32 ) && decimalValue <= UInt32 . MaxValue && decimalValue >= UInt32 . MinValue ) return ( UInt32 ) decimalValue ;
210
- if ( parseAs . Has ( ParseAsType . Int64 ) && decimalValue <= Int64 . MaxValue && decimalValue >= Int64 . MinValue ) return ( Int64 ) decimalValue ;
211
- if ( parseAs . Has ( ParseAsType . UInt64 ) && decimalValue <= UInt64 . MaxValue && decimalValue >= UInt64 . MinValue ) return ( UInt64 ) decimalValue ;
212
- return decimalValue ;
213
- }
214
-
215
- // Value is a floating point number
216
-
217
- // Return a decimal if the user accepts a decimal
218
- if ( isDecimal && acceptDecimal )
219
- return decimalValue ;
220
-
221
- var acceptFloat = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Single ) ;
222
- var isFloat = value . TryParseFloat ( out float floatValue ) ;
223
- if ( acceptFloat && isFloat )
224
- return floatValue ;
225
-
226
- var acceptDouble = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Double ) ;
227
- var isDouble = value . TryParseDouble ( out double doubleValue ) ;
228
- if ( acceptDouble && isDouble )
229
- return doubleValue ;
230
-
231
- if ( isDecimal )
232
- return decimalValue ;
233
- if ( isFloat )
234
- return floatValue ;
235
- if ( isDouble )
236
- return doubleValue ;
237
-
238
- return null ;
195
+ return value . ParseNumber ( ) ;
239
196
}
240
197
241
198
internal static object ParsePrimitive ( string value , char firstChar )
@@ -375,11 +332,79 @@ private static SetMemberDelegate GetSetFieldMethod(TypeConfig typeConfig, FieldI
375
332
}
376
333
}
377
334
378
- internal static class DeserializeTypeExensions
335
+ public static class DeserializeTypeExensions
379
336
{
380
337
public static bool Has ( this ParseAsType flags , ParseAsType flag )
381
338
{
382
339
return ( flag & flags ) != 0 ;
383
340
}
341
+
342
+ public static object ParseNumber ( this StringSegment value )
343
+ {
344
+ if ( value . Length == 1 )
345
+ {
346
+ int singleDigit = value . GetChar ( 0 ) ;
347
+ if ( singleDigit >= 48 || singleDigit <= 57 ) // 0 - 9
348
+ {
349
+ var result = singleDigit - 48 ;
350
+ if ( JsConfig . TryParseIntoBestFit )
351
+ return ( byte ) result ;
352
+ return result ;
353
+ }
354
+ }
355
+
356
+ // Parse as decimal
357
+ var acceptDecimal = JsConfig . ParsePrimitiveFloatingPointTypes . Has ( ParseAsType . Decimal ) ;
358
+ var isDecimal = value . TryParseDecimal ( out decimal decimalValue ) ;
359
+
360
+ // Check if the number is an Primitive Integer type given that we have a decimal
361
+ if ( isDecimal && decimalValue == decimal . Truncate ( decimalValue ) )
362
+ {
363
+ // Value is a whole number
364
+ var parseAs = JsConfig . ParsePrimitiveIntegerTypes ;
365
+ if ( parseAs . Has ( ParseAsType . Byte ) && decimalValue <= byte . MaxValue && decimalValue >= byte . MinValue )
366
+ return ( byte ) decimalValue ;
367
+ if ( parseAs . Has ( ParseAsType . SByte ) && decimalValue <= sbyte . MaxValue && decimalValue >= sbyte . MinValue )
368
+ return ( sbyte ) decimalValue ;
369
+ if ( parseAs . Has ( ParseAsType . Int16 ) && decimalValue <= Int16 . MaxValue && decimalValue >= Int16 . MinValue )
370
+ return ( Int16 ) decimalValue ;
371
+ if ( parseAs . Has ( ParseAsType . UInt16 ) && decimalValue <= UInt16 . MaxValue && decimalValue >= UInt16 . MinValue )
372
+ return ( UInt16 ) decimalValue ;
373
+ if ( parseAs . Has ( ParseAsType . Int32 ) && decimalValue <= Int32 . MaxValue && decimalValue >= Int32 . MinValue )
374
+ return ( Int32 ) decimalValue ;
375
+ if ( parseAs . Has ( ParseAsType . UInt32 ) && decimalValue <= UInt32 . MaxValue && decimalValue >= UInt32 . MinValue )
376
+ return ( UInt32 ) decimalValue ;
377
+ if ( parseAs . Has ( ParseAsType . Int64 ) && decimalValue <= Int64 . MaxValue && decimalValue >= Int64 . MinValue )
378
+ return ( Int64 ) decimalValue ;
379
+ if ( parseAs . Has ( ParseAsType . UInt64 ) && decimalValue <= UInt64 . MaxValue && decimalValue >= UInt64 . MinValue )
380
+ return ( UInt64 ) decimalValue ;
381
+ return decimalValue ;
382
+ }
383
+
384
+ // Value is a floating point number
385
+
386
+ // Return a decimal if the user accepts a decimal
387
+ if ( isDecimal && acceptDecimal )
388
+ return decimalValue ;
389
+
390
+ var acceptFloat = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Single ) ;
391
+ var isFloat = value . TryParseFloat ( out float floatValue ) ;
392
+ if ( acceptFloat && isFloat )
393
+ return floatValue ;
394
+
395
+ var acceptDouble = JsConfig . ParsePrimitiveFloatingPointTypes . HasFlag ( ParseAsType . Double ) ;
396
+ var isDouble = value . TryParseDouble ( out double doubleValue ) ;
397
+ if ( acceptDouble && isDouble )
398
+ return doubleValue ;
399
+
400
+ if ( isDecimal )
401
+ return decimalValue ;
402
+ if ( isFloat )
403
+ return floatValue ;
404
+ if ( isDouble )
405
+ return doubleValue ;
406
+
407
+ return null ;
408
+ }
384
409
}
385
410
}
0 commit comments