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;
31
using NUnit.Framework;
33
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
34
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
35
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
38
namespace Newtonsoft.Json.Tests.Serialization
41
public class MissingMemberHandlingTests : TestFixtureBase
44
public void MissingMemberDeserialize()
46
Product product = new Product();
48
product.Name = "Apple";
49
product.ExpiryDate = new DateTime(2008, 12, 28);
50
product.Price = 3.99M;
51
product.Sizes = new string[] { "Small", "Medium", "Large" };
53
string output = JsonConvert.SerializeObject(product, Formatting.Indented);
56
// "ExpiryDate": new Date(1230422400000),
65
ExceptionAssert.Throws<JsonSerializationException>(
66
@"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 11.",
69
ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
74
public void MissingMemberDeserializeOkay()
76
Product product = new Product();
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" };
83
string output = JsonConvert.SerializeObject(product);
86
// "ExpiryDate": new Date(1230422400000),
95
JsonSerializer jsonSerializer = new JsonSerializer();
96
jsonSerializer.MissingMemberHandling = MissingMemberHandling.Ignore;
98
object deserializedValue;
100
using (JsonReader jsonReader = new JsonTextReader(new StringReader(output)))
102
deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort));
105
ProductShort deserializedProductShort = (ProductShort)deserializedValue;
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]);
115
public void MissingMemberIgnoreComplexValue()
117
JsonSerializer serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore };
118
serializer.Converters.Add(new JavaScriptDateTimeConverter());
120
string response = @"{""PreProperty"":1,""DateProperty"":new Date(1225962698973),""PostProperty"":2}";
122
MyClass myClass = (MyClass)serializer.Deserialize(new StringReader(response), typeof(MyClass));
124
Assert.AreEqual(1, myClass.PreProperty);
125
Assert.AreEqual(2, myClass.PostProperty);
129
public void MissingMemeber()
131
string json = @"{""Missing"":1}";
133
ExceptionAssert.Throws<JsonSerializationException>(
134
"Could not find member 'Missing' on object of type 'DoubleClass'. Path 'Missing', line 1, position 11.",
137
JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
142
public void MissingJson()
146
JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings
148
MissingMemberHandling = MissingMemberHandling.Error
b'\\ No newline at end of file'