Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should fix Place the Discriminator property First by default #149

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions JsonSubTypes.Tests/DiscriminatorLocationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand Down
27 changes: 13 additions & 14 deletions JsonSubTypes.Tests/DynamicRegisterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -144,7 +144,7 @@ public void RegisterWithGenericTypes()
.RegisterSubtype<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 });

Expand Down Expand Up @@ -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]
Expand All @@ -532,10 +531,10 @@ public void TestNestedObjectInBothWay()
SubExpressionB = new ManyOrExpression2 { OrExpr = new List<IExpression2> { new ConstantExpression2 { Value = "A" }, new ManyOrExpression2 { OrExpr = new List<IExpression2> { 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);


Expand Down Expand Up @@ -566,10 +565,10 @@ public void TestNestedObjectInBothWayParallel()
SubExpressionB = new ManyOrExpression2 { OrExpr = new List<IExpression2> { new ConstantExpression2 { Value = "A" }, new ManyOrExpression2 { OrExpr = new List<IExpression2> { 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);


Expand Down
4 changes: 2 additions & 2 deletions JsonSubTypes/JsonSubtypesConverterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class JsonSubtypesConverterBuilder
private string _discriminatorProperty;
private readonly NullableDictionary<object, Type> _subTypeMapping = new NullableDictionary<object, Type>();
private bool _serializeDiscriminatorProperty;
private bool _addDiscriminatorFirst;
private bool _addDiscriminatorFirst = true;
private Type _fallbackSubtype;

public static JsonSubtypesConverterBuilder Of(Type baseType, string discriminatorProperty)
Expand All @@ -51,7 +51,7 @@ public static JsonSubtypesConverterBuilder Of<T>(string discriminatorProperty)

public JsonSubtypesConverterBuilder SerializeDiscriminatorProperty()
{
return SerializeDiscriminatorProperty(false);
return SerializeDiscriminatorProperty(true);
}

public JsonSubtypesConverterBuilder SerializeDiscriminatorProperty(bool addDiscriminatorFirst)
Expand Down