forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractTests.cs
More file actions
304 lines (249 loc) · 8.82 KB
/
DataContractTests.cs
File metadata and controls
304 lines (249 loc) · 8.82 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;
using ServiceStack.Text.Tests.Support;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class DataContractTests
: TestBase
{
[Test]
public void Only_Serializes_DataMember_fields_for_DataContracts()
{
var dto = new ResponseStatus {
ErrorCode = "ErrorCode",
Message = "Message",
StackTrace = "StackTrace",
Errors = new List<ResponseError>(),
};
Serialize(dto);
}
public class RequestWithIgnoredMembers
{
public string Name { get; set; }
[IgnoreDataMember]
public string Comment { get; set; }
}
private void DoIgnoreMemberTest(Func<RequestWithIgnoredMembers, string> serialize,
Func<string, RequestWithIgnoredMembers> deserialize)
{
var dto = new RequestWithIgnoredMembers() {
Name = "John",
Comment = "Some Comment"
};
var clone = deserialize(serialize(dto));
Assert.AreEqual(dto.Name, clone.Name);
Assert.IsNull(clone.Comment);
}
[Test]
public void JsonSerializerHonorsIgnoreMemberAttribute()
{
DoIgnoreMemberTest(r => JsonSerializer.SerializeToString(r),
s => JsonSerializer.DeserializeFromString<RequestWithIgnoredMembers>(s));
}
[Test]
public void JsvSerializerHonorsIgnoreMemberAttribute()
{
DoIgnoreMemberTest(r => TypeSerializer.SerializeToString(r),
s => TypeSerializer.DeserializeFromString<RequestWithIgnoredMembers>(s));
}
[Test]
public void XmlSerializerHonorsIgnoreMemberAttribute()
{
DoIgnoreMemberTest(r => XmlSerializer.SerializeToString(r),
s => XmlSerializer.DeserializeFromString<RequestWithIgnoredMembers>(s));
}
[DataContract]
public class EmptyDataContract { }
[Test]
public void Can_Serialize_Empty_DataContract()
{
var dto = new EmptyDataContract();
Serialize(dto);
}
[CollectionDataContract]
public class MyCollection : ICollection<MyType>
{
private List<MyType> _internal = new List<MyType> { new MyType() };
public IEnumerator<MyType> GetEnumerator()
{
return _internal.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _internal.GetEnumerator();
}
public void Add(MyType item)
{
_internal.Add(item);
}
public void Clear()
{
_internal.Clear();
}
public bool Contains(MyType item)
{
return _internal.Contains(item);
}
public void CopyTo(MyType[] array, int arrayIndex)
{
_internal.CopyTo(array, arrayIndex);
}
public bool Remove(MyType item)
{
return _internal.Remove(item);
}
public int Count
{
get { return _internal.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
}
[DataContract]
public class MyType { }
[Test]
public void Can_Serialize_MyCollection()
{
var dto = new MyCollection();
Serialize(dto);
}
[DataContract]
public class PersonRecord
{
public int Id { get; set; }
public string Name { get; set; }
}
[Test] //https://github.com/ServiceStack/ServiceStack.Text/issues/46
public void Replicate_serialization_bug()
{
var p = new PersonRecord { Id = 27, Name = "John" };
// Fails at this point, with a "Cannot access a closed Stream." exception.
// Am I doing something wrong?
string output = XmlSerializer.SerializeToString(p);
Console.WriteLine(output);
}
[DataContract]
public class ClassOne
{
[DataMember]
public int Id { get; set; }
[DataMember(Name = "listClassTwo", Order = 1)]
public List<ClassTwo> List { get; set; }
public ClassOne()
{
List = new List<ClassTwo>();
}
}
[DataContract]
public class ClassTwo
{
[DataMember(Name = "NewName")]
public string Name { get; set; }
}
[DataContract]
public class ClassThree
{
[DataMember(Name = "some-title")]
public string Title { get; set; }
}
[DataContract]
public class ClassFour
{
[DataMember(Name = "some-title")]
public string Title;
}
[DataContract]
public class ClassFive
{
[DataMember(Name = "some-bytes")]
public byte[] Bytes;
}
[Test]
public void Csv_Serialize_Should_Respects_DataContract_Name()
{
var classTwo = new ClassTwo {
Name = "Value"
};
Assert.That(CsvSerializer.SerializeToString(classTwo),
Is.EqualTo(String.Format("NewName\r\nValue\r\n")));
}
[Test]
public void deserialize_from_string_with_the_dataMember_name()
{
const string jsonList =
"{\"Id\":1,\"listClassTwo\":[{\"NewName\":\"Name One\"},{\"NewName\":\"Name Two\"}]}";
var classOne = JsonSerializer.DeserializeFromString<ClassOne>(jsonList);
Assert.AreEqual(1, classOne.Id);
Assert.AreEqual(2, classOne.List.Count);
}
[Test]
public void Json_Serialize_Should_Respects_DataContract_Name_When_Deserialize()
{
var t = JsonSerializer.DeserializeFromString<ClassThree>("{\"some-title\": \"right\", \"Title\": \"wrong\"}");
Assert.That(t.Title, Is.EqualTo("right"));
}
[Test]
public void Json_Serialize_Should_Respects_DataContract_Field_Name_When_Deserialize()
{
var t = JsonSerializer.DeserializeFromString<ClassFour>("{\"some-title\": \"right\", \"Title\": \"wrong\"}");
Assert.That(t.Title, Is.EqualTo("right"));
}
[Test]
public void Json_Serialize_Should_Respects_DataContract_Name()
{
var classOne = new ClassOne {
Id = 1,
List =
new List<ClassTwo> { new ClassTwo { Name = "Name One" }, new ClassTwo { Name = "Name Two" } }
};
Assert.That(JsonSerializer.SerializeToString(classOne),
Is.EqualTo("{\"Id\":1,\"listClassTwo\":[{\"NewName\":\"Name One\"},{\"NewName\":\"Name Two\"}]}"));
}
[Test]
public void Json_Serialize_Should_Respects_DataContract_Field_Name_With_Bytes_Deserialize()
{
var t = JsonSerializer.DeserializeFromString<ClassFive>("{\"some-bytes\": \"YWI=\"}");
Assert.IsTrue (t.Bytes.AreEqual (new byte[] { 0x61, 0x62 }));
}
#if !NETCORE_SUPPORT
[Test]
public void Can_get_weak_DataMember()
{
var dto = new ClassOne();
var dataMemberAttr = dto.GetType().GetProperty("Id").GetWeakDataMember();
Assert.That(dataMemberAttr.Name, Is.Null);
dataMemberAttr = dto.GetType().GetProperty("List").GetWeakDataMember();
Assert.That(dataMemberAttr.Name, Is.EqualTo("listClassTwo"));
Assert.That(dataMemberAttr.Order, Is.EqualTo(1));
}
#endif
[DataContract(Name = "my-class", Namespace = "http://schemas.ns.com")]
public class MyClass
{
[DataMember(Name = "some-title")]
public string Title { get; set; }
}
#if !NETCORE_SUPPORT
[Test]
public void Can_get_weak_DataContract()
{
var mc = new MyClass { Title = "Some random title" };
var attr = mc.GetType().GetWeakDataContract();
Assert.That(attr.Name, Is.EqualTo("my-class"));
Assert.That(attr.Namespace, Is.EqualTo("http://schemas.ns.com"));
}
#endif
[Test]
public void Does_use_DataMember_Name()
{
var mc = new MyClass { Title = "Some random title" };
Assert.That(mc.ToJson(), Is.EqualTo("{\"some-title\":\"Some random title\"}"));
}
}
}