Skip to content

Commit

Permalink
Merge pull request #118 from xzxzxc/fix_deserialization_hierarchy
Browse files Browse the repository at this point in the history
Fix deserialization of hierarchy with multiple levels
  • Loading branch information
manuc66 authored Nov 7, 2020
2 parents d011a77 + 91a6f95 commit ca88195
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
74 changes: 74 additions & 0 deletions JsonSubTypes.Tests/MultipleHierarchyLevelsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,78 @@ public class Run : Game
{
}
}

[TestFixture]
public class MultipleHierarchyLevelsSubtypesWithPropertyDynamicRegistrationTests
{
JsonSerializerSettings settings;

[SetUp]
public void Init()
{
settings = new JsonSerializerSettings();
JsonConvert.DefaultSettings = () => settings;

settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(Payload), Payload.PAYLOAD_KIND)
.RegisterSubtype(typeof(Game), PayloadDiscriminator.GAME)
.RegisterSubtype(typeof(Com), PayloadDiscriminator.COM)
.Build());

settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder
.Of(typeof(Game))
.RegisterSubtypeWithProperty(typeof(Walk), nameof(Walk.FootCount))
.RegisterSubtypeWithProperty(typeof(Ride), nameof(Ride.WheelCount))
.Build());
}

[Test]
public void ShouldDeserializeNestedLevel()
{
var data = "{\"$PayloadKind\":1,\"WheelCount\":2}";
Assert.IsInstanceOf<Ride>(JsonConvert.DeserializeObject<Payload>(data, settings));
}

[Test]
public void ShouldSerializeNestedLevel()
{
Payload ride = new Ride();
var data = JsonConvert.SerializeObject(ride, settings);
Assert.AreEqual("{\"WheelCount\":0,\"$PayloadKind\":1}", data);
}

public enum PayloadDiscriminator
{
COM = 0,
GAME = 1
}

public abstract class Payload
{
public const string PAYLOAD_KIND = "$PayloadKind";

[JsonProperty(PAYLOAD_KIND)] public abstract PayloadDiscriminator PayloadKind { get; }
}

public abstract class Game : Payload
{
public override PayloadDiscriminator PayloadKind => PayloadDiscriminator.GAME;

}

public class Com : Payload
{
public override PayloadDiscriminator PayloadKind => PayloadDiscriminator.COM;
}

public class Walk : Game
{
public int FootCount { get; set; }
}

public class Ride : Game
{
public int WheelCount { get; set; }
}
}
}
12 changes: 6 additions & 6 deletions JsonSubTypes/JsonSubtypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
namespace JsonSubTypes
{
// MIT License
//
//
// Copyright (c) 2017 Emmanuel Counasse
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -248,7 +248,7 @@ private Type GetType(JObject jObject, Type parentType, JsonSerializer serializer
JsonSubtypes currentTypeResolver = this;
var visitedTypes = new HashSet<Type> { targetType };

var jsonConverterCollection = serializer.Converters.OfType<JsonSubtypesByDiscriminatorValueConverter>().ToList();
var jsonConverterCollection = serializer.Converters.OfType<JsonSubtypes>().ToList();
while (currentTypeResolver != null && currentTypeResolver != lastTypeResolver)
{
targetType = currentTypeResolver.ResolveType(jObject, targetType, serializer);
Expand All @@ -265,7 +265,7 @@ private Type GetType(JObject jObject, Type parentType, JsonSerializer serializer
return targetType;
}

private JsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable<JsonSubtypesByDiscriminatorValueConverter> jsonConverterCollection)
private JsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable<JsonSubtypes> jsonConverterCollection)
{
if (targetType == null)
{
Expand Down

0 comments on commit ca88195

Please sign in to comment.