| ... | @@ -81,7 +81,6 @@ pub const Value = union(enum) { | ... | @@ -81,7 +81,6 @@ pub const Value = union(enum) { |
| 81 | } | 81 | } |
| 82 | | 82 | |
| 83 | pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) ParseError(@TypeOf(source.*))!@This() { | 83 | pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) ParseError(@TypeOf(source.*))!@This() { |
| 84 | _ = options; | | |
| 85 | // The grammar of the stack is: | 84 | // The grammar of the stack is: |
| 86 | // (.array | .object .string)* | 85 | // (.array | .object .string)* |
| 87 | var stack = Array.init(allocator); | 86 | var stack = Array.init(allocator); |
| ... | @@ -93,21 +92,21 @@ pub const Value = union(enum) { | ... | @@ -93,21 +92,21 @@ pub const Value = union(enum) { |
| 93 | stack.items[stack.items.len - 1] == .array or | 92 | stack.items[stack.items.len - 1] == .array or |
| 94 | (stack.items[stack.items.len - 2] == .object and stack.items[stack.items.len - 1] == .string)); | 93 | (stack.items[stack.items.len - 2] == .object and stack.items[stack.items.len - 1] == .string)); |
| 95 | | 94 | |
| 96 | switch (try source.nextAlloc(allocator, .alloc_always)) { | 95 | switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) { |
| 97 | .allocated_string => |s| { | 96 | .allocated_string => |s| { |
| 98 | return try handleCompleteValue(&stack, allocator, source, Value{ .string = s }) orelse continue; | 97 | return try handleCompleteValue(&stack, allocator, source, Value{ .string = s }, options) orelse continue; |
| 99 | }, | 98 | }, |
| 100 | .allocated_number => |slice| { | 99 | .allocated_number => |slice| { |
| 101 | return try handleCompleteValue(&stack, allocator, source, Value.parseFromNumberSlice(slice)) orelse continue; | 100 | return try handleCompleteValue(&stack, allocator, source, Value.parseFromNumberSlice(slice), options) orelse continue; |
| 102 | }, | 101 | }, |
| 103 | | 102 | |
| 104 | .null => return try handleCompleteValue(&stack, allocator, source, .null) orelse continue, | 103 | .null => return try handleCompleteValue(&stack, allocator, source, .null, options) orelse continue, |
| 105 | .true => return try handleCompleteValue(&stack, allocator, source, Value{ .bool = true }) orelse continue, | 104 | .true => return try handleCompleteValue(&stack, allocator, source, Value{ .bool = true }, options) orelse continue, |
| 106 | .false => return try handleCompleteValue(&stack, allocator, source, Value{ .bool = false }) orelse continue, | 105 | .false => return try handleCompleteValue(&stack, allocator, source, Value{ .bool = false }, options) orelse continue, |
| 107 | | 106 | |
| 108 | .object_begin => { | 107 | .object_begin => { |
| 109 | switch (try source.nextAlloc(allocator, .alloc_always)) { | 108 | switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) { |
| 110 | .object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = ObjectMap.init(allocator) }) orelse continue, | 109 | .object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = ObjectMap.init(allocator) }, options) orelse continue, |
| 111 | .allocated_string => |key| { | 110 | .allocated_string => |key| { |
| 112 | try stack.appendSlice(&[_]Value{ | 111 | try stack.appendSlice(&[_]Value{ |
| 113 | Value{ .object = ObjectMap.init(allocator) }, | 112 | Value{ .object = ObjectMap.init(allocator) }, |
| ... | @@ -120,7 +119,7 @@ pub const Value = union(enum) { | ... | @@ -120,7 +119,7 @@ pub const Value = union(enum) { |
| 120 | .array_begin => { | 119 | .array_begin => { |
| 121 | try stack.append(Value{ .array = Array.init(allocator) }); | 120 | try stack.append(Value{ .array = Array.init(allocator) }); |
| 122 | }, | 121 | }, |
| 123 | .array_end => return try handleCompleteValue(&stack, allocator, source, stack.pop()) orelse continue, | 122 | .array_end => return try handleCompleteValue(&stack, allocator, source, stack.pop(), options) orelse continue, |
| 124 | | 123 | |
| 125 | else => unreachable, | 124 | else => unreachable, |
| 126 | } | 125 | } |
| ... | @@ -134,7 +133,7 @@ pub const Value = union(enum) { | ... | @@ -134,7 +133,7 @@ pub const Value = union(enum) { |
| 134 | } | 133 | } |
| 135 | }; | 134 | }; |
| 136 | | 135 | |
| 137 | fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, value_: Value) !?Value { | 136 | fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, value_: Value, options: ParseOptions) !?Value { |
| 138 | if (stack.items.len == 0) return value_; | 137 | if (stack.items.len == 0) return value_; |
| 139 | var value = value_; | 138 | var value = value_; |
| 140 | while (true) { | 139 | while (true) { |
| ... | @@ -152,7 +151,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val | ... | @@ -152,7 +151,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val |
| 152 | | 151 | |
| 153 | // This is an invalid state to leave the stack in, | 152 | // This is an invalid state to leave the stack in, |
| 154 | // so we have to process the next token before we return. | 153 | // so we have to process the next token before we return. |
| 155 | switch (try source.nextAlloc(allocator, .alloc_always)) { | 154 | switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) { |
| 156 | .object_end => { | 155 | .object_end => { |
| 157 | // This object is complete. | 156 | // This object is complete. |
| 158 | value = stack.pop(); | 157 | value = stack.pop(); |