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:...@@ -24,6 +24,8 @@ pub fn parse(alloc: std.mem.Allocator, path: string, inreader: anytype, options:
24 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.true));24 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.true));
25 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.false));25 p.extras.appendAssumeCapacity(@intFromEnum(Value.Tag.false));
26 _ = try p.addStr(alloc, "");26 _ = 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
28 const root = try parseElement(alloc, &p);30 const root = try parseElement(alloc, &p);
29 if (p.avail() > 0) return error.JsonExpectedTODO;31 if (p.avail() > 0) return error.JsonExpectedTODO;
...@@ -79,7 +81,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -79,7 +81,7 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
79 defer members.deinit(alloc_local);81 defer members.deinit(alloc_local);
8082
81 if (try p.eatByte('}')) |_| {83 if (try p.eatByte('}')) |_| {
82 return try p.addObject(alloc, &members);84 return .empty_object;
83 }85 }
84 while (true) {86 while (true) {
85 const key = try parseString(alloc, p) orelse return error.JsonExpectedObjectKey;87 const key = try parseString(alloc, p) orelse return error.JsonExpectedObjectKey;
...@@ -97,6 +99,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -97,6 +99,9 @@ fn parseObject(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
97 if (!p.support_trailing_commas) continue;99 if (!p.support_trailing_commas) continue;
98 if (try p.eatByte('}')) |_| break;100 if (try p.eatByte('}')) |_| break;
99 }101 }
102 if (members.entries.len == 0) {
103 return .empty_object;
104 }
100 return try p.addObject(alloc, &members);105 return try p.addObject(alloc, &members);
101}106}
102107
...@@ -117,7 +122,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -117,7 +122,7 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
117 defer elements.deinit(alloc_local);122 defer elements.deinit(alloc_local);
118123
119 if (try p.eatByte(']')) |_| {124 if (try p.eatByte(']')) |_| {
120 return try p.addArray(alloc, elements.items);125 return .empty_array;
121 }126 }
122 while (true) {127 while (true) {
123 const elem = try parseValue(alloc, p);128 const elem = try parseValue(alloc, p);
...@@ -131,6 +136,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {...@@ -131,6 +136,9 @@ fn parseArray(alloc: std.mem.Allocator, p: *Parser) anyerror!?ValueIndex {
131 if (!p.support_trailing_commas) continue;136 if (!p.support_trailing_commas) continue;
132 if (try p.eatByte(']')) |_| break;137 if (try p.eatByte(']')) |_| break;
133 }138 }
139 if (elements.items.len == 0) {
140 return .empty_array;
141 }
134 return try p.addArray(alloc, elements.items);142 return try p.addArray(alloc, elements.items);
135}143}
136144
...@@ -452,6 +460,13 @@ pub const Document = struct {...@@ -452,6 +460,13 @@ pub const Document = struct {
452};460};
453461
454pub const ValueIndex = enum(u32) {462pub 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,
455 _,470 _,
456};471};
457472