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