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" {...@@ -1234,7 +1234,7 @@ test "json.validate" {
1234const Allocator = std.mem.Allocator;1234const Allocator = std.mem.Allocator;
1235const ArenaAllocator = std.heap.ArenaAllocator;1235const ArenaAllocator = std.heap.ArenaAllocator;
1236const ArrayList = std.ArrayList;1236const ArrayList = std.ArrayList;
1237const StringHashMap = std.StringHashMap;1237const StringArrayHashMap = std.StringArrayHashMap;
12381238
1239pub const ValueTree = struct {1239pub const ValueTree = struct {
1240 arena: ArenaAllocator,1240 arena: ArenaAllocator,
...@@ -1245,7 +1245,7 @@ pub const ValueTree = struct {...@@ -1245,7 +1245,7 @@ pub const ValueTree = struct {
1245 }1245 }
1246};1246};
12471247
1248pub const ObjectMap = StringHashMap(Value);1248pub const ObjectMap = StringArrayHashMap(Value);
1249pub const Array = ArrayList(Value);1249pub const Array = ArrayList(Value);
12501250
1251/// Represents a JSON value1251/// Represents a JSON value
lib/std/json/test.zig+41-25
...@@ -52,12 +52,28 @@ fn anyStreamingErrNonStreaming(comptime s: []const u8) void {...@@ -52,12 +52,28 @@ fn anyStreamingErrNonStreaming(comptime s: []const u8) void {
52 testing.expect(false);52 testing.expect(false);
53}53}
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
55////////////////////////////////////////////////////////////////////////////////////////////////////71////////////////////////////////////////////////////////////////////////////////////////////////////
56//72//
57// Additional tests not part of test JSONTestSuite.73// Additional tests not part of test JSONTestSuite.
5874
59test "y_trailing_comma_after_empty" {75test "y_trailing_comma_after_empty" {
60 ok(76 try roundTrip(
61 \\{"1":[],"2":{},"3":"4"}77 \\{"1":[],"2":{},"3":"4"}
62 );78 );
63}79}
...@@ -71,25 +87,25 @@ test "y_array_arraysWithSpaces" {...@@ -71,25 +87,25 @@ test "y_array_arraysWithSpaces" {
71}87}
7288
73test "y_array_empty" {89test "y_array_empty" {
74 ok(90 try roundTrip(
75 \\[]91 \\[]
76 );92 );
77}93}
7894
79test "y_array_empty-string" {95test "y_array_empty-string" {
80 ok(96 try roundTrip(
81 \\[""]97 \\[""]
82 );98 );
83}99}
84100
85test "y_array_ending_with_newline" {101test "y_array_ending_with_newline" {
86 ok(102 try roundTrip(
87 \\["a"]103 \\["a"]
88 );104 );
89}105}
90106
91test "y_array_false" {107test "y_array_false" {
92 ok(108 try roundTrip(
93 \\[false]109 \\[false]
94 );110 );
95}111}
...@@ -101,7 +117,7 @@ test "y_array_heterogeneous" {...@@ -101,7 +117,7 @@ test "y_array_heterogeneous" {
101}117}
102118
103test "y_array_null" {119test "y_array_null" {
104 ok(120 try roundTrip(
105 \\[null]121 \\[null]
106 );122 );
107}123}
...@@ -120,7 +136,7 @@ test "y_array_with_leading_space" {...@@ -120,7 +136,7 @@ test "y_array_with_leading_space" {
120}136}
121137
122test "y_array_with_several_null" {138test "y_array_with_several_null" {
123 ok(139 try roundTrip(
124 \\[1,null,null,null,2]140 \\[1,null,null,null,2]
125 );141 );
126}142}
...@@ -172,13 +188,13 @@ test "y_number_minus_zero" {...@@ -172,13 +188,13 @@ test "y_number_minus_zero" {
172}188}
173189
174test "y_number_negative_int" {190test "y_number_negative_int" {
175 ok(191 try roundTrip(
176 \\[-123]192 \\[-123]
177 );193 );
178}194}
179195
180test "y_number_negative_one" {196test "y_number_negative_one" {
181 ok(197 try roundTrip(
182 \\[-1]198 \\[-1]
183 );199 );
184}200}
...@@ -232,7 +248,7 @@ test "y_number_real_pos_exponent" {...@@ -232,7 +248,7 @@ test "y_number_real_pos_exponent" {
232}248}
233249
234test "y_number_simple_int" {250test "y_number_simple_int" {
235 ok(251 try roundTrip(
236 \\[123]252 \\[123]
237 );253 );
238}254}
...@@ -244,7 +260,7 @@ test "y_number_simple_real" {...@@ -244,7 +260,7 @@ test "y_number_simple_real" {
244}260}
245261
246test "y_object_basic" {262test "y_object_basic" {
247 ok(263 try roundTrip(
248 \\{"asd":"sdf"}264 \\{"asd":"sdf"}
249 );265 );
250}266}
...@@ -262,13 +278,13 @@ test "y_object_duplicated_key" {...@@ -262,13 +278,13 @@ test "y_object_duplicated_key" {
262}278}
263279
264test "y_object_empty" {280test "y_object_empty" {
265 ok(281 try roundTrip(
266 \\{}282 \\{}
267 );283 );
268}284}
269285
270test "y_object_empty_key" {286test "y_object_empty_key" {
271 ok(287 try roundTrip(
272 \\{"":0}288 \\{"":0}
273 );289 );
274}290}
...@@ -298,7 +314,7 @@ test "y_object_long_strings" {...@@ -298,7 +314,7 @@ test "y_object_long_strings" {
298}314}
299315
300test "y_object_simple" {316test "y_object_simple" {
301 ok(317 try roundTrip(
302 \\{"a":[]}318 \\{"a":[]}
303 );319 );
304}320}
...@@ -348,7 +364,7 @@ test "y_string_backslash_and_u_escaped_zero" {...@@ -348,7 +364,7 @@ test "y_string_backslash_and_u_escaped_zero" {
348}364}
349365
350test "y_string_backslash_doublequotes" {366test "y_string_backslash_doublequotes" {
351 ok(367 try roundTrip(
352 \\["\""]368 \\["\""]
353 );369 );
354}370}
...@@ -366,7 +382,7 @@ test "y_string_double_escape_a" {...@@ -366,7 +382,7 @@ test "y_string_double_escape_a" {
366}382}
367383
368test "y_string_double_escape_n" {384test "y_string_double_escape_n" {
369 ok(385 try roundTrip(
370 \\["\\n"]386 \\["\\n"]
371 );387 );
372}388}
...@@ -450,7 +466,7 @@ test "y_string_simple_ascii" {...@@ -450,7 +466,7 @@ test "y_string_simple_ascii" {
450}466}
451467
452test "y_string_space" {468test "y_string_space" {
453 ok(469 try roundTrip(
454 \\" "470 \\" "
455 );471 );
456}472}
...@@ -568,13 +584,13 @@ test "y_string_with_del_character" {...@@ -568,13 +584,13 @@ test "y_string_with_del_character" {
568}584}
569585
570test "y_structure_lonely_false" {586test "y_structure_lonely_false" {
571 ok(587 try roundTrip(
572 \\false588 \\false
573 );589 );
574}590}
575591
576test "y_structure_lonely_int" {592test "y_structure_lonely_int" {
577 ok(593 try roundTrip(
578 \\42594 \\42
579 );595 );
580}596}
...@@ -586,37 +602,37 @@ test "y_structure_lonely_negative_real" {...@@ -586,37 +602,37 @@ test "y_structure_lonely_negative_real" {
586}602}
587603
588test "y_structure_lonely_null" {604test "y_structure_lonely_null" {
589 ok(605 try roundTrip(
590 \\null606 \\null
591 );607 );
592}608}
593609
594test "y_structure_lonely_string" {610test "y_structure_lonely_string" {
595 ok(611 try roundTrip(
596 \\"asd"612 \\"asd"
597 );613 );
598}614}
599615
600test "y_structure_lonely_true" {616test "y_structure_lonely_true" {
601 ok(617 try roundTrip(
602 \\true618 \\true
603 );619 );
604}620}
605621
606test "y_structure_string_empty" {622test "y_structure_string_empty" {
607 ok(623 try roundTrip(
608 \\""624 \\""
609 );625 );
610}626}
611627
612test "y_structure_trailing_newline" {628test "y_structure_trailing_newline" {
613 ok(629 try roundTrip(
614 \\["a"]630 \\["a"]
615 );631 );
616}632}
617633
618test "y_structure_true_in_array" {634test "y_structure_true_in_array" {
619 ok(635 try roundTrip(
620 \\[true]636 \\[true]
621 );637 );
622}638}