-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonSubTypesTests.mustache
125 lines (105 loc) · 3.25 KB
/
JsonSubTypesTests.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{{>partial_header}}
using System.Collections.Generic;
using System.Linq;
using JsonSubTypes;
using System.Text.Json;
using NUnit.Framework;
using {{packageName}}.{{apiPackage}};
using {{packageName}}.{{modelPackage}};
using {{packageName}}.Client;
namespace {{packageName}}.Test.Client
{
public class JsonSubTypesTests
{
[Test]
public void TestSimpleJsonSubTypesExample()
{
var animal =
JsonSerializer.Deserialize<IAnimal>("{\"Kind\":\"Dog\",\"Breed\":\"Jack Russell Terrier\"}");
Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed);
}
[Test]
public void DeserializeObjectWithCustomMapping()
{
var animal =
JsonSerializer.Deserialize<Animal2>("{\"Sound\":\"Bark\",\"Breed\":\"Jack Russell Terrier\"}");
Assert.AreEqual("Jack Russell Terrier", (animal as Dog2)?.Breed);
}
[Test]
public void DeserializeObjectMappingByPropertyPresence()
{
string json =
"[{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}," +
"{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}," +
"{\"Skill\":\"Painter\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}]";
var persons = JsonSerializer.Deserialize<ICollection<Person>>(json);
Assert.AreEqual("Painter", (persons.Last() as Artist)?.Skill);
}
}
[JsonConverter(typeof(JsonSubtypes), "Kind")]
public interface IAnimal
{
string Kind { get; }
}
public class Dog : IAnimal
{
public Dog()
{
Kind = "Dog";
}
public string Kind { get; }
public string Breed { get; set; }
}
class Cat : IAnimal
{
public Cat()
{
Kind = "Cat";
}
public string Kind { get; }
bool Declawed { get; set; }
}
[JsonConverter(typeof(JsonSubtypes), "Sound")]
[JsonSubtypes.KnownSubType(typeof(Dog2), "Bark")]
[JsonSubtypes.KnownSubType(typeof(Cat2), "Meow")]
public class Animal2
{
public virtual string Sound { get; }
public string Color { get; set; }
}
public class Dog2 : Animal2
{
public Dog2()
{
Sound = "Bark";
}
public override string Sound { get; }
public string Breed { get; set; }
}
public class Cat2 : Animal2
{
public Cat2()
{
Sound = "Meow";
}
public override string Sound { get; }
public bool Declawed { get; set; }
}
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Employee), "JobTitle")]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Artist), "Skill")]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class Artist : Person
{
public string Skill { get; set; }
}
}