authorgravatar for Lewis@scphillips.comLewis Gaul <Lewis@scphillips.com> 2021-04-04 09:16:59+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-04 10:16:59+02:00
log74fd7107e85e19412c4b5d2c55d230844f22f372
tree17b0b5e32e04006df60a88a095a04741cdace67e
parent6fc822a9481c0d2c8b37f26f41be603dd77ab5a4
signature Signed by PGP key 4AEE18F83AFDEB23

Switch std.json to use an ordered hashmap


2 files changed, 43 insertions(+), 27 deletions(-)

lib/std/json.zig+2-2
......@@ -1234,7 +1234,7 @@ test "json.validate" {
12341234const Allocator = std.mem.Allocator;
12351235const ArenaAllocator = std.heap.ArenaAllocator;
12361236const ArrayList = std.ArrayList;
1237const StringHashMap = std.StringHashMap;
1237const StringArrayHashMap = std.StringArrayHashMap;
12381238
12391239pub const ValueTree = struct {
12401240 arena: ArenaAllocator,
......@@ -1245,7 +1245,7 @@ pub const ValueTree = struct {
12451245 }
12461246};
12471247
1248pub const ObjectMap = StringHashMap(Value);
1248pub const ObjectMap = StringArrayHashMap(Value);
12491249pub const Array = ArrayList(Value);
12501250
12511251/// Represents a JSON value
lib/std/json/test.zig+41-25
......@@ -52,12 +52,28 @@ fn anyStreamingErrNonStreaming(comptime s: []const u8) void {
5252 testing.expect(false);
5353}
5454
55fn roundTrip(s: []const u8) !void {
56 testing.expect(json.validate(s));
57
58 var p = json.Parser.init(testing.allocator, false);
59 defer p.deinit();
60
61 var tree = try p.parse(s);
62 defer tree.deinit();
63
64 var buf: [256]u8 = undefined;
65 var fbs = std.io.fixedBufferStream(&buf);
66 try tree.root.jsonStringify(.{}, fbs.writer());
67
68 testing.expectEqualStrings(s, fbs.getWritten());
69}
70
5571////////////////////////////////////////////////////////////////////////////////////////////////////
5672//
5773// Additional tests not part of test JSONTestSuite.
5874
5975test "y_trailing_comma_after_empty" {
60 ok(
76 try roundTrip(
6177 \\{"1":[],"2":{},"3":"4"}
6278 );
6379}
......@@ -71,25 +87,25 @@ test "y_array_arraysWithSpaces" {
7187}
7288
7389test "y_array_empty" {
74 ok(
90 try roundTrip(
7591 \\[]
7692 );
7793}
7894
7995test "y_array_empty-string" {
80 ok(
96 try roundTrip(
8197 \\[""]
8298 );
8399}
84100
85101test "y_array_ending_with_newline" {
86 ok(
102 try roundTrip(
87103 \\["a"]
88104 );
89105}
90106
91107test "y_array_false" {
92 ok(
108 try roundTrip(
93109 \\[false]
94110 );
95111}
......@@ -101,7 +117,7 @@ test "y_array_heterogeneous" {
101117}
102118
103119test "y_array_null" {
104 ok(
120 try roundTrip(
105121 \\[null]
106122 );
107123}
......@@ -120,7 +136,7 @@ test "y_array_with_leading_space" {
120136}
121137
122138test "y_array_with_several_null" {
123 ok(
139 try roundTrip(
124140 \\[1,null,null,null,2]
125141 );
126142}
......@@ -172,13 +188,13 @@ test "y_number_minus_zero" {
172188}
173189
174190test "y_number_negative_int" {
175 ok(
191 try roundTrip(
176192 \\[-123]
177193 );
178194}
179195
180196test "y_number_negative_one" {
181 ok(
197 try roundTrip(
182198 \\[-1]
183199 );
184200}
......@@ -232,7 +248,7 @@ test "y_number_real_pos_exponent" {
232248}
233249
234250test "y_number_simple_int" {
235 ok(
251 try roundTrip(
236252 \\[123]
237253 );
238254}
......@@ -244,7 +260,7 @@ test "y_number_simple_real" {
244260}
245261
246262test "y_object_basic" {
247 ok(
263 try roundTrip(
248264 \\{"asd":"sdf"}
249265 );
250266}
......@@ -262,13 +278,13 @@ test "y_object_duplicated_key" {
262278}
263279
264280test "y_object_empty" {
265 ok(
281 try roundTrip(
266282 \\{}
267283 );
268284}
269285
270286test "y_object_empty_key" {
271 ok(
287 try roundTrip(
272288 \\{"":0}
273289 );
274290}
......@@ -298,7 +314,7 @@ test "y_object_long_strings" {
298314}
299315
300316test "y_object_simple" {
301 ok(
317 try roundTrip(
302318 \\{"a":[]}
303319 );
304320}
......@@ -348,7 +364,7 @@ test "y_string_backslash_and_u_escaped_zero" {
348364}
349365
350366test "y_string_backslash_doublequotes" {
351 ok(
367 try roundTrip(
352368 \\["\""]
353369 );
354370}
......@@ -366,7 +382,7 @@ test "y_string_double_escape_a" {
366382}
367383
368384test "y_string_double_escape_n" {
369 ok(
385 try roundTrip(
370386 \\["\\n"]
371387 );
372388}
......@@ -450,7 +466,7 @@ test "y_string_simple_ascii" {
450466}
451467
452468test "y_string_space" {
453 ok(
469 try roundTrip(
454470 \\" "
455471 );
456472}
......@@ -568,13 +584,13 @@ test "y_string_with_del_character" {
568584}
569585
570586test "y_structure_lonely_false" {
571 ok(
587 try roundTrip(
572588 \\false
573589 );
574590}
575591
576592test "y_structure_lonely_int" {
577 ok(
593 try roundTrip(
578594 \\42
579595 );
580596}
......@@ -586,37 +602,37 @@ test "y_structure_lonely_negative_real" {
586602}
587603
588604test "y_structure_lonely_null" {
589 ok(
605 try roundTrip(
590606 \\null
591607 );
592608}
593609
594610test "y_structure_lonely_string" {
595 ok(
611 try roundTrip(
596612 \\"asd"
597613 );
598614}
599615
600616test "y_structure_lonely_true" {
601 ok(
617 try roundTrip(
602618 \\true
603619 );
604620}
605621
606622test "y_structure_string_empty" {
607 ok(
623 try roundTrip(
608624 \\""
609625 );
610626}
611627
612628test "y_structure_trailing_newline" {
613 ok(
629 try roundTrip(
614630 \\["a"]
615631 );
616632}
617633
618634test "y_structure_true_in_array" {
619 ok(
635 try roundTrip(
620636 \\[true]
621637 );
622638}