forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructTests.cs
More file actions
318 lines (263 loc) · 9.52 KB
/
StructTests.cs
File metadata and controls
318 lines (263 loc) · 9.52 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Text.Common;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class StructTests
{
[Serializable]
public class Foo
{
public string Name { get; set; }
public Text Content1 { get; set; }
public Text Content2 { get; set; }
}
public interface IText { }
[Serializable]
public struct Text
{
private readonly string _value;
public Text(string value)
{
_value = value;
}
public static Text Parse(string value)
{
return value == null ? null : new Text(value);
}
public static implicit operator Text(string value)
{
return new Text(value);
}
public static implicit operator string(Text item)
{
return item._value;
}
public override string ToString()
{
return _value;
}
}
[TestFixtureSetUp]
public void SetUp()
{
#if IOS
JsConfig.RegisterTypeForAot<Text> ();
JsConfig.RegisterTypeForAot<Foo> ();
JsConfig.RegisterTypeForAot<PersonStatus> ();
JsConfig.RegisterTypeForAot<Person> ();
JsConfig.RegisterTypeForAot<TestDictionary> ();
JsConfig.RegisterTypeForAot<KeyValuePair<string, string>> ();
JsConfig.RegisterTypeForAot<Pair> ();
#endif
}
[TearDown]
public void TearDown()
{
JsConfig.Reset();
}
[Test]
public void Test_structs()
{
JsConfig<Text>.SerializeFn = text => text.ToString();
var dto = new Foo { Content1 = "My content", Name = "My name" };
var json = JsonSerializer.SerializeToString(dto, dto.GetType());
Assert.That(json, Is.EqualTo("{\"Name\":\"My name\",\"Content1\":\"My content\"}"));
}
[Test]
public void Test_structs_with_double_quotes()
{
var dto = new Foo { Content1 = "My \"quoted\" content", Name = "My \"quoted\" name" };
JsConfig<Text>.SerializeFn = text => text.ToString();
JsConfig<Text>.DeSerializeFn = v => new Text(v);
var json = JsonSerializer.SerializeToString(dto, dto.GetType());
Assert.That(json, Is.EqualTo("{\"Name\":\"My \\\"quoted\\\" name\",\"Content1\":\"My \\\"quoted\\\" content\"}"));
var foo = JsonSerializer.DeserializeFromString<Foo>(json);
Assert.That(foo.Name, Is.EqualTo(dto.Name));
Assert.That(foo.Content1, Is.EqualTo(dto.Content1));
}
public enum PersonStatus
{
None,
ActiveAgent,
InactiveAgent
}
public class Person
{
//A bunch of other properties
public PersonStatus Status { get; set; }
}
[Test]
public void Test_enum_overloads()
{
JsConfig<Person>.EmitCamelCaseNames = true;
JsConfig.IncludeNullValues = true;
JsConfig<PersonStatus>.SerializeFn = text => text.ToString().ToCamelCase();
var dto = new Person { Status = PersonStatus.ActiveAgent };
var json = JsonSerializer.SerializeToString(dto);
Assert.That(json, Is.EqualTo("{\"status\":\"activeAgent\"}"));
Console.WriteLine(json);
JsConfig.Reset();
}
public class TestDictionary
{
public Dictionary<string, string> Dictionary { get; set; }
public List<KeyValuePair<string, string>> KvpList { get; set; }
public IEnumerable<KeyValuePair<string, string>> KvpEnumerable { get; set; }
}
public class Pair
{
public string Key { get; set; }
public string Value { get; set; }
}
[Test]
public void Serializes_ListOfKvp_AsPocoList()
{
var map = new Dictionary<string, string> { { "foo", "bar" }, { "x", "y" } };
var dto = new TestDictionary
{
Dictionary = map,
KvpList = map.ToList(),
KvpEnumerable = map,
};
var json = dto.ToJson();
Console.WriteLine(json);
Assert.That(json, Is.EqualTo("{\"Dictionary\":{\"foo\":\"bar\",\"x\":\"y\"},"
+ "\"KvpList\":[{\"Key\":\"foo\",\"Value\":\"bar\"},{\"Key\":\"x\",\"Value\":\"y\"}],"
+ "\"KvpEnumerable\":[{\"Key\":\"foo\",\"Value\":\"bar\"},{\"Key\":\"x\",\"Value\":\"y\"}]}"));
}
[Test]
public void Should_deserialize_KeyValuePair_with_int_DateTime()
{
var t = "{\"Key\":99,\"Value\":\"\\/Date(1320098400000+0200)\\/\"}";
var b = JsonSerializer.DeserializeFromString<KeyValuePair<int, DateTime>>(t);
Assert.That(b, Is.Not.Null);
Assert.That(b.Key, Is.EqualTo(99));
Assert.That(b.Value, Is.EqualTo(new DateTime(2011, 11, 1)));
}
public class TestKeyValuePair
{
public KeyValuePair<int?, bool?> KeyValue { get; set; }
public string Name { get; set; }
}
[Test]
public void Should_deserialize_class_with_KeyValuePair_with_nullables()
{
var t = "{\"KeyValue\":{\"Value\":true},\"Name\":\"test\"}";
var b = JsonSerializer.DeserializeFromString<TestKeyValuePair>(t);
Assert.That(b.KeyValue.Key, Is.Null);
Assert.That(b.KeyValue.Value, Is.EqualTo(true));
Assert.That(b.Name, Is.EqualTo("test"));
}
[Test]
public void Can_TreatValueAsRefType()
{
JsConfig<UserStruct>.TreatValueAsRefType = true;
var dto = new UserStruct { Id = 1, Name = "foo" };
Assert.That(dto.ToJson(),
Is.EqualTo("{\"Id\":1,\"Name\":\"foo\"}"));
Assert.That(dto.ToJsv(),
Is.EqualTo("{Id:1,Name:foo}"));
#if !XBOX && !SL5 && !IOS
Assert.That(dto.ToXml(),
Is.EqualTo("<?xml version=\"1.0\" encoding=\"utf-8\"?><UserStruct xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/ServiceStack.Text.Tests\"><Id>1</Id><Name>foo</Name></UserStruct>"));
#endif
JsConfig.Reset();
}
[Serializable]
protected struct DangerousText1
{
public static object Parse(string text)
{
return new DangerousText1();
}
}
[Serializable]
protected struct DangerousText2
{
public static int Parse(string text)
{
return 42;
}
}
[Test]
public void StaticParseMethod_will_not_throw_on_standard_usage()
{
ParseStringDelegate ret = null;
Assert.DoesNotThrow(() => ret = StaticParseMethod<Text>.Parse);
Assert.IsNotNull(ret);
}
[Test]
public void StaticParseMethod_will_not_throw_on_old_usage()
{
ParseStringDelegate ret = null;
Assert.DoesNotThrow(() => ret = StaticParseMethod<DangerousText1>.Parse);
Assert.IsNotNull(ret);
}
[Test]
public void StaticParseMethod_will_not_throw_on_unstandard_usage()
{
ParseStringDelegate ret = null;
Assert.DoesNotThrow(() => ret = StaticParseMethod<DangerousText2>.Parse);
Assert.IsNull(ret);
}
[Explicit("Ensure this test has proven to work, before adding it to the test suite")]
[Test]
[TestCase("en")]
[TestCase("en-US")]
[TestCase("de-CH")]
[TestCase("de")]
public void test_rect_different_cultures(string culture)
{
var currentCulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentCulture;
var s = new JsonSerializer<Rect>();
var r = new Rect(23, 34, 1024, 768);
var interim = s.SerializeToString(r);
var r2 = s.DeserializeFromString(interim);
Assert.AreEqual(r, r2);
}
}
public struct UserStruct
{
public int Id { get; set; }
public string Name { get; set; }
}
public struct Rect : IFormattable
{
double x;
double y;
double width;
double height;
public Rect(double x, double y, double width, double height)
{
if (width < 0 || height < 0)
throw new ArgumentException("width and height must be non-negative.");
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public string ToString(string format, IFormatProvider formatProvider)
{
return "{0},{1},{2},{3}".Fmt(x, y, width, height);
}
public static Rect Parse(string input)
{
var parts = input.Split(',');
return new Rect(
double.Parse(parts[0]),
double.Parse(parts[1]),
double.Parse(parts[2]),
double.Parse(parts[3])
);
}
}
}