forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonArrayObjectTests.cs
More file actions
164 lines (142 loc) · 4.61 KB
/
Copy pathJsonArrayObjectTests.cs
File metadata and controls
164 lines (142 loc) · 4.61 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
using System.Collections.Generic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.JsonTests
{
[TestFixture]
public class JsonArrayObjectTests
{
[Test]
public void Can_serialize_int_array()
{
var array = new [] {1,2};
Assert.That(JsonSerializer.SerializeToString(array), Is.EqualTo("[1,2]"));
}
[Test]
public void Can_parse_empty_array()
{
Assert.That(JsonArrayObjects.Parse("[]"), Is.Empty);
}
[Test]
public void Can_parse_empty_array_with_tab()
{
Assert.That(JsonArrayObjects.Parse("[\t]"), Is.Empty);
}
[Test]
public void Can_parse_array_with_null()
{
Assert.That(JsonArrayObjects.Parse("[null]"), Is.EqualTo(new string[]{null}));
}
[Test]
public void Can_parse_array_with_nulls()
{
Assert.That(JsonArrayObjects.Parse("[null,null]"), Is.EqualTo(new string[]{null, null}));
}
[Test]
public void Can_parse_empty_array_with_whitespaces()
{
Assert.That(JsonArrayObjects.Parse("[ ]"), Is.Empty);
Assert.That(JsonArrayObjects.Parse("[\n\n]"), Is.Empty);
Assert.That(JsonArrayObjects.Parse("[\t\t]"), Is.Empty);
}
[Test]
public void Can_parse_empty_array_with_mixed_whitespaces()
{
Assert.That(JsonArrayObjects.Parse("[ \n\t \n\r]"), Is.Empty);
}
public class NamesTest
{
public NamesTest(List<string> names)
{
Names = names;
}
public List<string> Names { get; set; }
}
[Test]
public void Can_parse_empty_array_in_dto_with_tab()
{
var prettyJson = "{\"Names\":[\t]}";
var oPretty = prettyJson.FromJson<NamesTest>();
Assert.That(oPretty.Names.Count, Is.EqualTo(0));
}
public class CustomAuth
{
public string City { get; set; }
public int Country { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ScreenName { get; set; }
public long Uid { get; set; }
}
public class CustomAuthResponse
{
public List<CustomAuth> Response { get; set; }
}
[Test]
public void Can_parse_custom_AuthResponse()
{
var json = @"{
""response"" : [
{ ""city"" : 56,
""country"" : 1,
""first_name"" : ""Rouslan"",
""last_name"" : ""Grabar"",
""screen_name"" : ""iamruss2"",
""uid"" : 180423804
}
]
}";
var dto = JsonObject.Parse(json)
.ArrayObjects("response")[0].ConvertTo(x =>
new CustomAuth
{
City = x["city"],
Country = x.Get<int>("country"),
FirstName = x["first_name"],
LastName = x["last_name"],
ScreenName = x["screen_name"],
Uid = x.Get<long>("uid"),
});
dto.PrintDump();
using (JsConfig.With(
emitLowercaseUnderscoreNames: true,
propertyConvention: PropertyConvention.Lenient))
{
var response = json.FromJson<CustomAuthResponse>();
response.PrintDump();
}
}
class Test
{
public string Value { get; set; }
}
[Test]
public void Can_deserialize_empty_array_with_whitespace()
{
const string data = "[]\n";
var result = data.FromJson<Test[]>();
Assert.That(result.Length, Is.EqualTo(0));
}
public class MyClass
{
public string Item { get; set; }
}
[Test]
public void Can_parse_array_with_null_objects_starting_with_not_null_item()
{
var compactJson = @"{""items"":[{""Item"":""myitem""},null]}";
var json = JsonObject.Parse(compactJson);
var items = json.ArrayObjects("items");
Assert.NotNull(items[0]);
Assert.Null(items[1]);
}
[Test]
public void Can_parse_array_with_null_objects_starting_with_null_item()
{
var compactJson = @"{""items"":[null,{""Item"":""myitem""}]}";
var json = JsonObject.Parse(compactJson);
var items = json.ArrayObjects("items");
Assert.Null(items[0]);
Assert.NotNull(items[1]);
}
}
}