This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
/
StructTests.cs
169 lines (132 loc) · 4.02 KB
/
StructTests.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
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 MONOTOUCH
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.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\"}]}"));
}
}
}