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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Documentation/ConditionalPropertiesTests.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
#if !(NET35 || NET20 || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using System.Dynamic;
 
31
using System.IO;
 
32
using System.Linq;
 
33
using System.Net;
 
34
using System.Reflection;
 
35
using System.Runtime.Serialization;
 
36
using System.Text;
 
37
using System.Threading.Tasks;
 
38
using Newtonsoft.Json.Converters;
 
39
using Newtonsoft.Json.Linq;
 
40
#if !NETFX_CORE
 
41
using NUnit.Framework;
 
42
#else
 
43
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
44
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
45
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
46
#endif
 
47
using Newtonsoft.Json.Serialization;
 
48
using Newtonsoft.Json.Tests.TestObjects;
 
49
using Newtonsoft.Json.Utilities;
 
50
using System.Globalization;
 
51
 
 
52
namespace Newtonsoft.Json.Tests.Documentation
 
53
{
 
54
  public class Employee
 
55
  {
 
56
    public string Name { get; set; }
 
57
    public Employee Manager { get; set; }
 
58
  }
 
59
 
 
60
  #region ShouldSerializeContractResolver
 
61
  public class ShouldSerializeContractResolver : DefaultContractResolver
 
62
  {
 
63
    public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
 
64
 
 
65
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
 
66
    {
 
67
      JsonProperty property = base.CreateProperty(member, memberSerialization);
 
68
 
 
69
      if (property.DeclaringType == typeof(Employee) && property.PropertyName == "Manager")
 
70
      {
 
71
        property.ShouldSerialize =
 
72
          instance =>
 
73
            {
 
74
              Employee e = (Employee) instance;
 
75
              return e.Manager != e;
 
76
            };
 
77
      }
 
78
 
 
79
      return property;
 
80
    }
 
81
  }
 
82
  #endregion
 
83
 
 
84
  [TestFixture]
 
85
  public class ConditionalPropertiesTests : TestFixtureBase
 
86
  {
 
87
    #region EmployeeShouldSerializeExample
 
88
    public class Employee
 
89
    {
 
90
      public string Name { get; set; }
 
91
      public Employee Manager { get; set; }
 
92
 
 
93
      public bool ShouldSerializeManager()
 
94
      {
 
95
        // don't serialize the Manager property if an employee is their own manager
 
96
        return (Manager != this);
 
97
      }
 
98
    }
 
99
    #endregion
 
100
 
 
101
    [Test]
 
102
    public void ShouldSerializeClassTest()
 
103
    {
 
104
      #region ShouldSerializeClassTest
 
105
      Employee joe = new Employee();
 
106
      joe.Name = "Joe Employee";
 
107
      Employee mike = new Employee();
 
108
      mike.Name = "Mike Manager";
 
109
 
 
110
      joe.Manager = mike;
 
111
 
 
112
      // mike is his own manager
 
113
      // ShouldSerialize will skip this property
 
114
      mike.Manager = mike;
 
115
 
 
116
      string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented);
 
117
      // [
 
118
      //   {
 
119
      //     "Name": "Joe Employee",
 
120
      //     "Manager": {
 
121
      //       "Name": "Mike Manager"
 
122
      //     }
 
123
      //   },
 
124
      //   {
 
125
      //     "Name": "Mike Manager"
 
126
      //   }
 
127
      // ]
 
128
      #endregion
 
129
 
 
130
      Assert.AreEqual(@"[
 
131
  {
 
132
    ""Name"": ""Joe Employee"",
 
133
    ""Manager"": {
 
134
      ""Name"": ""Mike Manager""
 
135
    }
 
136
  },
 
137
  {
 
138
    ""Name"": ""Mike Manager""
 
139
  }
 
140
]", json);
 
141
    }
 
142
 
 
143
    [Test]
 
144
    public void ShouldSerializeContractResolverTest()
 
145
    {
 
146
      Newtonsoft.Json.Tests.Documentation.Employee joe = new Newtonsoft.Json.Tests.Documentation.Employee();
 
147
      joe.Name = "Joe Employee";
 
148
      Newtonsoft.Json.Tests.Documentation.Employee mike = new Newtonsoft.Json.Tests.Documentation.Employee();
 
149
      mike.Name = "Mike Manager";
 
150
 
 
151
      joe.Manager = mike;
 
152
      mike.Manager = mike;
 
153
 
 
154
      string json = JsonConvert.SerializeObject(
 
155
        new[] {joe, mike},
 
156
        Formatting.Indented,
 
157
        new JsonSerializerSettings
 
158
          {
 
159
            ContractResolver = ShouldSerializeContractResolver.Instance
 
160
          });
 
161
 
 
162
      Assert.AreEqual(@"[
 
163
  {
 
164
    ""Name"": ""Joe Employee"",
 
165
    ""Manager"": {
 
166
      ""Name"": ""Mike Manager""
 
167
    }
 
168
  },
 
169
  {
 
170
    ""Name"": ""Mike Manager""
 
171
  }
 
172
]", json);
 
173
 
 
174
    }
 
175
  }
 
176
}
 
177
#endif
 
 
b'\\ No newline at end of file'