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.
26
#if !(NET35 || NET20 || PORTABLE)
28
using System.Collections.Generic;
29
using System.ComponentModel;
34
using System.Reflection;
35
using System.Runtime.Serialization;
37
using System.Threading.Tasks;
38
using Newtonsoft.Json.Converters;
39
using Newtonsoft.Json.Linq;
41
using NUnit.Framework;
43
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
44
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
45
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
47
using Newtonsoft.Json.Serialization;
48
using Newtonsoft.Json.Tests.TestObjects;
49
using Newtonsoft.Json.Utilities;
50
using System.Globalization;
52
namespace Newtonsoft.Json.Tests.Documentation
56
public string Name { get; set; }
57
public Employee Manager { get; set; }
60
#region ShouldSerializeContractResolver
61
public class ShouldSerializeContractResolver : DefaultContractResolver
63
public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
65
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
67
JsonProperty property = base.CreateProperty(member, memberSerialization);
69
if (property.DeclaringType == typeof(Employee) && property.PropertyName == "Manager")
71
property.ShouldSerialize =
74
Employee e = (Employee) instance;
75
return e.Manager != e;
85
public class ConditionalPropertiesTests : TestFixtureBase
87
#region EmployeeShouldSerializeExample
90
public string Name { get; set; }
91
public Employee Manager { get; set; }
93
public bool ShouldSerializeManager()
95
// don't serialize the Manager property if an employee is their own manager
96
return (Manager != this);
102
public void ShouldSerializeClassTest()
104
#region ShouldSerializeClassTest
105
Employee joe = new Employee();
106
joe.Name = "Joe Employee";
107
Employee mike = new Employee();
108
mike.Name = "Mike Manager";
112
// mike is his own manager
113
// ShouldSerialize will skip this property
116
string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented);
119
// "Name": "Joe Employee",
121
// "Name": "Mike Manager"
125
// "Name": "Mike Manager"
132
""Name"": ""Joe Employee"",
134
""Name"": ""Mike Manager""
138
""Name"": ""Mike Manager""
144
public void ShouldSerializeContractResolverTest()
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";
154
string json = JsonConvert.SerializeObject(
157
new JsonSerializerSettings
159
ContractResolver = ShouldSerializeContractResolver.Instance
164
""Name"": ""Joe Employee"",
166
""Name"": ""Mike Manager""
170
""Name"": ""Mike Manager""
b'\\ No newline at end of file'