authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-01 05:57:41 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-01 05:57:41 -07:00
log9fc84b75957900dda6dcc3714eb4406725c21cb7
tree28db2ade7de427101c84def29d8b5d062757d98b
parentaec2392d2cbf58db2ab118643e3a20a36bbc3f8d

add shorthands in this enum


1 files changed, 17 insertions(+), 2 deletions(-)

json.zig+17-2
......@@ -24,6 +24,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
2424 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.true));
2525 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.false));
2626 _ = try p.addStr(alloc, "");
27 std.debug.assert(try p.addArray(alloc, &.{}) == .empty_array);
28 std.debug.assert(try p.addObject(alloc, &ObjectHashMap{}) == .empty_object);
2729
2830 const root = try parseElement(alloc, &p);
2931 if (p.avail() > 0) return error.JsonExpectedTODO;
......@@ -79,7 +81,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
7981 defer members.deinit(alloc_local);
8082
8183 if (try p.eatByte('}')) |_| {
82 return try p.addObject(alloc, &members);
84 return .empty_object;
8385 }
8486 while (true) {
8587 const key = try parseString(alloc, p) orelse return error.JsonExpectedObjectKey;
......@@ -97,6 +99,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
9799 if (!p.support_trailing_commas) continue;
98100 if (try p.eatByte('}')) |_| break;
99101 }
102 if (members.entries.len == 0) {
103 return .empty_object;
104 }
100105 return try p.addObject(alloc, &members);
101106}
102107
......@@ -117,7 +122,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
117122 defer elements.deinit(alloc_local);
118123
119124 if (try p.eatByte(']')) |_| {
120 return try p.addArray(alloc, elements.items);
125 return .empty_array;
121126 }
122127 while (true) {
123128 const elem = try parseValue(alloc, p);
......@@ -131,6 +136,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
131136 if (!p.support_trailing_commas) continue;
132137 if (try p.eatByte(']')) |_| break;
133138 }
139 if (elements.items.len == 0) {
140 return .empty_array;
141 }
134142 return try p.addArray(alloc, elements.items);
135143}
136144
......@@ -452,6 +460,13 @@ pub const Document = struct {
452460};
453461
454462pub const ValueIndex = enum(u32) {
463 zero = 0,
464 null = 1,
465 true = 2,
466 false = 3,
467 empty_string = 4,
468 empty_array = 9,
469 empty_object = 14,
455470 _,
456471};
457472