5
5
// -----------------------------------------------------------------------
6
6
7
7
using System ;
8
+ using System . Collections . Generic ;
8
9
using System . Configuration ;
9
10
using System . Linq ;
11
+ using System . Reflection ;
10
12
using FluentAssertions ;
13
+ using Newtonsoft . Json ;
14
+ using Newtonsoft . Json . Serialization ;
11
15
using Xunit ;
12
16
13
17
namespace Hocon . Configuration . Tests
@@ -427,5 +431,49 @@ public void ShouldSerializeFallbackValues()
427
431
c . ToString ( ) . Should ( ) . NotContain ( "other-key" , "Fallback values are ignored by default" ) ;
428
432
c . ToString ( true ) . Should ( ) . Contain ( "other-key" , "Fallback values should be displayed when requested" ) ;
429
433
}
434
+
435
+ /// <summary>
436
+ /// Source issue: https://github.com/akkadotnet/HOCON/issues/175
437
+ /// </summary>
438
+ [ Fact ]
439
+ public void ShouldDeserializeFromJson ( )
440
+ {
441
+ var settings = new JsonSerializerSettings
442
+ {
443
+ PreserveReferencesHandling = PreserveReferencesHandling . Objects ,
444
+ Converters = new List < JsonConverter > ( ) ,
445
+ NullValueHandling = NullValueHandling . Ignore ,
446
+ DefaultValueHandling = DefaultValueHandling . Ignore ,
447
+ MissingMemberHandling = MissingMemberHandling . Ignore ,
448
+ ObjectCreationHandling = ObjectCreationHandling . Replace ,
449
+ ConstructorHandling = ConstructorHandling . AllowNonPublicDefaultConstructor ,
450
+ TypeNameHandling = TypeNameHandling . All ,
451
+ ContractResolver = new AkkaContractResolver ( )
452
+ } ;
453
+
454
+ var json = "{\" $id\" :\" 1\" ,\" $type\" :\" Hocon.Config, Hocon.Configuration\" ,\" Root\" :{\" $type\" :\" Hocon.HoconEmptyValue, Hocon\" ,\" $values\" :[]},\" Value\" :{\" $type\" :\" Hocon.HoconEmptyValue, Hocon\" ,\" $values\" :[]},\" Substitutions\" :{\" $type\" :\" Hocon.HoconSubstitution[], Hocon\" ,\" $values\" :[]},\" IsEmpty\" :true}" ;
455
+ var restored = JsonConvert . DeserializeObject < Config > ( json , settings ) ;
456
+ restored . IsEmpty . Should ( ) . BeTrue ( ) ;
457
+ }
458
+
459
+ internal class AkkaContractResolver : DefaultContractResolver
460
+ {
461
+ protected override JsonProperty CreateProperty ( MemberInfo member , MemberSerialization memberSerialization )
462
+ {
463
+ var prop = base . CreateProperty ( member , memberSerialization ) ;
464
+
465
+ if ( ! prop . Writable )
466
+ {
467
+ var property = member as PropertyInfo ;
468
+ if ( property != null )
469
+ {
470
+ var hasPrivateSetter = property . GetSetMethod ( true ) != null ;
471
+ prop . Writable = hasPrivateSetter ;
472
+ }
473
+ }
474
+
475
+ return prop ;
476
+ }
477
+ }
430
478
}
431
479
}
0 commit comments