Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Serialization\MissingMemberHandlingTests.cs

Symbol Coverage: 94.74% (36 of 38)

Branch Coverage: 71.43% (5 of 7)

Cyclomatic Complexity Avg: 1.17 Max:2

Code Lines: 41


L V Source
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
 1
      Product product = new Product();
41

  
42
 1
      product.Name = "Apple";
43
 1
      product.ExpiryDate = new DateTime(2008, 12, 28);
44
 1
      product.Price = 3.99M;
45
 1
      product.Sizes = new string[] { "Small", "Medium", "Large" };
46

  
47
 1
      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
 1
      ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
60
 0
    }
61

  
62
    [Test]
63
    public void MissingMemberDeserializeOkay()
64
    {
65
 1
      Product product = new Product();
66

  
67
 1
      product.Name = "Apple";
68
 1
      product.ExpiryDate = new DateTime(2008, 12, 28);
69
 1
      product.Price = 3.99M;
70
 1
      product.Sizes = new string[] { "Small", "Medium", "Large" };
71

  
72
 1
      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
 1
      JsonSerializer jsonSerializer = new JsonSerializer();
85
 1
      jsonSerializer.MissingMemberHandling = MissingMemberHandling.Ignore;
86

  
87
      object deserializedValue;
88

  
89
 1
      using (JsonReader jsonReader = new JsonTextReader(new StringReader(output)))
90
      {
91
 1
        deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort));
92
      }
93

  
94
 1
      ProductShort deserializedProductShort = (ProductShort)deserializedValue;
95

  
96
 1
      Assert.AreEqual("Apple", deserializedProductShort.Name);
97
 1
      Assert.AreEqual(new DateTime(2008, 12, 28), deserializedProductShort.ExpiryDate);
98
 1
      Assert.AreEqual("Small", deserializedProductShort.Sizes[0]);
99
 1
      Assert.AreEqual("Medium", deserializedProductShort.Sizes[1]);
100
 1
      Assert.AreEqual("Large", deserializedProductShort.Sizes[2]);
101
 1
    }
102

  
103
    [Test]
104
    public void MissingMemberIgnoreComplexValue()
105
    {
106
 1
      JsonSerializer serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Ignore };
107
 1
      serializer.Converters.Add(new JavaScriptDateTimeConverter());
108

  
109
 1
      string response = @"{""PreProperty"":1,""DateProperty"":new Date(1225962698973),""PostProperty"":2}";
110

  
111
 1
      MyClass myClass = (MyClass)serializer.Deserialize(new StringReader(response), typeof(MyClass));
112

  
113
 1
      Assert.AreEqual(1, myClass.PreProperty);
114
 1
      Assert.AreEqual(2, myClass.PostProperty);
115
 1
    }
116

  
117
    [Test]
118
    [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not find member 'Missing' on object of type 'DoubleClass'")]
119
    public void MissingMemeber()
120
    {
121
 1
      string json = @"{""Missing"":1}";
122

  
123
 1
      JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
124
 0
    }
125

  
126
    [Test]
127
    public void MissingJson()
128
    {
129
 1
      string json = @"{}";
130

  
131
 1
      JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings
132
 1
        {
133
 1
          MissingMemberHandling = MissingMemberHandling.Error
134
 1
        });
135
 1
    }
136
  }
137
}