~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Serialization/MissingMemberHandlingTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.TestPlatform.UnitTestFramework;
 
34
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
35
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
36
#endif
 
37
 
 
38
namespace Newtonsoft.Json.Tests.Serialization
 
39
{
 
40
  [TestFixture]
 
41
  public class MissingMemberHandlingTests : TestFixtureBase
 
42
  {
 
43
    [Test]
 
44
    public void MissingMemberDeserialize()
 
45
    {
 
46
      Product product = new Product();
 
47
 
 
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" };
 
52
 
 
53
      string output = JsonConvert.SerializeObject(product, Formatting.Indented);
 
54
      //{
 
55
      //  "Name": "Apple",
 
56
      //  "ExpiryDate": new Date(1230422400000),
 
57
      //  "Price": 3.99,
 
58
      //  "Sizes": [
 
59
      //    "Small",
 
60
      //    "Medium",
 
61
      //    "Large"
 
62
      //  ]
 
63
      //}
 
64
 
 
65
      ExceptionAssert.Throws<JsonSerializationException>(
 
66
        @"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 11.",
 
67
        () =>
 
68
          {
 
69
            ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
 
70
          });
 
71
    }
 
72
 
 
73
    [Test]
 
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]
 
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
    public void MissingMemeber()
 
130
    {
 
131
      string json = @"{""Missing"":1}";
 
132
 
 
133
      ExceptionAssert.Throws<JsonSerializationException>(
 
134
        "Could not find member 'Missing' on object of type 'DoubleClass'. Path 'Missing', line 1, position 11.",
 
135
        () =>
 
136
          {
 
137
            JsonConvert.DeserializeObject<DoubleClass>(json, new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error });
 
138
          });
 
139
    }
 
140
 
 
141
    [Test]
 
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
}
 
 
b'\\ No newline at end of file'