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