forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandoTests.cs
More file actions
185 lines (154 loc) · 5.17 KB
/
ExpandoTests.cs
File metadata and controls
185 lines (154 loc) · 5.17 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
using System;
using System.Collections.Generic;
using System.Dynamic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class ExpandoTests : TestBase
{
[Test]
public void Can_serialize_one_level_expando()
{
dynamic map = new ExpandoObject();
map.One = 1;
map.Two = 2;
map.Three = 3;
Serialize(map);
}
[Test]
public void Can_serialize_empty_map()
{
var emptyMap = new ExpandoObject();
Serialize(emptyMap);
}
[Test]
public void Can_serialize_two_level_expando()
{
dynamic map1 = new ExpandoObject();
map1.One = 1;
map1.Two = 2;
map1.Three = 3;
dynamic map2 = new ExpandoObject();
map2.Four = 4;
map2.Five = 5;
map2.Six = 6;
dynamic map = new ExpandoObject();
map.map1 = map1;
map.map2 = map2;
Serialize(map);
}
private static ExpandoObject SetupMap()
{
dynamic map = new ExpandoObject();
map.a = "text";
map.b = 32;
map.c = false;
map.d = new[] { 1, 2, 3 };
return map;
}
public class MixType
{
public string a { get; set; }
public int b { get; set; }
public bool c { get; set; }
public int[] d { get; set; }
}
private static void AssertMap(dynamic map)
{
Assert.AreEqual("text", map.a);
Assert.AreEqual(32, map.b);
Assert.AreEqual(false, map.c);
Assert.AreEqual(new List<int> { 1, 2, 3 }, map.d);
}
[Test]
public void Can_deserialize_mixed_expando_into_strongtyped_map()
{
var mixedMap = SetupMap();
var json = JsonSerializer.SerializeToString(mixedMap);
Console.WriteLine("JSON:\n" + json);
var mixedType = json.FromJson<MixType>();
Assert.AreEqual("text", mixedType.a);
Assert.AreEqual(32, mixedType.b);
Assert.AreEqual(false, mixedType.c);
}
[Test]
public void Can_deserialize_two_level_expando()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
var json = "{\"a\":\"text\",\"b\":32,\"Map\":{\"a\":\"text\",\"b\":32,\"c\":false,\"d\":[1,2,3]}}";
dynamic expandoObject = TypeSerializer.DeserializeFromString<ExpandoObject>(json);
Assert.AreEqual("text", expandoObject.a);
Assert.AreEqual(32, expandoObject.b);
AssertMap((ExpandoObject)expandoObject.Map);
JsConfig.TryToParsePrimitiveTypeValues = false;
}
[Test]
public void Can_serialise_null_values_from_expando_correctly()
{
JsConfig.IncludeNullValues = true;
dynamic expando = new ExpandoObject();
expando.value = null;
Serialize(expando, includeXml: false);
var json = JsonSerializer.SerializeToString(expando);
Log(json);
Assert.That(json, Is.EqualTo("{\"value\":null}"));
JsConfig.Reset();
}
[Test]
public void Will_ignore_null_values_from_expando_correctly()
{
JsConfig.IncludeNullValues = false;
dynamic expando = new ExpandoObject();
expando.value = null;
Serialize(expando, includeXml: false);
var json = JsonSerializer.SerializeToString(expando);
Log(json);
Assert.That(json, Is.EqualTo("{}"));
JsConfig.Reset();
}
public class FooSlash
{
public ExpandoObject Nested { get; set; }
public string Bar { get; set; }
}
[Test]
public void Can_serialize_ExpandoObject_with_end_slash()
{
dynamic nested = new ExpandoObject();
nested.key = "value\"";
var foo = new FooSlash
{
Nested = nested,
Bar = "BarValue"
};
Serialize(foo);
}
[Test]
public void Can_serialise_null_values_from_nested_expando_correctly()
{
JsConfig.IncludeNullValues = true;
var foo = new FooSlash();
var json = JsonSerializer.SerializeToString(foo);
Assert.That(json, Is.EqualTo("{\"Nested\":null,\"Bar\":null}"));
JsConfig.Reset();
}
[Test]
public void Can_serialize_ExpandoObject_with_quotes()
{
dynamic dto = new ExpandoObject();
dto.title = "\"test\"";
dynamic to = Serialize(dto);
Assert.That(to.title, Is.EqualTo(dto.title));
}
[Test]
public void Can_Deserialize_Object_To_ExpandoObject()
{
JsConfig.TryToParsePrimitiveTypeValues = true;
const string json = "{\"Id\":1}";
dynamic d = json.To<ExpandoObject>();
Assert.That(d.Id, Is.Not.Null);
Assert.That(d.Id, Is.EqualTo(1));
}
}
}