All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Serialization / DefaultValueHandlingTests.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Text;
6 using Newtonsoft.Json.Tests.TestObjects;
7 using NUnit.Framework;
8
9 namespace Newtonsoft.Json.Tests.Serialization
10 {
11   public class DefaultValueHandlingTests : TestFixtureBase
12   {
13     [Test]
14     public void SerializeInvoice()
15     {
16       Invoice invoice = new Invoice
17       {
18         Company = "Acme Ltd.",
19         Amount = 50.0m,
20         Paid = false,
21         FollowUpDays = 30,
22         FollowUpEmailAddress = string.Empty,
23         PaidDate = null
24       };
25
26       string included = JsonConvert.SerializeObject(invoice,
27         Formatting.Indented,
28         new JsonSerializerSettings { });
29
30       // {
31       //   "Company": "Acme Ltd.",
32       //   "Amount": 50.0,
33       //   "Paid": false,
34       //   "PaidDate": null,
35       //   "FollowUpDays": 30,
36       //   "FollowUpEmailAddress": ""
37       // }
38
39       string ignored = JsonConvert.SerializeObject(invoice,
40         Formatting.Indented,
41         new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
42
43       // {
44       //   "Company": "Acme Ltd.",
45       //   "Amount": 50.0
46       // }
47
48       Console.WriteLine(included);
49       Console.WriteLine(ignored);
50     }
51
52     [Test]
53     public void DefaultValueAttributeTest()
54     {
55       string json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass(),
56         Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
57       Assert.AreEqual(@"{""TestField1"":0,""TestProperty1"":null}", json);
58
59       json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = int.MinValue, TestProperty1 = "NotDefault" },
60         Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
61       Assert.AreEqual(@"{""TestField1"":-2147483648,""TestProperty1"":""NotDefault""}", json);
62
63       json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "NotDefault" },
64         Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
65       Assert.AreEqual(@"{""TestProperty1"":""NotDefault""}", json);
66
67       json = JsonConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "TestProperty1Value" },
68         Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
69       Assert.AreEqual(@"{}", json);
70     }
71
72     [JsonObject]
73     public class NetworkUser
74     {
75       [JsonProperty(PropertyName = "userId")]
76       [DefaultValue(-1)]
77       public long GlobalId { get; set; }
78
79       [JsonProperty(PropertyName = "floatUserId")]
80       [DefaultValue(-1.0d)]
81       public float FloatGlobalId { get; set; }
82
83       [JsonProperty(PropertyName = "firstName")]
84       public string Firstname { get; set; }
85       [JsonProperty(PropertyName = "lastName")]
86       public string Lastname { get; set; }
87
88       public NetworkUser()
89       {
90         GlobalId = -1;
91         FloatGlobalId = -1.0f;
92       }
93     }
94
95     [Test]
96     public void IgnoreNumberTypeDifferencesWithDefaultValue()
97     {
98       NetworkUser user = new NetworkUser
99       {
100         Firstname = "blub"
101       };
102
103       string json = JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore });
104
105       Assert.AreEqual(@"{""firstName"":""blub""}", json);
106     }
107   }
108 }