Skip to content

Commit c718aa0

Browse files
Added test + fixed config deserialization (#179)
Co-authored-by: Aaron Stannard <[email protected]>
1 parent cccfe97 commit c718aa0

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Hocon.Configuration.Test/ConfigurationSpec.cs

+48
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
// -----------------------------------------------------------------------
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Configuration;
910
using System.Linq;
11+
using System.Reflection;
1012
using FluentAssertions;
13+
using Newtonsoft.Json;
14+
using Newtonsoft.Json.Serialization;
1115
using Xunit;
1216

1317
namespace Hocon.Configuration.Tests
@@ -427,5 +431,49 @@ public void ShouldSerializeFallbackValues()
427431
c.ToString().Should().NotContain("other-key", "Fallback values are ignored by default");
428432
c.ToString(true).Should().Contain("other-key", "Fallback values should be displayed when requested");
429433
}
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+
}
430478
}
431479
}

src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="FluentAssertions" Version="5.10.0" />
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
11+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1112
<PackageReference Include="xunit" Version="$(XunitVersion)" />
1213
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitCliVersion)" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)">

src/Hocon/Impl/HoconEmptyValue.cs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Hocon
1414
/// </summary>
1515
public sealed class HoconEmptyValue : HoconValue
1616
{
17+
public HoconEmptyValue() : base(null) { }
18+
1719
public HoconEmptyValue(IHoconElement parent) : base(parent)
1820
{
1921
}

0 commit comments

Comments
 (0)