~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.ComponentModel;
 
28
using System.IO;
 
29
using System.Runtime.Serialization;
 
30
#if !(SILVERLIGHT || PocketPC || NET20 || NET35 || NETFX_CORE || PORTABLE)
 
31
using System.Runtime.Serialization.Json;
 
32
#endif
 
33
using System.Text;
 
34
using Newtonsoft.Json.Tests.TestObjects;
 
35
#if !NETFX_CORE
 
36
using NUnit.Framework;
 
37
#else
 
38
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
39
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
40
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
41
#endif
 
42
using Newtonsoft.Json.Utilities;
 
43
 
 
44
namespace Newtonsoft.Json.Tests.Serialization
 
45
{
 
46
  [TestFixture]
 
47
  public class DefaultValueHandlingTests : TestFixtureBase
 
48
  {
 
49
    [Test]
 
50
    public void Include()
 
51
    {
 
52
      Invoice invoice = new Invoice
 
53
      {
 
54
        Company = "Acme Ltd.",
 
55
        Amount = 50.0m,
 
56
        Paid = false,
 
57
        FollowUpDays = 30,
 
58
        FollowUpEmailAddress = string.Empty,
 
59
        PaidDate = null
 
60
      };
 
61
 
 
62
      string included = JsonConvert.SerializeObject(invoice,
 
63
        Formatting.Indented,
 
64
        new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Include });
 
65
 
 
66
      Assert.AreEqual(@"{
 
67
  ""Company"": ""Acme Ltd."",
 
68
  ""Amount"": 50.0,
 
69
  ""Paid"": false,
 
70
  ""PaidDate"": null,
 
71
  ""FollowUpDays"": 30,
 
72
  ""FollowUpEmailAddress"": """"
 
73
}", included);
 
74
    }
 
75
 
 
76
    [Test]
 
77
    public void SerializeInvoice()
 
78
    {
 
79
      Invoice invoice = new Invoice
 
80
      {
 
81
        Company = "Acme Ltd.",
 
82
        Amount = 50.0m,
 
83
        Paid = false,
 
84
        FollowUpDays = 30,
 
85
        FollowUpEmailAddress = string.Empty,
 
86
        PaidDate = null
 
87
      };
 
88
 
 
89
      string included = JsonConvert.SerializeObject(invoice,
 
90
        Formatting.Indented,
 
91
        new JsonSerializerSettings { });
 
92
 
 
93
      Assert.AreEqual(@"{
 
94
  ""Company"": ""Acme Ltd."",
 
95
  ""Amount"": 50.0,
 
96
  ""Paid"": false,
 
97
  ""PaidDate"": null,
 
98
  ""FollowUpDays"": 30,
 
99
  ""FollowUpEmailAddress"": """"
 
100
}", included);
 
101
 
 
102
      string ignored = JsonConvert.SerializeObject(invoice,
 
103
        Formatting.Indented,
 
104
        new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
 
105
 
 
106
      Assert.AreEqual(@"{
 
107
  ""Company"": ""Acme Ltd."",
 
108
  ""Amount"": 50.0
 
109
}", ignored);
 
110
    }
 
111
 
 
112
    [Test]
 
113
    public void SerializeDefaultValueAttributeTest()
 
114
    {
 
115
      string json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass(),
 
116
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
 
117
      Assert.AreEqual(@"{""TestField1"":0,""TestProperty1"":null}", json);
 
118
 
 
119
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = int.MinValue, TestProperty1 = "NotDefault" },
 
120
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
 
121
      Assert.AreEqual(@"{""TestField1"":-2147483648,""TestProperty1"":""NotDefault""}", json);
 
122
 
 
123
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "NotDefault" },
 
124
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
 
125
      Assert.AreEqual(@"{""TestProperty1"":""NotDefault""}", json);
 
126
 
 
127
      json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "TestProperty1Value" },
 
128
        Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
 
129
      Assert.AreEqual(@"{}", json);
 
130
    }
 
131
 
 
132
    [Test]
 
133
    public void DeserializeDefaultValueAttributeTest()
 
134
    {
 
135
      string json = "{}";
 
136
 
 
137
      DefaultValueAttributeTestClass c = JsonConvert.DeserializeObject<DefaultValueAttributeTestClass>(json, new JsonSerializerSettings
 
138
        {
 
139
          DefaultValueHandling = DefaultValueHandling.Populate
 
140
        });
 
141
      Assert.AreEqual("TestProperty1Value", c.TestProperty1);
 
142
 
 
143
      c = JsonConvert.DeserializeObject<DefaultValueAttributeTestClass>(json, new JsonSerializerSettings
 
144
      {
 
145
        DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
 
146
      });
 
147
      Assert.AreEqual("TestProperty1Value", c.TestProperty1);
 
148
    }
 
149
 
 
150
    [JsonObject]
 
151
    public class NetworkUser
 
152
    {
 
153
      [JsonProperty(PropertyName = "userId")]
 
154
      [DefaultValue(-1)]
 
155
      public long GlobalId { get; set; }
 
156
 
 
157
      [JsonProperty(PropertyName = "age")]
 
158
      [DefaultValue(0)]
 
159
      public int Age { get; set; }
 
160
 
 
161
      [JsonProperty(PropertyName = "amount")]
 
162
      [DefaultValue(0.0)]
 
163
      public decimal Amount { get; set; }
 
164
 
 
165
      [JsonProperty(PropertyName = "floatUserId")]
 
166
      [DefaultValue(-1.0d)]
 
167
      public float FloatGlobalId { get; set; }
 
168
 
 
169
      [JsonProperty(PropertyName = "firstName")]
 
170
      public string Firstname { get; set; }
 
171
      [JsonProperty(PropertyName = "lastName")]
 
172
      public string Lastname { get; set; }
 
173
 
 
174
      public NetworkUser()
 
175
      {
 
176
        GlobalId = -1;
 
177
        FloatGlobalId = -1.0f;
 
178
        Amount = 0.0m;
 
179
        Age = 0;
 
180
      }
 
181
    }
 
182
 
 
183
    [Test]
 
184
    public void IgnoreNumberTypeDifferencesWithDefaultValue()
 
185
    {
 
186
      NetworkUser user = new NetworkUser
 
187
      {
 
188
        Firstname = "blub"
 
189
      };
 
190
 
 
191
      string json = JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore });
 
192
 
 
193
      Assert.AreEqual(@"{""firstName"":""blub""}", json);
 
194
    }
 
195
 
 
196
    [Test]
 
197
    public void ApproxEquals()
 
198
    {
 
199
      Assert.IsTrue(MathUtils.ApproxEquals(0.0, 0.0));
 
200
      Assert.IsTrue(MathUtils.ApproxEquals(1000.0, 1000.0000000000001));
 
201
 
 
202
      Assert.IsFalse(MathUtils.ApproxEquals(1000.0, 1000.000000000001));
 
203
      Assert.IsFalse(MathUtils.ApproxEquals(0.0, 0.00001));
 
204
    }
 
205
 
 
206
#if !NET20
 
207
    [Test]
 
208
    public void EmitDefaultValueTest()
 
209
    {
 
210
      EmitDefaultValueClass c = new EmitDefaultValueClass();
 
211
 
 
212
#if !(SILVERLIGHT || PocketPC || NET20 || NET35 || NETFX_CORE || PORTABLE)
 
213
      DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(EmitDefaultValueClass));
 
214
 
 
215
      MemoryStream ms = new MemoryStream();
 
216
      jsonSerializer.WriteObject(ms, c);
 
217
 
 
218
      Assert.AreEqual("{}", Encoding.UTF8.GetString(ms.ToArray()));
 
219
#endif
 
220
 
 
221
      string json = JsonConvert.SerializeObject(c);
 
222
 
 
223
      Assert.AreEqual("{}", json);
 
224
    }
 
225
#endif
 
226
 
 
227
    [Test]
 
228
    public void DefaultValueHandlingPropertyTest()
 
229
    {
 
230
      DefaultValueHandlingPropertyClass c = new DefaultValueHandlingPropertyClass();
 
231
 
 
232
      string json = JsonConvert.SerializeObject(c, Formatting.Indented);
 
233
 
 
234
      Assert.AreEqual(@"{
 
235
  ""IntInclude"": 0,
 
236
  ""IntDefault"": 0
 
237
}", json);
 
238
 
 
239
      json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
 
240
        {
 
241
          DefaultValueHandling = DefaultValueHandling.Ignore
 
242
        });
 
243
 
 
244
      Assert.AreEqual(@"{
 
245
  ""IntInclude"": 0
 
246
}", json);
 
247
 
 
248
      json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
 
249
      {
 
250
        DefaultValueHandling = DefaultValueHandling.Include
 
251
      });
 
252
 
 
253
      Assert.AreEqual(@"{
 
254
  ""IntInclude"": 0,
 
255
  ""IntDefault"": 0
 
256
}", json);
 
257
    }
 
258
 
 
259
    [Test]
 
260
    public void DeserializeWithIgnore()
 
261
    {
 
262
      string json = @"{'Value':null,'IntValue1':1,'IntValue2':0,'IntValue3':null}";
 
263
 
 
264
      var o = JsonConvert.DeserializeObject<DefaultValueHandlingDeserializeHolder>(json, new JsonSerializerSettings
 
265
        {
 
266
          DefaultValueHandling = DefaultValueHandling.Ignore
 
267
        });
 
268
 
 
269
      Assert.AreEqual(int.MaxValue, o.IntValue1);
 
270
      Assert.AreEqual(int.MinValue, o.IntValue2);
 
271
      Assert.AreEqual(int.MaxValue, o.IntValue3);
 
272
      Assert.AreEqual("Derp!", o.ClassValue.Derp);
 
273
    }
 
274
 
 
275
    [Test]
 
276
    public void DeserializeWithPopulate()
 
277
    {
 
278
      string json = @"{}";
 
279
 
 
280
      var o = JsonConvert.DeserializeObject<DefaultValueHandlingDeserializePopulate>(json, new JsonSerializerSettings
 
281
      {
 
282
        DefaultValueHandling = DefaultValueHandling.Populate
 
283
      });
 
284
 
 
285
      Assert.AreEqual(1, o.IntValue1);
 
286
      Assert.AreEqual(0, o.IntValue2);
 
287
      Assert.AreEqual(null, o.ClassValue);
 
288
    }
 
289
  }
 
290
 
 
291
  public class DefaultValueHandlingDeserialize
 
292
  {
 
293
    public string Derp { get; set; }
 
294
  }
 
295
 
 
296
  public class DefaultValueHandlingDeserializeHolder
 
297
  {
 
298
    public DefaultValueHandlingDeserializeHolder()
 
299
    {
 
300
      ClassValue = new DefaultValueHandlingDeserialize
 
301
        {
 
302
          Derp = "Derp!"
 
303
        };
 
304
      IntValue1 = int.MaxValue;
 
305
      IntValue2 = int.MinValue;
 
306
      IntValue3 = int.MaxValue;
 
307
    }
 
308
 
 
309
    [DefaultValue(1)]
 
310
    public int IntValue1 { get; set; }
 
311
    public int IntValue2 { get; set; }
 
312
    [DefaultValue(null)]
 
313
    public int IntValue3 { get; set; }
 
314
    public DefaultValueHandlingDeserialize ClassValue { get; set; }
 
315
  }
 
316
 
 
317
  public class DefaultValueHandlingDeserializePopulate
 
318
  {
 
319
    public DefaultValueHandlingDeserializePopulate()
 
320
    {
 
321
      ClassValue = new DefaultValueHandlingDeserialize
 
322
      {
 
323
        Derp = "Derp!"
 
324
      };
 
325
      IntValue1 = int.MaxValue;
 
326
      IntValue2 = int.MinValue;
 
327
    }
 
328
 
 
329
    [DefaultValue(1)]
 
330
    public int IntValue1 { get; set; }
 
331
    public int IntValue2 { get; set; }
 
332
    public DefaultValueHandlingDeserialize ClassValue { get; set; }
 
333
  }
 
334
 
 
335
  public struct DefaultStruct
 
336
  {
 
337
    public string Default { get; set; }
 
338
  }
 
339
 
 
340
  public class DefaultValueHandlingPropertyClass
 
341
  {
 
342
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
 
343
    public int IntIgnore { get; set; }
 
344
 
 
345
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
 
346
    public int IntInclude { get; set; }
 
347
 
 
348
    [JsonProperty]
 
349
    public int IntDefault { get; set; }
 
350
  }
 
351
 
 
352
#if !NET20
 
353
  [DataContract]
 
354
  public class EmitDefaultValueClass
 
355
  {
 
356
    [DataMember(EmitDefaultValue = false)]
 
357
    public Guid Guid { get; set; }
 
358
    [DataMember(EmitDefaultValue = false)]
 
359
    public TimeSpan TimeSpan { get; set; }
 
360
    [DataMember(EmitDefaultValue = false)]
 
361
    public DateTime DateTime { get; set; }
 
362
    [DataMember(EmitDefaultValue = false)]
 
363
    public DateTimeOffset DateTimeOffset { get; set; }
 
364
    [DataMember(EmitDefaultValue = false)]
 
365
    public decimal Decimal { get; set; }
 
366
    [DataMember(EmitDefaultValue = false)]
 
367
    public int Integer { get; set; }
 
368
    [DataMember(EmitDefaultValue = false)]
 
369
    public double Double { get; set; }
 
370
    [DataMember(EmitDefaultValue = false)]
 
371
    public bool Boolean { get; set; }
 
372
    [DataMember(EmitDefaultValue = false)]
 
373
    public DefaultStruct Struct { get; set; }
 
374
    [DataMember(EmitDefaultValue = false)]
 
375
    public StringComparison Enum { get; set; }
 
376
 
 
377
    [DataMember(EmitDefaultValue = false)]
 
378
    public Guid? NullableGuid { get; set; }
 
379
    [DataMember(EmitDefaultValue = false)]
 
380
    public TimeSpan? NullableTimeSpan { get; set; }
 
381
    [DataMember(EmitDefaultValue = false)]
 
382
    public DateTime? NullableDateTime { get; set; }
 
383
    [DataMember(EmitDefaultValue = false)]
 
384
    public DateTimeOffset? NullableDateTimeOffset { get; set; }
 
385
    [DataMember(EmitDefaultValue = false)]
 
386
    public decimal? NullableDecimal { get; set; }
 
387
    [DataMember(EmitDefaultValue = false)]
 
388
    public int? NullableInteger { get; set; }
 
389
    [DataMember(EmitDefaultValue = false)]
 
390
    public double? NullableDouble { get; set; }
 
391
    [DataMember(EmitDefaultValue = false)]
 
392
    public bool? NullableBoolean { get; set; }
 
393
    [DataMember(EmitDefaultValue = false)]
 
394
    public DefaultStruct? NullableStruct { get; set; }
 
395
    [DataMember(EmitDefaultValue = false)]
 
396
    public StringComparison? NullableEnum { get; set; }
 
397
 
 
398
    [DataMember(EmitDefaultValue = false)]
 
399
    public object Object { get; set; }
 
400
  }
 
401
#endif
 
402
}
 
 
b'\\ No newline at end of file'