File: CustomCreationConverterTests.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (235 lines) | stat: -rw-r--r-- 6,406 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
´╗┐using System;
using System.Collections.Generic;
using Newtonsoft.Json.Converters;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif

namespace Newtonsoft.Json.Tests.Converters
{
  [TestFixture]
  public class CustomCreationConverterTests : TestFixtureBase
  {
    public interface IPerson
    {
      string FirstName { get; set; }
      string LastName { get; set; }
      DateTime BirthDate { get; set; }
    }

    public class Employee : IPerson
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public DateTime BirthDate { get; set; }

      public string Department { get; set; }
      public string JobTitle { get; set; }
    }

    public class PersonConverter : CustomCreationConverter<IPerson>
    {
      public override IPerson Create(Type objectType)
      {
        return new Employee();
      }
    }

    public void DeserializeObject()
    {
      string json = JsonConvert.SerializeObject(new List<Employee>
        {
          new Employee
            {
              BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
              FirstName = "Maurice",
              LastName = "Moss",
              Department = "IT",
              JobTitle = "Support"
            },
          new Employee
            {
              BirthDate = new DateTime(1978, 3, 15, 1, 1, 1, DateTimeKind.Utc),
              FirstName = "Jen",
              LastName = "Barber",
              Department = "IT",
              JobTitle = "Manager"
            }
        }, Formatting.Indented);

      //[
      //  {
      //    "FirstName": "Maurice",
      //    "LastName": "Moss",
      //    "BirthDate": "\/Date(252291661000)\/",
      //    "Department": "IT",
      //    "JobTitle": "Support"
      //  },
      //  {
      //    "FirstName": "Jen",
      //    "LastName": "Barber",
      //    "BirthDate": "\/Date(258771661000)\/",
      //    "Department": "IT",
      //    "JobTitle": "Manager"
      //  }
      //]

      List<IPerson> people = JsonConvert.DeserializeObject<List<IPerson>>(json, new PersonConverter());

      IPerson person = people[0];

      Console.WriteLine(person.GetType());
      // Newtonsoft.Json.Tests.Employee

      Console.WriteLine(person.FirstName);
      // Maurice

      Employee employee = (Employee)person;

      Console.WriteLine(employee.JobTitle);
      // Support
    }

    public class MyClass
    {
      public string Value { get; set; }

      [JsonConverter(typeof(MyThingConverter))]
      public IThing Thing { get; set; }
    }

    public interface IThing
    {
      int Number { get; }
    }

    public class MyThing : IThing
    {
      public int Number { get; set; }
    }

    public class MyThingConverter : CustomCreationConverter<IThing>
    {
      public override IThing Create(Type objectType)
      {
        return new MyThing();
      }
    }

    [Test]
    public void AssertDoesDeserialize()
    {
      const string json = @"{
""Value"": ""A value"",
""Thing"": {
""Number"": 123
}
}
";
      MyClass myClass = JsonConvert.DeserializeObject<MyClass>(json);
      Assert.IsNotNull(myClass);
      Assert.AreEqual("A value", myClass.Value);
      Assert.IsNotNull(myClass.Thing);
      Assert.AreEqual(123, myClass.Thing.Number);
    }

    [Test]
    public void AssertShouldSerializeTest()
    {
      MyClass myClass = new MyClass
      {
        Value = "Foo",
        Thing = new MyThing { Number = 456, }
      };
      string json = JsonConvert.SerializeObject(myClass); // <-- Exception here

      const string expected = @"{""Value"":""Foo"",""Thing"":{""Number"":456}}";
      Assert.AreEqual(expected, json);
    }

    internal interface IRange<T>
    {
      T First { get; }
      T Last { get; }
    }

    internal class Range<T> : IRange<T>
    {
      public T First { get; set; }
      public T Last { get; set; }
    }

    internal class NullInterfaceTestClass
    {
      public virtual Guid Id { get; set; }
      public virtual int? Year { get; set; }
      public virtual string Company { get; set; }
      public virtual IRange<decimal> DecimalRange { get; set; }
      public virtual IRange<int> IntRange { get; set; }
      public virtual IRange<decimal> NullDecimalRange { get; set; }
    }

    internal class DecimalRangeConverter : CustomCreationConverter<IRange<decimal>>
    {
      public override IRange<decimal> Create(Type objectType)
      {
        return new Range<decimal>();
      }
    }

    internal class IntRangeConverter : CustomCreationConverter<IRange<int>>
    {
      public override IRange<int> Create(Type objectType)
      {
        return new Range<int>();
      }
    }

    [Test]
    public void DeserializeAndConvertNullValue()
    {
      NullInterfaceTestClass initial = new NullInterfaceTestClass
      {
        Company = "Company!",
        DecimalRange = new Range<decimal> { First = 0, Last = 1 },
        Id = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
        IntRange = new Range<int> { First = int.MinValue, Last = int.MaxValue },
        Year = 2010,
        NullDecimalRange = null
      };

      string json = JsonConvert.SerializeObject(initial, Formatting.Indented);

      Assert.AreEqual(@"{
  ""Id"": ""00000001-0002-0003-0405-060708090a0b"",
  ""Year"": 2010,
  ""Company"": ""Company!"",
  ""DecimalRange"": {
    ""First"": 0.0,
    ""Last"": 1.0
  },
  ""IntRange"": {
    ""First"": -2147483648,
    ""Last"": 2147483647
  },
  ""NullDecimalRange"": null
}", json);

      NullInterfaceTestClass deserialized = JsonConvert.DeserializeObject<NullInterfaceTestClass>(
        json, new IntRangeConverter(), new DecimalRangeConverter());

      Assert.AreEqual("Company!", deserialized.Company);
      Assert.AreEqual(new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), deserialized.Id);
      Assert.AreEqual(0, deserialized.DecimalRange.First);
      Assert.AreEqual(1, deserialized.DecimalRange.Last);
      Assert.AreEqual(int.MinValue, deserialized.IntRange.First);
      Assert.AreEqual(int.MaxValue, deserialized.IntRange.Last);
      Assert.AreEqual(null, deserialized.NullDecimalRange);
      Assert.AreEqual(2010, deserialized.Year);
    }
  }
}