2 // Copyright (c) 2007 James Newton-King
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
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
28 using Newtonsoft.Json.Converters;
29 using Newtonsoft.Json.Tests.TestObjects;
30 using NUnit.Framework;
32 namespace Newtonsoft.Json.Tests.Serialization
34 public class MissingMemberHandlingTests : TestFixtureBase
37 [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Could not find member 'Price' on object of type 'ProductShort'")]
38 public void MissingMemberDeserialize()
40 Product product = new Product();
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" };
47 string output = JsonConvert.SerializeObject(product);
50 // "ExpiryDate": new Date(1230422400000),
59 ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
63 public void MissingMemberDeserializeOkay()
65 Product product = new Product();
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" };
72 string output = JsonConvert.SerializeObject(product);
75 // "ExpiryDate": new Date(1230422400000),
84 JsonSerializer jsonSerializer = new JsonSerializer();
85 jsonSerializer.MissingMemberHandling = MissingMemberHandling.Ignore;
87 object deserializedValue;
89 using (JsonReader jsonReader = new JsonTextReader(new StringReader(output)))
91 deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort));
94 ProductShort deserializedProductShort = (ProductShort)deserializedValue;
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]);
104 public void MissingMemberIgnoreComplexValue()
106 JsonSerializer serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore };
107 serializer.Converters.Add(new JavaScriptDateTimeConverter());
109 string response = @"{""PreProperty"":1,""DateProperty"":new Date(1225962698973),""PostProperty"":2}";
111 MyClass myClass = (MyClass)serializer.Deserialize(new StringReader(response), typeof(MyClass));
113 Assert.AreEqual(1, myClass.PreProperty);
114 Assert.AreEqual(2, myClass.PostProperty);
118 [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not find member 'Missing' on object of type 'DoubleClass'")]
119 public void MissingMemeber()
121 string json = @"{""Missing"":1}";
123 JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
127 public void MissingJson()
131 JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings
133 MissingMemberHandling = MissingMemberHandling.Error