All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Serialization / MissingMemberHandlingTests.cs
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.IO;
28 using Newtonsoft.Json.Converters;
29 using Newtonsoft.Json.Tests.TestObjects;
30 using NUnit.Framework;
31
32 namespace Newtonsoft.Json.Tests.Serialization
33 {
34   public class MissingMemberHandlingTests : TestFixtureBase
35   {
36     [Test]
37     [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Could not find member 'Price' on object of type 'ProductShort'")]
38     public void MissingMemberDeserialize()
39     {
40       Product product = new Product();
41
42       product.Name = "Apple";
43       product.ExpiryDate = new DateTime(2008, 12, 28);
44       product.Price = 3.99M;
45       product.Sizes = new string[] { "Small", "Medium", "Large" };
46
47       string output = JsonConvert.SerializeObject(product);
48       //{
49       //  "Name": "Apple",
50       //  "ExpiryDate": new Date(1230422400000),
51       //  "Price": 3.99,
52       //  "Sizes": [
53       //    "Small",
54       //    "Medium",
55       //    "Large"
56       //  ]
57       //}
58
59       ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
60     }
61
62     [Test]
63     public void MissingMemberDeserializeOkay()
64     {
65       Product product = new Product();
66
67       product.Name = "Apple";
68       product.ExpiryDate = new DateTime(2008, 12, 28);
69       product.Price = 3.99M;
70       product.Sizes = new string[] { "Small", "Medium", "Large" };
71
72       string output = JsonConvert.SerializeObject(product);
73       //{
74       //  "Name": "Apple",
75       //  "ExpiryDate": new Date(1230422400000),
76       //  "Price": 3.99,
77       //  "Sizes": [
78       //    "Small",
79       //    "Medium",
80       //    "Large"
81       //  ]
82       //}
83
84       JsonSerializer jsonSerializer = new JsonSerializer();
85       jsonSerializer.MissingMemberHandling = MissingMemberHandling.Ignore;
86
87       object deserializedValue;
88
89       using (JsonReader jsonReader = new JsonTextReader(new StringReader(output)))
90       {
91         deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort));
92       }
93
94       ProductShort deserializedProductShort = (ProductShort)deserializedValue;
95
96       Assert.AreEqual("Apple", deserializedProductShort.Name);
97       Assert.AreEqual(new DateTime(2008, 12, 28), deserializedProductShort.ExpiryDate);
98       Assert.AreEqual("Small", deserializedProductShort.Sizes[0]);
99       Assert.AreEqual("Medium", deserializedProductShort.Sizes[1]);
100       Assert.AreEqual("Large", deserializedProductShort.Sizes[2]);
101     }
102
103     [Test]
104     public void MissingMemberIgnoreComplexValue()
105     {
106       JsonSerializer serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore };
107       serializer.Converters.Add(new JavaScriptDateTimeConverter());
108
109       string response = @"{""PreProperty"":1,""DateProperty"":new Date(1225962698973),""PostProperty"":2}";
110
111       MyClass myClass = (MyClass)serializer.Deserialize(new StringReader(response), typeof(MyClass));
112
113       Assert.AreEqual(1, myClass.PreProperty);
114       Assert.AreEqual(2, myClass.PostProperty);
115     }
116
117     [Test]
118     [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not find member 'Missing' on object of type 'DoubleClass'")]
119     public void MissingMemeber()
120     {
121       string json = @"{""Missing"":1}";
122
123       JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
124     }
125
126     [Test]
127     public void MissingJson()
128     {
129       string json = @"{}";
130
131       JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings
132         {
133           MissingMemberHandling = MissingMemberHandling.Error
134         });
135     }
136   }
137 }