@@ -179,7 +179,8 @@ private static Dictionary<string, object> SetupDict()
179
179
{ "a" , "text" } ,
180
180
{ "b" , 32 } ,
181
181
{ "c" , false } ,
182
- { "d" , new [ ] { 1 , 2 , 3 } }
182
+ { "d" , new [ ] { 1 , 2 , 3 } } ,
183
+ { "e" , 1m } ,
183
184
} ;
184
185
}
185
186
@@ -244,6 +245,118 @@ public void Test_ServiceStack_Text_JsonSerializer_Array_Value_Deserializes_Corre
244
245
Assert . AreEqual ( new List < int > { 1 , 2 , 3 } , deserializedDict [ "d" ] ) ;
245
246
}
246
247
248
+
249
+ [ Test ]
250
+ public void deserizes_to_decimal_by_default ( )
251
+ {
252
+ JsConfig . TryToParsePrimitiveTypeValues = true ;
253
+
254
+ var dict = SetupDict ( ) ;
255
+ var json = JsonSerializer . SerializeToString ( dict ) ;
256
+ var deserializedDict = JsonSerializer . DeserializeFromString < IDictionary < string , object > > ( json ) ;
257
+ Assert . That ( deserializedDict [ "e" ] , Is . TypeOf < decimal > ( ) ) ;
258
+ Assert . That ( deserializedDict [ "e" ] , Is . EqualTo ( 1m ) ) ;
259
+
260
+ }
261
+ class NumericType
262
+ {
263
+
264
+ public NumericType ( decimal max , Type type )
265
+ : this ( 0 , max , type )
266
+ {
267
+
268
+ }
269
+ public NumericType ( decimal min , decimal max , Type type )
270
+ {
271
+ Min = min ;
272
+ Max = max ;
273
+ Type = type ;
274
+ }
275
+
276
+ public decimal Min { get ; private set ; }
277
+ public decimal Max { get ; private set ; }
278
+ public Type Type { get ; private set ; }
279
+ }
280
+
281
+ [ Test ]
282
+ public void deserizes_signed_bytes_into_to_best_fit_numeric ( )
283
+ {
284
+ JsConfig . TryToParsePrimitiveTypeValues = true ;
285
+ JsConfig . TryToParseNumericType = true ;
286
+
287
+ var deserializedDict = JsonSerializer . DeserializeFromString < IDictionary < string , object > > ( "{\" min\" :-128,\" max\" :127}" ) ;
288
+ Assert . That ( deserializedDict [ "min" ] , Is . TypeOf < sbyte > ( ) ) ;
289
+ Assert . That ( deserializedDict [ "min" ] , Is . EqualTo ( sbyte . MinValue ) ) ;
290
+ //it seemed strange having zero return as a signed byte
291
+ Assert . That ( deserializedDict [ "max" ] , Is . TypeOf < byte > ( ) ) ;
292
+ Assert . That ( deserializedDict [ "max" ] , Is . EqualTo ( sbyte . MaxValue ) ) ;
293
+ }
294
+
295
+ [ Test ]
296
+ public void deserizes_signed_types_into_to_best_fit_numeric ( )
297
+ {
298
+ var unsignedTypes = new [ ]
299
+ {
300
+ new NumericType ( Int16 . MinValue , Int16 . MaxValue , typeof ( Int16 ) ) ,
301
+ new NumericType ( Int32 . MinValue , Int32 . MaxValue , typeof ( Int32 ) ) ,
302
+ new NumericType ( Int64 . MinValue , Int64 . MaxValue , typeof ( Int64 ) ) ,
303
+ } ;
304
+
305
+ JsConfig . TryToParsePrimitiveTypeValues = true ;
306
+ JsConfig . TryToParseNumericType = true ;
307
+
308
+
309
+ foreach ( var signedType in unsignedTypes )
310
+ {
311
+ var dict = new Dictionary < string , object >
312
+ {
313
+ { "min" , signedType . Min } ,
314
+ { "max" , signedType . Max } ,
315
+ } ;
316
+
317
+ var json = JsonSerializer . SerializeToString ( dict ) ;
318
+ var deserializedDict = JsonSerializer . DeserializeFromString < IDictionary < string , object > > ( json ) ;
319
+ Assert . That ( deserializedDict [ "min" ] , Is . TypeOf ( signedType . Type ) ) ;
320
+ Assert . That ( deserializedDict [ "min" ] , Is . EqualTo ( signedType . Min ) ) ;
321
+ Assert . That ( deserializedDict [ "max" ] , Is . TypeOf ( signedType . Type ) ) ;
322
+ Assert . That ( deserializedDict [ "max" ] , Is . EqualTo ( signedType . Max ) ) ;
323
+
324
+ }
325
+ }
326
+
327
+ [ Test ]
328
+ public void deserizes_unsigned_types_into_to_best_fit_numeric ( )
329
+ {
330
+ var unsignedTypes = new [ ]
331
+ {
332
+ new NumericType ( byte . MinValue , byte . MaxValue , typeof ( byte ) ) ,
333
+ new NumericType ( UInt16 . MaxValue , typeof ( UInt16 ) ) ,
334
+ new NumericType ( UInt32 . MaxValue , typeof ( UInt32 ) ) ,
335
+ new NumericType ( UInt64 . MaxValue , typeof ( UInt64 ) ) ,
336
+ } ;
337
+
338
+ JsConfig . TryToParsePrimitiveTypeValues = true ;
339
+ JsConfig . TryToParseNumericType = true ;
340
+
341
+
342
+ foreach ( var unsignedType in unsignedTypes )
343
+ {
344
+ var dict = new Dictionary < string , object >
345
+ {
346
+ { "min" , unsignedType . Min } ,
347
+ { "max" , unsignedType . Max } ,
348
+ } ;
349
+
350
+ var json = JsonSerializer . SerializeToString ( dict ) ;
351
+ var deserializedDict = JsonSerializer . DeserializeFromString < IDictionary < string , object > > ( json ) ;
352
+ Assert . That ( deserializedDict [ "min" ] , Is . EqualTo ( 0 ) ) ;
353
+ Assert . That ( deserializedDict [ "min" ] , Is . TypeOf < byte > ( ) ) ;
354
+ Assert . That ( deserializedDict [ "max" ] , Is . TypeOf ( unsignedType . Type ) ) ;
355
+ Assert . That ( deserializedDict [ "max" ] , Is . EqualTo ( unsignedType . Max ) ) ;
356
+
357
+ }
358
+ }
359
+
247
360
[ Test ]
248
361
public void Can_deserialize_mixed_dictionary_into_strongtyped_map ( )
249
362
{
0 commit comments