forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalSerializationTests.cs
More file actions
151 lines (125 loc) · 4.71 KB
/
ConditionalSerializationTests.cs
File metadata and controls
151 lines (125 loc) · 4.71 KB
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
using System.Reflection;
using System.Runtime.Serialization;
using NUnit.Framework;
using System;
using ServiceStack.Text.WP;
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, Is.StringMatching("{\"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, Is.StringMatching("{\"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, Is.StringMatching("{\"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, Is.StringMatching("{\"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, Is.StringMatching("{\"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;
}
}
}
}