This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 633
/
ConditionalSerializationTests.cs
168 lines (138 loc) · 5.15 KB
/
ConditionalSerializationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;
using System;
namespace ServiceStack.Text.Tests.JsonTests
{
[TestFixture]
public class ConditionalSerializationTests
{
[Test]
public void TestSerializeRespected()
{
var obj = new Foo { X = "abc", Z = "def" }; // don't touch Y...
string json = JsonSerializer.SerializeToString(obj);
Assert.That(json, Does.Match("{\"X\":\"abc\",\"Z\":\"def\"}"));
}
[Test]
public void TestSerializeRespectedWithInheritance()
{
var obj = new SuperFoo { X = "abc", Z = "def", A =123, C = 456 }; // don't touch Y or B...
string json = JsonSerializer.SerializeToString(obj);
Assert.That(json, Does.Match("{\"A\":123,\"C\":456,\"X\":\"abc\",\"Z\":\"def\"}"));
}
[Test]
public void TestSerializeHasAttributeNull()
{
var obj = new ByNameFoo { A = 123, B = 456 };
string json = JsonSerializer.SerializeToString(obj);
Assert.That(json, Does.Match("{\"A\":123,\"B\":456}"));
}
[Test]
public void TestSerializeHasAttributeSet()
{
var obj = new ByNameFoo { A = 123, B = 456, hasAttribute = new HashSet<string> { "A" } };
string json = JsonSerializer.SerializeToString(obj);
Assert.That(json, Does.Match("{\"A\":123"));
var cpy = JsonSerializer.DeserializeFromString<ByNameFoo>(json);
Assert.That(cpy.A, Is.EqualTo(obj.A));
Assert.That(cpy.hasAttribute, Is.EqualTo(obj.hasAttribute));
}
[Test]
public void TestSerializeHasAttributeSetNullValue()
{
var obj = new ByNameFoo { A = 123, B = null, hasAttribute = new HashSet<string> { "B" } };
string json = JsonSerializer.SerializeToString(obj);
Assert.That(json, Does.Match("{\"B\":null"));
var cpy = JsonSerializer.DeserializeFromString<ByNameFoo>(json);
Assert.That(cpy.A, Is.EqualTo(0));
Assert.That(cpy.B, Is.EqualTo(obj.B));
Assert.That(cpy.hasAttribute, Is.EqualTo(obj.hasAttribute));
}
[Test]
public void OnDeserializingMemberUnknown()
{
var cpy = JsonSerializer.DeserializeFromString<ByNameFoo>("{\"B\":1,\"C\":1");
Assert.That(cpy.A, Is.EqualTo(0));
Assert.That(cpy.B, Is.EqualTo(1));
Assert.That(cpy.hasAttribute, Is.EqualTo(new HashSet<string> { "B", "C" }));
}
public class Foo
{
public string X { get; set; } // not conditional
public string Y // conditional: never serialized
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public bool ShouldSerializeY()
{
return false;
}
public string Z { get;set;} // conditional: always serialized
public bool ShouldSerializeZ()
{
return true;
}
}
public class SuperFoo : Foo
{
public int A { get; set; } // not conditional
public int B // conditional: never serialized
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public bool ShouldSerializeB()
{
return false;
}
public int C { get; set; } // conditional: always serialized
public bool ShouldSerializeC()
{
return true;
}
}
[DataContract]
public class ByNameFoo
{
[IgnoreDataMember]
public HashSet<string> hasAttribute;
[DataMember(EmitDefaultValue = false, IsRequired = false)]
public int A { get; set; }
[DataMember(EmitDefaultValue = false, IsRequired = false)]
public int? B { get; set; }
public bool? ShouldSerialize(string fieldName)
{
if (hasAttribute == null)
{
return null;
}
return hasAttribute.Contains(fieldName);
}
public object OnDeserializing(string fieldName, object value)
{
if (hasAttribute == null)
{
hasAttribute = new HashSet<string>();
}
hasAttribute.Add(fieldName);
return value;
}
}
[DataContract]
public class HasEmitDefaultValue
{
[DataMember(EmitDefaultValue = false)]
public int DontEmitDefaultValue { get; set; }
[DataMember]
public int IntValue { get; set; }
}
[Test]
public void Does_exclude_default_property_with_EmitDefaultValue()
{
var dto = new HasEmitDefaultValue();
Assert.That(dto.ToJson(), Is.EqualTo("{\"IntValue\":0}"));
}
}
}