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