22
22
using System ;
23
23
using Newtonsoft . Json ;
24
24
using NUnit . Framework ;
25
+ using System . Collections . Generic ;
25
26
26
27
namespace UnitsNet . Serialization . JsonNet . Tests
27
28
{
@@ -226,12 +227,162 @@ public void UnitEnumChangedAfterSerialization_ExpectUnitCorrectlyDeserialized()
226
227
// still deserializable, and the correct value of 1000 g is obtained.
227
228
Assert . That ( deserializedMass . Grams , Is . EqualTo ( 1000 ) ) ;
228
229
}
230
+
231
+ [ Test ]
232
+ public void UnitInIComparable_ExpectUnitCorrectlyDeserialized ( )
233
+ {
234
+ TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable ( )
235
+ {
236
+ Value = Power . FromWatts ( 10 )
237
+ } ;
238
+ JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings ( ) ;
239
+
240
+ string json = JsonConvert . SerializeObject ( testObjWithIComparable , jsonSerializerSettings ) ;
241
+
242
+ var deserializedTestObject = JsonConvert . DeserializeObject < TestObjWithIComparable > ( json , jsonSerializerSettings ) ;
243
+
244
+ Assert . That ( deserializedTestObject . Value . GetType ( ) , Is . EqualTo ( typeof ( Power ) ) ) ;
245
+ Assert . That ( ( Power ) deserializedTestObject . Value , Is . EqualTo ( Power . FromWatts ( 10 ) ) ) ;
246
+ }
247
+
248
+ [ Test ]
249
+ public void DoubleInIComparable_ExpectUnitCorrectlyDeserialized ( )
250
+ {
251
+ TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable ( )
252
+ {
253
+ Value = 10.0
254
+ } ;
255
+ JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings ( ) ;
256
+
257
+ string json = JsonConvert . SerializeObject ( testObjWithIComparable , jsonSerializerSettings ) ;
258
+
259
+ var deserializedTestObject = JsonConvert . DeserializeObject < TestObjWithIComparable > ( json , jsonSerializerSettings ) ;
260
+
261
+ Assert . That ( deserializedTestObject . Value . GetType ( ) , Is . EqualTo ( typeof ( double ) ) ) ;
262
+ Assert . That ( ( double ) deserializedTestObject . Value , Is . EqualTo ( 10.0 ) ) ;
263
+ }
264
+
265
+ [ Test ]
266
+ public void ClassInIComparable_ExpectUnitCorrectlyDeserialized ( )
267
+ {
268
+ TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable ( )
269
+ {
270
+ Value = new ComparableClass ( ) { Value = 10 }
271
+ } ;
272
+ JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings ( ) ;
273
+
274
+ string json = JsonConvert . SerializeObject ( testObjWithIComparable , jsonSerializerSettings ) ;
275
+ var deserializedTestObject = JsonConvert . DeserializeObject < TestObjWithIComparable > ( json , jsonSerializerSettings ) ;
276
+
277
+ Assert . That ( deserializedTestObject . Value . GetType ( ) , Is . EqualTo ( typeof ( ComparableClass ) ) ) ;
278
+ Assert . That ( ( ( ComparableClass ) ( deserializedTestObject . Value ) ) . Value , Is . EqualTo ( 10.0 ) ) ;
279
+ }
280
+
281
+ [ Test ]
282
+ public void OtherObjectWithUnitAndValue_ExpectCorrectResturnValues ( )
283
+ {
284
+ TestObjWithValueAndUnit testObjWithValueAndUnit = new TestObjWithValueAndUnit ( )
285
+ {
286
+ Value = 5 ,
287
+ Unit = "Test" ,
288
+ } ;
289
+ JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings ( ) ;
290
+
291
+ string json = JsonConvert . SerializeObject ( testObjWithValueAndUnit , jsonSerializerSettings ) ;
292
+ TestObjWithValueAndUnit deserializedTestObject = JsonConvert . DeserializeObject < TestObjWithValueAndUnit > ( json , jsonSerializerSettings ) ;
293
+
294
+ Assert . That ( deserializedTestObject . Value . GetType ( ) , Is . EqualTo ( typeof ( double ) ) ) ;
295
+ Assert . That ( deserializedTestObject . Value , Is . EqualTo ( 5.0 ) ) ;
296
+ Assert . That ( deserializedTestObject . Unit , Is . EqualTo ( "Test" ) ) ;
297
+ }
298
+
299
+ [ Test ]
300
+ public void ThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized ( )
301
+ {
302
+ TestObjWithThreeIComparable testObjWithIComparable = new TestObjWithThreeIComparable ( )
303
+ {
304
+ Value1 = 10.0 ,
305
+ Value2 = Power . FromWatts ( 19 ) ,
306
+ Value3 = new ComparableClass ( ) { Value = 10 } ,
307
+ } ;
308
+ JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings ( ) ;
309
+
310
+ string json = JsonConvert . SerializeObject ( testObjWithIComparable , jsonSerializerSettings ) ;
311
+ var deserializedTestObject = JsonConvert . DeserializeObject < TestObjWithThreeIComparable > ( json , jsonSerializerSettings ) ;
312
+
313
+ Assert . That ( deserializedTestObject . Value1 . GetType ( ) , Is . EqualTo ( typeof ( double ) ) ) ;
314
+ Assert . That ( ( deserializedTestObject . Value1 ) , Is . EqualTo ( 10.0 ) ) ;
315
+ Assert . That ( deserializedTestObject . Value2 . GetType ( ) , Is . EqualTo ( typeof ( Power ) ) ) ;
316
+ Assert . That ( ( deserializedTestObject . Value2 ) , Is . EqualTo ( Power . FromWatts ( 19 ) ) ) ;
317
+ Assert . That ( deserializedTestObject . Value3 . GetType ( ) , Is . EqualTo ( typeof ( ComparableClass ) ) ) ;
318
+ Assert . That ( ( deserializedTestObject . Value3 ) , Is . EqualTo ( testObjWithIComparable . Value3 ) ) ;
319
+ }
320
+
321
+ private static JsonSerializerSettings CreateJsonSerializerSettings ( )
322
+ {
323
+ var jsonSerializerSettings = new JsonSerializerSettings ( )
324
+ {
325
+ Formatting = Formatting . Indented ,
326
+ TypeNameHandling = TypeNameHandling . Auto
327
+ } ;
328
+ jsonSerializerSettings . Converters . Add ( new UnitsNetJsonConverter ( ) ) ;
329
+ return jsonSerializerSettings ;
330
+ }
229
331
}
230
332
231
- internal class TestObj
333
+ private class TestObj
232
334
{
233
335
public Frequency ? NullableFrequency { get ; set ; }
234
336
public Frequency NonNullableFrequency { get ; set ; }
235
337
}
338
+
339
+ private class TestObjWithValueAndUnit : IComparable
340
+ {
341
+ public double Value { get ; set ; }
342
+ public string Unit { get ; set ; }
343
+
344
+ public int CompareTo ( object obj )
345
+ {
346
+ return Value . CompareTo ( obj ) ;
347
+ }
348
+ }
349
+
350
+ private class ComparableClass : IComparable
351
+ {
352
+ public int Value { get ; set ; }
353
+ public int CompareTo ( object obj )
354
+ {
355
+ return Value . CompareTo ( obj ) ;
356
+ }
357
+
358
+ // Needed for virfying that the deserialized object is the same, should not affect the serilization code
359
+ public override bool Equals ( object obj )
360
+ {
361
+ if ( obj == null || GetType ( ) != obj . GetType ( ) )
362
+ {
363
+ return false ;
364
+ }
365
+ return Value . Equals ( ( ( ComparableClass ) obj ) . Value ) ;
366
+ }
367
+
368
+ public override int GetHashCode ( )
369
+ {
370
+ return Value . GetHashCode ( ) ;
371
+ }
372
+ }
373
+
374
+ private class TestObjWithIComparable
375
+ {
376
+ public IComparable Value { get ; set ; }
377
+ }
378
+
379
+ private class TestObjWithThreeIComparable
380
+ {
381
+ public IComparable Value1 { get ; set ; }
382
+
383
+ public IComparable Value2 { get ; set ; }
384
+
385
+ public IComparable Value3 { get ; set ; }
386
+ }
236
387
}
237
388
}
0 commit comments