forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexObjectGraphTest.cs
More file actions
227 lines (191 loc) · 9.76 KB
/
ComplexObjectGraphTest.cs
File metadata and controls
227 lines (191 loc) · 9.76 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using NUnit.Framework;
using ServiceStack.Text.Tests.DynamicModels.DataModel;
namespace ServiceStack.Text.Tests.DynamicModels
{
[TestFixture]
public class ComplexObjectGraphTest
{
[Test]
public void ShouldSerializeCustomCollection()
{
var orig = new CustomCollection
{
AddressUri = new Uri("http://www.example.com/"),
IntValue = 123,
SomeType = typeof(CustomCollection)
};
var s = TypeSerializer.SerializeToString(orig);
//FYI TypeSerializer is recognized as an IList<CustomCollectionItem>
//Where CustomCollectionItem has a property of 'object Value' where it
//is unable to work out the original types so there deserialized back out as strings
var clone = TypeSerializer.DeserializeFromString<CustomCollection>(s);
Assert.That(clone, Is.Not.Null);
Assert.That(clone, Has.All.No.Null);
Assert.That(clone.Count, Is.EqualTo(orig.Count));
Assert.That(clone.AddressUri, Is.EqualTo(orig.AddressUri));
Assert.That(clone.IntValue, Is.EqualTo(orig.IntValue));
Assert.That(clone.SomeType, Is.EqualTo(orig.SomeType));
//Collections are not same, one has object values, the other has string values as explained above
//Assert.That(clone, Is.EquivalentTo(orig));
}
[Test]
public void ShouldSerializeCustomCollectionDto()
{
var orig = new CustomCollectionDto
{
//Only saves the Message, i.e. not InnerEx, StackTrace etc.
Exception = new Exception("Exception Test"),
CustomException = new CustomException("CustomException Test"),
AddressUri = new Uri("http://www.example.com/"),
IntValue = 123,
SomeType = typeof(CustomCollection),
};
var s = TypeSerializer.SerializeToString(orig);
var clone = TypeSerializer.DeserializeFromString<CustomCollectionDto>(s);
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Exception.Message, Is.EqualTo(orig.Exception.Message));
Assert.That(clone.Exception.GetType(), Is.EqualTo(orig.Exception.GetType()));
Assert.That(clone.CustomException.Message, Is.EqualTo(orig.CustomException.Message));
Assert.That(clone.CustomException.GetType(), Is.EqualTo(orig.CustomException.GetType()));
Assert.That(clone.AddressUri, Is.EqualTo(orig.AddressUri));
Assert.That(clone.IntValue, Is.EqualTo(orig.IntValue));
Assert.That(clone.SomeType, Is.EqualTo(orig.SomeType));
}
[Test]
public void ShouldSerializeCustomCollectionItem()
{
var orig = new CustomCollectionItem("Test", "Some Value");
var s = TypeSerializer.SerializeToString(orig);
var clone = TypeSerializer.DeserializeFromString<CustomCollectionItem>(s);
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Name, Is.EqualTo(orig.Name));
Assert.That(clone.Value, Is.EqualTo(orig.Value));
}
[Test]
public void ShouldSerializeDataContainer()
{
var orig = new DataContainer
{
Exception = new Exception("Test"),
Identifier = Guid.NewGuid(),
Object = "Test", //Can't deserialize 'new object()' as an empty object has no serialized form
Type = this.GetType(),
TypeList = new List<Type> { typeof(string), this.GetType(), typeof(Int32) },
//Can't serialize a list of objects, will have no idea what to deserialize it to
//ObjectList = new List<object> { typeof(string), new Exception("Another Test"), "Teststring" },
};
var s = TypeSerializer.SerializeToString(orig);
Console.WriteLine(s);
var clone = TypeSerializer.DeserializeFromString<DataContainer>(s);
Assert.That(clone, Is.Not.Null);
Assert.That(clone.Exception.Message, Is.EqualTo(orig.Exception.Message));
Assert.That(clone.Exception.GetType(), Is.EqualTo(orig.Exception.GetType()));
Assert.That(clone.Identifier, Is.EqualTo(orig.Identifier));
Assert.That(clone.Object, Is.EqualTo(orig.Object));
//Assert.That(clone.ObjectList, Has.All.Not.Null);
//Assert.That(clone.ObjectList, Is.EquivalentTo(orig.ObjectList));
Assert.That(clone.Type, Is.EqualTo(orig.Type));
Assert.That(clone.TypeList, Has.All.Not.Null);
Assert.That(clone.TypeList, Is.EquivalentTo(orig.TypeList));
}
[Test]
public void ShouldSerializeObjectGraph()
{
var dc = new DataContainer
{
Exception = new Exception("Test"),
Identifier = Guid.NewGuid(),
Object = "Test Object",
//ObjectList = new List<object> { typeof(string), new Exception("Another Test"), "Teststring" },
Type = this.GetType(),
TypeList = new List<Type> { typeof(string), this.GetType(), typeof(Int32) }
};
var orig = new ObjectGraph
{
AddressUri = new Uri("http://www.example.com/"),
IntValue = 123,
SomeType = typeof(CustomCollection),
Data = dc
};
var s = TypeSerializer.SerializeToString(orig);
var clone = TypeSerializer.DeserializeFromString<ObjectGraph>(s);
Assert.That(clone, Is.Not.Null);
Assert.That(clone.MyCollection, Is.Not.Null);
Assert.That(clone.MyCollection, Has.All.Not.Null);
//Collections are not same, one has object values, the other has string values
//Assert.That(clone.MyCollection, Is.EquivalentTo(orig.MyCollection));
Assert.That(clone.Data, Is.Not.Null);
//Can't do ref comparisons, they are not the same
//Assert.That(clone.Data, Is.EqualTo(orig.Data));
Assert.That(clone.AddressUri, Is.EqualTo(orig.AddressUri));
Assert.That(clone.IntValue, Is.EqualTo(orig.IntValue));
Assert.That(clone.SomeType, Is.EqualTo(orig.SomeType));
}
[Test]
public void Can_JSV_Serialize_DynamicType_and_Deserialize_into_StrictType()
{
var orig = new DynamicType
{
Name = "Dynamic Type",
Type = typeof(CustomCollectionDto),
Value = new CustomCollectionDto
{
AddressUri = new Uri("http://www.example.com/"),
IntValue = 123,
SomeType = typeof(int)
}
};
var jsv = TypeSerializer.SerializeToString(orig);
var strictType = TypeSerializer.DeserializeFromString<StrictType>(jsv);
var dynamicType = TypeSerializer.DeserializeFromString<DynamicType>(jsv);
var origDto = (CustomCollectionDto)orig.Value;
Assert.That(strictType, Is.Not.Null);
Assert.That(strictType.Name, Is.EqualTo(orig.Name));
Assert.That(strictType.Type, Is.EqualTo(orig.Type));
Assert.That(strictType.Value.AddressUri, Is.EqualTo(origDto.AddressUri));
Assert.That(strictType.Value.IntValue, Is.EqualTo(origDto.IntValue));
Assert.That(strictType.Value.SomeType, Is.EqualTo(origDto.SomeType));
Assert.That(dynamicType, Is.Not.Null);
Assert.That(dynamicType.Name, Is.EqualTo(orig.Name));
Assert.That(dynamicType.Type, Is.EqualTo(orig.Type));
var dynamicValue = (CustomCollectionDto)dynamicType.GetTypedValue();
Assert.That(dynamicValue.AddressUri, Is.EqualTo(origDto.AddressUri));
Assert.That(dynamicValue.IntValue, Is.EqualTo(origDto.IntValue));
Assert.That(dynamicValue.SomeType, Is.EqualTo(origDto.SomeType));
}
[Test]
public void Can_JSON_Serialize_DynamicType_and_Deserialize_into_StrictType()
{
var orig = new DynamicType
{
Name = "Dynamic Type",
Type = typeof(CustomCollectionDto),
Value = new CustomCollectionDto
{
AddressUri = new Uri("http://www.example.com/"),
IntValue = 123,
SomeType = typeof(int)
}
};
var jsv = JsonSerializer.SerializeToString(orig);
var strictType = JsonSerializer.DeserializeFromString<StrictType>(jsv);
var dynamicType = JsonSerializer.DeserializeFromString<DynamicType>(jsv);
var origDto = (CustomCollectionDto)orig.Value;
Assert.That(strictType, Is.Not.Null);
Assert.That(strictType.Name, Is.EqualTo(orig.Name));
Assert.That(strictType.Type, Is.EqualTo(orig.Type));
Assert.That(strictType.Value.AddressUri, Is.EqualTo(origDto.AddressUri));
Assert.That(strictType.Value.IntValue, Is.EqualTo(origDto.IntValue));
Assert.That(strictType.Value.SomeType, Is.EqualTo(origDto.SomeType));
Assert.That(dynamicType, Is.Not.Null);
Assert.That(dynamicType.Name, Is.EqualTo(orig.Name));
Assert.That(dynamicType.Type, Is.EqualTo(orig.Type));
var dynamicValue = (CustomCollectionDto)dynamicType.GetTypedValue();
Assert.That(dynamicValue.AddressUri, Is.EqualTo(origDto.AddressUri));
Assert.That(dynamicValue.IntValue, Is.EqualTo(origDto.IntValue));
Assert.That(dynamicValue.SomeType, Is.EqualTo(origDto.SomeType));
}
}
}