This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathReportedIssues.cs
More file actions
319 lines (275 loc) · 9.44 KB
/
Copy pathReportedIssues.cs
File metadata and controls
319 lines (275 loc) · 9.44 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
#if !MONOTOUCH
using ServiceStack.ServiceInterface.ServiceModel;
#endif
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class ReportedIssues
: TestBase
{
[Test]
public void Issue5_Can_serialize_Dictionary_with_null_value()
{
var map = new Dictionary<string, string> {
{"p1","v1"},{"p2","v2"},{"p3",null},
};
Serialize(map);
}
public abstract class CorrelativeDataBase
{
protected CorrelativeDataBase()
{
CorrelationIdentifier = GetNextId();
}
public Guid CorrelationIdentifier { get; set; }
protected static Guid GetNextId()
{
return Guid.NewGuid();
}
}
public sealed class TestObject : CorrelativeDataBase
{
public Type SomeType { get; set; }
public string SomeString { get; set; }
public IEnumerable<Type> SomeTypeList { get; set; }
public IEnumerable<Type> SomeTypeList2 { get; set; }
public IEnumerable<object> SomeObjectList { get; set; }
}
[Test]
public void Serialize_object_with_type_field()
{
var obj = new TestObject
{
SomeType = typeof(string),
SomeString = "Test",
SomeObjectList = new object[0]
};
Serialize(obj, includeXml: false); // xml cannot serialize Type objects.
}
[Test]
public void Serialize_object_with_type_field2()
{
var obj = new TestObject
{
SomeType = typeof(string),
SomeString = "Test",
SomeObjectList = new object[0]
};
var strModel = TypeSerializer.SerializeToString<object>(obj);
Console.WriteLine("Len: " + strModel.Length + ", " + strModel);
var toModel = TypeSerializer.DeserializeFromString<TestObject>(strModel);
}
public class Article
{
public string title { get; set; }
public string url { get; set; }
public string author { get; set; }
public string author_id { get; set; }
public string date { get; set; }
public string type { get; set; }
}
[Test]
public void Serialize_Dictionary_with_backslash_as_last_char()
{
var map = new Dictionary<string, Article>
{
{
"http://www.eurogamer.net/articles/2010-09-14-vanquish-limited-edition-has-7-figurine",
new Article
{
title = "Vanquish Limited Edition has 7\" figurine",
url = "articles/2010-09-14-vanquish-limited-edition-has-7-figurine",
author = "Wesley Yin-Poole",
author_id = "621",
date = "14/09/2010",
type = "news",
}
},
{
"http://www.eurogamer.net/articles/2010-09-14-supercar-challenge-devs-next-detailed",
new Article
{
title = "SuperCar Challenge dev's next detailed",
url = "articles/2010-09-14-supercar-challenge-devs-next-detailed",
author = "Wesley Yin-Poole",
author_id = "621",
date = "14/09/2010",
type = "news",
}
},
{
"http://www.eurogamer.net/articles/2010-09-14-hmv-to-sell-dead-rising-2-a-day-early",
new Article
{
title = "HMV to sell Dead Rising 2 a day early",
url = "articles/2010-09-14-hmv-to-sell-dead-rising-2-a-day-early",
author = "Wesley Yin-Poole",
author_id = "621",
date = "14/09/2010",
type = "News",
}
},
};
Serialize(map);
var json = JsonSerializer.SerializeToString(map);
var fromJson = JsonSerializer.DeserializeFromString<Dictionary<string, Article>>(json);
Assert.That(fromJson, Has.Count.EqualTo(map.Count));
}
public class Item
{
public int type { get; set; }
public int color { get; set; }
}
public class Basket
{
public Basket()
{
Items = new Dictionary<Item, int>();
}
public Dictionary<Item, int> Items { get; set; }
}
[Test]
public void Can_Serialize_Class_with_Typed_Dictionary()
{
var basket = new Basket();
basket.Items.Add(new Item { type = 1, color = 2 }, 10);
basket.Items.Add(new Item { type = 4, color = 1 }, 20);
Serialize(basket);
}
public class Book
{
public UInt64 Id { get; set; }
public UInt64 OwnerUserId { get; set; }
public String Title { get; set; }
public String Description { get; set; }
public UInt16 CategoryId { get; set; }
public DateTime CreatedDateTime { get; set; }
public Byte Icon { get; set; }
public Boolean Canceled { get; set; }
}
#if !MONOTOUCH
public class BookResponse : IHasResponseStatus
{
public Book Book { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public object GetBook()
{
var response = new BookResponse();
return response;
}
[Test]
public void Does_not_serialize_typeinfo_for_concrete_types()
{
var json = GetBook().ToJson();
Console.WriteLine(json);
Assert.That(json.IndexOf("__"), Is.EqualTo(-1));
var jsv = GetBook().ToJsv();
Assert.That(jsv.IndexOf("__"), Is.EqualTo(-1));
}
#endif
public class TextTags
{
public string Text { get; set; }
public string[] Tags { get; set; }
}
[Test]
public void Can_serialize_sweedish_chars()
{
var dto = new TextTags { Text = "Olle är en ÖL ål", Tags = new[] { "öl", "ål", "mål" } };
Serialize(dto);
}
[Test]
public void Objects_Do_Not_Survive_RoundTrips_Via_StringStringDictionary_Due_To_DoubleQuoted_Properties()
{
var book = new Book();
book.Id = 1234;
book.Title = "ServiceStack in Action";
book.CategoryId = 16;
book.Description = "Manning eBooks";
var json = book.ToJson();
Console.WriteLine("Book to Json: " + json);
var dictionary = json.FromJson<Dictionary<string, string>>();
Console.WriteLine("Json to Dictionary: " + dictionary.Dump());
var fromDictionary = dictionary.ToJson();
Console.WriteLine("Json from Dictionary: " + fromDictionary);
var fromJsonViaDictionary = fromDictionary.FromJson<Book>();
Assert.AreEqual(book.Description, fromJsonViaDictionary.Description);
Assert.AreEqual(book.Id, fromJsonViaDictionary.Id);
Assert.AreEqual(book.Title, fromJsonViaDictionary.Title);
Assert.AreEqual(book.CategoryId, fromJsonViaDictionary.CategoryId);
}
public class Test
{
public IDictionary<string, string> Items { get; set; }
public string TestString { get; set; }
}
[Test]
public void Does_Trailing_Backslashes()
{
var test = new Test {
TestString = "Test",
Items = new Dictionary<string, string> { { "foo", "bar\\" } }
};
var serialized = JsonSerializer.SerializeToString(test);
Console.WriteLine(serialized);
var deserialized = JsonSerializer.DeserializeFromString<Test>(serialized);
Assert.That(deserialized.TestString, Is.EqualTo("Test")); // deserialized.TestString is NULL
}
[Test]
public void Deserialize_Correctly_When_Last_Item_Is_Null_in_array()
{
var arrayOfInt = new int?[2] {1, null };
var serialized = TypeSerializer.SerializeToString(arrayOfInt);
Console.WriteLine(serialized);
var deserialized = TypeSerializer.DeserializeFromString<int?[]>(serialized);
Assert.That(deserialized, Is.EqualTo(arrayOfInt));
}
[Test]
public void Deserialize_Correctly_When_Last_Item_Is_Null_in_list()
{
var arrayOfInt = new List<int?> { 1, null };
var serialized = TypeSerializer.SerializeToString(arrayOfInt);
Console.WriteLine(serialized);
var deserialized = TypeSerializer.DeserializeFromString<List<int?>>(serialized);
Assert.That(deserialized, Is.EqualTo(arrayOfInt));
}
[Test]
public void Deserialize_Correctly_When_Last_Item_Is_Null_in_String_list_prop()
{
var type = new TestMappedList() { StringListProp = new List<String> { "hello", null } };
var serialized = TypeSerializer.SerializeToString(type);
Console.WriteLine(serialized);
var deserialized = TypeSerializer.DeserializeFromString<TestMappedList>(serialized);
Assert.That(deserialized.StringListProp, Is.EqualTo(type.StringListProp));
}
[Test]
public void Deserialize_Correctly_When_Last_Item_Is_Null_in_String_array_prop()
{
var type = new TestMappedList() { StringArrayProp = new string [] { "hello", null } };
var serialized = TypeSerializer.SerializeToString(type);
Console.WriteLine(serialized);
var deserialized = TypeSerializer.DeserializeFromString<TestMappedList>(serialized);
Assert.That(deserialized.StringArrayProp, Is.EqualTo(type.StringArrayProp));
}
[Test]
public void Deserialize_Correctly_When_Last_Item_Is_Null_in_Int_array_prop()
{
var type = new TestMappedList() { IntArrayProp = new List<int?> { 1, null } };
var serialized = TypeSerializer.SerializeToString(type);
Console.WriteLine(serialized);
var deserialized = TypeSerializer.DeserializeFromString<TestMappedList>(serialized);
Assert.That(deserialized.IntArrayProp, Is.EqualTo(type.IntArrayProp));
}
}
public class TestMappedList
{
public List<String> StringListProp { get; set; }
public string[] StringArrayProp { get; set; }
public List<int?> IntArrayProp { get; set; }
}
}