Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_json.d

raw
1 @system unittest
2 {
3 import std.json;
4
5 import std.conv : to;
6
7 // parse a file or string of json into a usable structure
8 string s = `{ "language": "D", "rating": 3.5, "code": "42" }`;
9 JSONValue j = parseJSON(s);
10 // j and j["language"] return JSONValue,
11 // j["language"].str returns a string
12 assert(j["language"].str == "D");
13 assert(j["rating"].floating == 3.5);
14
15 // check a type
16 long x;
17 if (const(JSONValue)* code = "code" in j)
18 {
19 if (code.type() == JSONType.integer)
20 x = code.integer;
21 else
22 x = to!int(code.str);
23 }
24
25 // create a json struct
26 JSONValue jj = [ "language": "D" ];
27 // rating doesnt exist yet, so use .object to assign
28 jj.object["rating"] = JSONValue(3.5);
29 // create an array to assign to list
30 jj.object["list"] = JSONValue( ["a", "b", "c"] );
31 // list already exists, so .object optional
32 jj["list"].array ~= JSONValue("D");
33
34 string jjStr = `{"language":"D","list":["a","b","c","D"],"rating":3.5}`;
35 assert(jj.toString == jjStr);
36 }
37
38 @safe unittest
39 {
40 import std.json;
41
42 string s = "{ \"language\": \"D\" }";
43 JSONValue j = parseJSON(s);
44 assert(j.type == JSONType.object);
45 assert(j["language"].type == JSONType.string);
46
47 }
48
49 @safe unittest
50 {
51 import std.json;
52
53 JSONValue j = [ "language": "D" ];
54
55 // get value
56 assert(j["language"].str == "D");
57
58 // change existing key to new string
59 j["language"].str = "Perl";
60 assert(j["language"].str == "Perl");
61
62 }
63
64 @safe unittest
65 {
66 import std.json;
67
68 JSONValue j = true;
69 assert(j.boolean == true);
70
71 j.boolean = false;
72 assert(j.boolean == false);
73
74 j.integer = 12;
75 import std.exception : assertThrown;
76 assertThrown!JSONException(j.boolean);
77
78 }
79
80 @safe unittest
81 {
82 import std.json;
83
84 import std.exception;
85 import std.conv;
86 string s =
87 `{
88 "a": 123,
89 "b": 3.1415,
90 "c": "text",
91 "d": true,
92 "e": [1, 2, 3],
93 "f": { "a": 1 },
94 "g": -45,
95 "h": ` ~ ulong.max.to!string ~ `,
96 }`;
97
98 struct a { }
99
100 immutable json = parseJSON(s);
101 assert(json["a"].get!double == 123.0);
102 assert(json["a"].get!int == 123);
103 assert(json["a"].get!uint == 123);
104 assert(json["b"].get!double == 3.1415);
105 assertThrown!JSONException(json["b"].get!int);
106 assert(json["c"].get!string == "text");
107 assert(json["d"].get!bool == true);
108 assertNotThrown(json["e"].get!(JSONValue[]));
109 assertNotThrown(json["f"].get!(JSONValue[string]));
110 static assert(!__traits(compiles, json["a"].get!a));
111 assertThrown!JSONException(json["e"].get!float);
112 assertThrown!JSONException(json["d"].get!(JSONValue[string]));
113 assertThrown!JSONException(json["f"].get!(JSONValue[]));
114 assert(json["g"].get!int == -45);
115 assertThrown!ConvException(json["g"].get!uint);
116 assert(json["h"].get!ulong == ulong.max);
117 assertThrown!ConvException(json["h"].get!uint);
118 assertNotThrown(json["h"].get!float);
119
120 }
121
122 @safe unittest
123 {
124 import std.json;
125
126 JSONValue j = JSONValue( "a string" );
127 j = JSONValue(42);
128
129 j = JSONValue( [1, 2, 3] );
130 assert(j.type == JSONType.array);
131
132 j = JSONValue( ["language": "D"] );
133 assert(j.type == JSONType.object);
134
135 }
136
137 @system unittest
138 {
139 import std.json;
140
141 JSONValue obj1 = JSONValue.emptyObject;
142 assert(obj1.type == JSONType.object);
143 obj1.object["a"] = JSONValue(1);
144 assert(obj1.object["a"] == JSONValue(1));
145
146 JSONValue obj2 = JSONValue.emptyObject;
147 assert("a" !in obj2.object);
148 obj2.object["b"] = JSONValue(5);
149 assert(obj1 != obj2);
150
151 }
152
153 @system unittest
154 {
155 import std.json;
156
157 JSONValue obj = JSONValue.emptyOrderedObject;
158 assert(obj.type == JSONType.object);
159 assert(obj.isOrdered);
160 obj["b"] = JSONValue(2);
161 obj["a"] = JSONValue(1);
162 assert(obj["a"] == JSONValue(1));
163 assert(obj["b"] == JSONValue(2));
164
165 string[] keys;
166 foreach (string k, JSONValue v; obj)
167 keys ~= k;
168 assert(keys == ["b", "a"]);
169
170 }
171
172 @system unittest
173 {
174 import std.json;
175
176 JSONValue arr1 = JSONValue.emptyArray;
177 assert(arr1.type == JSONType.array);
178 assert(arr1.array.length == 0);
179 arr1.array ~= JSONValue("Hello");
180 assert(arr1.array.length == 1);
181 assert(arr1.array[0] == JSONValue("Hello"));
182
183 JSONValue arr2 = JSONValue.emptyArray;
184 assert(arr2.array.length == 0);
185 assert(arr1 != arr2);
186
187 }
188
189 @safe unittest
190 {
191 import std.json;
192
193 JSONValue j = JSONValue( [42, 43, 44] );
194 assert( j[0].integer == 42 );
195 assert( j[1].integer == 43 );
196
197 }
198
199 @safe unittest
200 {
201 import std.json;
202
203 JSONValue j = JSONValue( ["language": "D"] );
204 assert( j["language"].str == "D" );
205
206 }
207
208 @safe unittest
209 {
210 import std.json;
211
212 JSONValue j = JSONValue( ["language": "D"] );
213 j["language"].str = "Perl";
214 assert( j["language"].str == "Perl" );
215
216 }
217
218 @safe unittest
219 {
220 import std.json;
221
222 JSONValue j = JSONValue( ["Perl", "C"] );
223 j[1].str = "D";
224 assert( j[1].str == "D" );
225
226 }
227
228 @safe unittest
229 {
230 import std.json;
231
232 JSONValue j = [ "language": "D", "author": "walter" ];
233 string a = ("author" in j).str;
234 *("author" in j) = "Walter";
235 assert(j["author"].str == "Walter");
236
237 }
238
239 @safe unittest
240 {
241 import std.json;
242
243 assert(JSONValue(10).opEquals(JSONValue(10.0)));
244 assert(JSONValue(10) != (JSONValue(10.5)));
245
246 assert(JSONValue(1) != JSONValue(true));
247 assert(JSONValue.emptyArray != JSONValue.emptyObject);
248
249 assert(parseJSON(`{"a": 1, "b": 2}`).opEquals(parseJSON(`{"b": 2, "a": 1}`)));
250
251 }
252