From 4c695bcb2688c636d62a743120e4fec8e2a1ef40 Mon Sep 17 00:00:00 2001 From: manuc66 Date: Sat, 10 Sep 2022 21:30:19 +0200 Subject: [PATCH] Should fix Place the Discriminator property First by default #46 --- .../DiscriminatorLocationTests.cs | 30 ++++++++++++++++--- JsonSubTypes.Tests/DynamicRegisterTests.cs | 27 ++++++++--------- JsonSubTypes/JsonSubtypesConverterBuilder.cs | 4 +-- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/JsonSubTypes.Tests/DiscriminatorLocationTests.cs b/JsonSubTypes.Tests/DiscriminatorLocationTests.cs index 068ae0a..c5321ab 100644 --- a/JsonSubTypes.Tests/DiscriminatorLocationTests.cs +++ b/JsonSubTypes.Tests/DiscriminatorLocationTests.cs @@ -48,9 +48,9 @@ public void CheckFirst() Assert.AreEqual(json_first, result); } - + [Test] - public void CheckDefaultLast() + public void CheckFirstByDefault() { var settings = new JsonSerializerSettings(); JsonConvert.DefaultSettings = () => settings; @@ -66,13 +66,35 @@ public void CheckDefaultLast() SimpleBase test_object = new SimpleChildA() { Name = "bob", Age = 12 }; - var json_first = "{\"Age\":12,\"Name\":\"bob\",\"type\":\"TypeA\"}"; + var json_first = "{\"type\":\"TypeA\",\"Age\":12,\"Name\":\"bob\"}"; var result = JsonConvert.SerializeObject(test_object); - + Assert.AreEqual(json_first, result); } + [Test] + public void CheckDefaultNotFirst() + { + var settings = new JsonSerializerSettings(); + JsonConvert.DefaultSettings = () => settings; + + settings.Converters.Add(JsonSubtypesConverterBuilder + .Of(typeof(SimpleBase), "type") + .SerializeDiscriminatorProperty(false) + .RegisterSubtype(typeof(SimpleChildA), "TypeA") + .RegisterSubtype(typeof(SimpleChildB), "TypeB") + .Build()); + + + SimpleBase test_object = new SimpleChildA() { Name = "bob", Age = 12 }; + + + var result = JsonConvert.SerializeObject(test_object); + + Assert.AreEqual("{\"Age\":12,\"Name\":\"bob\",\"type\":\"TypeA\"}", result); + } + [Test] public void CheckExplicitLast() { diff --git a/JsonSubTypes.Tests/DynamicRegisterTests.cs b/JsonSubTypes.Tests/DynamicRegisterTests.cs index c4de678..3e48a92 100644 --- a/JsonSubTypes.Tests/DynamicRegisterTests.cs +++ b/JsonSubTypes.Tests/DynamicRegisterTests.cs @@ -124,7 +124,7 @@ public void SerializeTest() .RegisterSubtype(typeof(Dog), AnimalType.Dog) .Build()); - var json = "{\"catLives\":6,\"age\":11,\"type\":2}"; + var json = "{\"type\":2,\"catLives\":6,\"age\":11}"; var result = JsonConvert.SerializeObject(new Cat { Age = 11, Lives = 6 }); @@ -144,7 +144,7 @@ public void RegisterWithGenericTypes() .RegisterSubtype(AnimalType.Dog) .Build()); - var json = "{\"catLives\":6,\"age\":11,\"type\":2}"; + var json = "{\"type\":2,\"catLives\":6,\"age\":11}"; var result = JsonConvert.SerializeObject(new Cat { Age = 11, Lives = 6 }); @@ -506,10 +506,9 @@ public void TestIfNestedObjectIsSerialized2() SubExpressionB = new ConstantExpression2 { Value = "B" } }); - Assert.AreEqual("{" + - "\"SubExpressionA\":{\"Value\":\"A\",\"Type\":\"Constant\"}," + - "\"SubExpressionB\":{\"Value\":\"B\",\"Type\":\"Constant\"}" + - ",\"Type\":\"Binary\"}", target); + Assert.AreEqual("{\"Type\":\"Binary\"," + + "\"SubExpressionA\":{\"Type\":\"Constant\",\"Value\":\"A\"}," + + "\"SubExpressionB\":{\"Type\":\"Constant\",\"Value\":\"B\"}}", target); } [Test] @@ -532,10 +531,10 @@ public void TestNestedObjectInBothWay() SubExpressionB = new ManyOrExpression2 { OrExpr = new List { new ConstantExpression2 { Value = "A" }, new ManyOrExpression2 { OrExpr = new List { new ConstantExpression2 { Value = "A" }, new ConstantExpression2 { Value = "B" } } } } } }); - var json = "{" + - "\"SubExpressionA\":{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}," + - "\"SubExpressionB\":{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}],\"Type\":\"ManyOr\"}" + - ",\"Type\":\"Binary\"}"; + var json = "{\"Type\":\"Binary\"," + + "\"SubExpressionA\":{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"Constant\",\"Value\":\"B\"}]}," + + "\"SubExpressionB\":{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"Constant\",\"Value\":\"B\"}]}]}" + + "}"; Assert.AreEqual(json, target); @@ -566,10 +565,10 @@ public void TestNestedObjectInBothWayParallel() SubExpressionB = new ManyOrExpression2 { OrExpr = new List { new ConstantExpression2 { Value = "A" }, new ManyOrExpression2 { OrExpr = new List { new ConstantExpression2 { Value = "A" }, new ConstantExpression2 { Value = "B" } } } } } }); - var json = "{" + - "\"SubExpressionA\":{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}," + - "\"SubExpressionB\":{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"OrExpr\":[{\"Value\":\"A\",\"Type\":\"Constant\"},{\"Value\":\"B\",\"Type\":\"Constant\"}],\"Type\":\"ManyOr\"}],\"Type\":\"ManyOr\"}" + - ",\"Type\":\"Binary\"}"; + var json = "{\"Type\":\"Binary\"," + + "\"SubExpressionA\":{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"Constant\",\"Value\":\"B\"}]}," + + "\"SubExpressionB\":{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"ManyOr\",\"OrExpr\":[{\"Type\":\"Constant\",\"Value\":\"A\"},{\"Type\":\"Constant\",\"Value\":\"B\"}]}]}" + + "}"; Assert.AreEqual(json, target); diff --git a/JsonSubTypes/JsonSubtypesConverterBuilder.cs b/JsonSubTypes/JsonSubtypesConverterBuilder.cs index 49c5c48..06d2cd3 100644 --- a/JsonSubTypes/JsonSubtypesConverterBuilder.cs +++ b/JsonSubTypes/JsonSubtypesConverterBuilder.cs @@ -31,7 +31,7 @@ public class JsonSubtypesConverterBuilder private string _discriminatorProperty; private readonly NullableDictionary _subTypeMapping = new NullableDictionary(); private bool _serializeDiscriminatorProperty; - private bool _addDiscriminatorFirst; + private bool _addDiscriminatorFirst = true; private Type _fallbackSubtype; public static JsonSubtypesConverterBuilder Of(Type baseType, string discriminatorProperty) @@ -51,7 +51,7 @@ public static JsonSubtypesConverterBuilder Of(string discriminatorProperty) public JsonSubtypesConverterBuilder SerializeDiscriminatorProperty() { - return SerializeDiscriminatorProperty(false); + return SerializeDiscriminatorProperty(true); } public JsonSubtypesConverterBuilder SerializeDiscriminatorProperty(bool addDiscriminatorFirst)