| ... | @@ -10,21 +10,29 @@ const AllocWhen = @import("./scanner.zig").AllocWhen; | ... | @@ -10,21 +10,29 @@ const AllocWhen = @import("./scanner.zig").AllocWhen; |
| 10 | const default_max_value_len = @import("./scanner.zig").default_max_value_len; | 10 | const default_max_value_len = @import("./scanner.zig").default_max_value_len; |
| 11 | const isNumberFormattedLikeAnInteger = @import("./scanner.zig").isNumberFormattedLikeAnInteger; | 11 | const isNumberFormattedLikeAnInteger = @import("./scanner.zig").isNumberFormattedLikeAnInteger; |
| 12 | | 12 | |
| | 13 | const Value = @import("./dynamic.zig").Value; |
| | 14 | const Array = @import("./dynamic.zig").Array; |
| | 15 | |
| | 16 | /// Controls how to deal with various inconsistencies between the JSON document and the Zig struct type passed in. |
| | 17 | /// For duplicate fields or unknown fields, set options in this struct. |
| | 18 | /// For missing fields, give the Zig struct fields default values. |
| 13 | pub const ParseOptions = struct { | 19 | pub const ParseOptions = struct { |
| 14 | /// Behaviour when a duplicate field is encountered. | 20 | /// Behaviour when a duplicate field is encountered. |
| | 21 | /// The default is to return `error.DuplicateField`. |
| 15 | duplicate_field_behavior: enum { | 22 | duplicate_field_behavior: enum { |
| 16 | use_first, | 23 | use_first, |
| 17 | @"error", | 24 | @"error", |
| 18 | use_last, | 25 | use_last, |
| 19 | } = .@"error", | 26 | } = .@"error", |
| 20 | | 27 | |
| 21 | /// If false, finding an unknown field returns an error. | 28 | /// If false, finding an unknown field returns `error.UnknownField`. |
| 22 | ignore_unknown_fields: bool = false, | 29 | ignore_unknown_fields: bool = false, |
| 23 | | 30 | |
| 24 | /// Passed to json.Scanner.nextAllocMax() or json.Reader.nextAllocMax(). | 31 | /// Passed to `std.json.Scanner.nextAllocMax` or `std.json.Reader.nextAllocMax`. |
| 25 | /// The default for parseFromSlice() or parseFromTokenSource() with a *json.Scanner input | 32 | /// The default for `parseFromSlice` or `parseFromTokenSource` with a `*std.json.Scanner` input |
| 26 | /// is the length of the input slice, which means error.ValueTooLong will never be returned. | 33 | /// is the length of the input slice, which means `error.ValueTooLong` will never be returned. |
| 27 | /// The default for parseFromTokenSource() with a *json.Reader is default_max_value_len. | 34 | /// The default for `parseFromTokenSource` with a `*std.json.Reader` is `std.json.default_max_value_len`. |
| | 35 | /// Ignored for `parseFromValue` and `parseFromValueLeaky`. |
| 28 | max_value_len: ?usize = null, | 36 | max_value_len: ?usize = null, |
| 29 | }; | 37 | }; |
| 30 | | 38 | |
| ... | @@ -43,6 +51,7 @@ pub fn Parsed(comptime T: type) type { | ... | @@ -43,6 +51,7 @@ pub fn Parsed(comptime T: type) type { |
| 43 | | 51 | |
| 44 | /// Parses the json document from `s` and returns the result packaged in a `std.json.Parsed`. | 52 | /// Parses the json document from `s` and returns the result packaged in a `std.json.Parsed`. |
| 45 | /// You must call `deinit()` of the returned object to clean up allocated resources. | 53 | /// You must call `deinit()` of the returned object to clean up allocated resources. |
| | 54 | /// If you are using a `std.heap.ArenaAllocator` or similar, consider calling `parseFromSliceLeaky` instead. |
| 46 | /// Note that `error.BufferUnderrun` is not actually possible to return from this function. | 55 | /// Note that `error.BufferUnderrun` is not actually possible to return from this function. |
| 47 | pub fn parseFromSlice( | 56 | pub fn parseFromSlice( |
| 48 | comptime T: type, | 57 | comptime T: type, |
| ... | @@ -114,33 +123,65 @@ pub fn parseFromTokenSourceLeaky( | ... | @@ -114,33 +123,65 @@ pub fn parseFromTokenSourceLeaky( |
| 114 | } | 123 | } |
| 115 | } | 124 | } |
| 116 | | 125 | |
| 117 | const value = try parseInternal(T, allocator, scanner_or_reader, resolved_options); | 126 | const value = try internalParse(T, allocator, scanner_or_reader, resolved_options); |
| 118 | | 127 | |
| 119 | assert(.end_of_document == try scanner_or_reader.next()); | 128 | assert(.end_of_document == try scanner_or_reader.next()); |
| 120 | | 129 | |
| 121 | return value; | 130 | return value; |
| 122 | } | 131 | } |
| 123 | | 132 | |
| | 133 | /// Like `parseFromSlice`, but the input is an already-parsed `std.json.Value` object. |
| | 134 | pub fn parseFromValue( |
| | 135 | comptime T: type, |
| | 136 | allocator: Allocator, |
| | 137 | source: Value, |
| | 138 | options: ParseOptions, |
| | 139 | ) ParseFromValueError!Parsed(T) { |
| | 140 | var parsed = Parsed(T){ |
| | 141 | .arena = try allocator.create(ArenaAllocator), |
| | 142 | .value = undefined, |
| | 143 | }; |
| | 144 | errdefer allocator.destroy(parsed.arena); |
| | 145 | parsed.arena.* = ArenaAllocator.init(allocator); |
| | 146 | errdefer parsed.arena.deinit(); |
| | 147 | |
| | 148 | parsed.value = try parseFromValueLeaky(T, parsed.arena.allocator(), source, options); |
| | 149 | |
| | 150 | return parsed; |
| | 151 | } |
| | 152 | |
| | 153 | pub fn parseFromValueLeaky( |
| | 154 | comptime T: type, |
| | 155 | allocator: Allocator, |
| | 156 | source: Value, |
| | 157 | options: ParseOptions, |
| | 158 | ) ParseFromValueError!T { |
| | 159 | // I guess this function doesn't need to exist, |
| | 160 | // but the flow of the sourcecode is easy to follow and grouped nicely with |
| | 161 | // this pub redirect function near the top and the implementation near the bottom. |
| | 162 | return internalParseFromValue(T, allocator, source, options); |
| | 163 | } |
| | 164 | |
| 124 | /// The error set that will be returned when parsing from `*Source`. | 165 | /// The error set that will be returned when parsing from `*Source`. |
| 125 | /// Note that this may contain `error.BufferUnderrun`, but that error will never actually be returned. | 166 | /// Note that this may contain `error.BufferUnderrun`, but that error will never actually be returned. |
| 126 | pub fn ParseError(comptime Source: type) type { | 167 | pub fn ParseError(comptime Source: type) type { |
| 127 | // A few of these will either always be present or present enough of the time that | 168 | // A few of these will either always be present or present enough of the time that |
| 128 | // omitting them is more confusing than always including them. | 169 | // omitting them is more confusing than always including them. |
| 129 | return error{ | 170 | return ParseFromValueError || Source.NextError || Source.PeekError || Source.AllocError; |
| 130 | UnexpectedToken, | | |
| 131 | InvalidNumber, | | |
| 132 | Overflow, | | |
| 133 | InvalidEnumTag, | | |
| 134 | DuplicateField, | | |
| 135 | UnknownField, | | |
| 136 | MissingField, | | |
| 137 | LengthMismatch, | | |
| 138 | } || | | |
| 139 | std.fmt.ParseIntError || std.fmt.ParseFloatError || | | |
| 140 | Source.NextError || Source.PeekError || Source.AllocError; | | |
| 141 | } | 171 | } |
| 142 | | 172 | |
| 143 | fn parseInternal( | 173 | pub const ParseFromValueError = std.fmt.ParseIntError || std.fmt.ParseFloatError || Allocator.Error || error{ |
| | 174 | UnexpectedToken, |
| | 175 | InvalidNumber, |
| | 176 | Overflow, |
| | 177 | InvalidEnumTag, |
| | 178 | DuplicateField, |
| | 179 | UnknownField, |
| | 180 | MissingField, |
| | 181 | LengthMismatch, |
| | 182 | }; |
| | 183 | |
| | 184 | fn internalParse( |
| 144 | comptime T: type, | 185 | comptime T: type, |
| 145 | allocator: Allocator, | 186 | allocator: Allocator, |
| 146 | source: anytype, | 187 | source: anytype, |
| ... | @@ -170,13 +211,7 @@ fn parseInternal( | ... | @@ -170,13 +211,7 @@ fn parseInternal( |
| 170 | inline .number, .allocated_number, .string, .allocated_string => |slice| slice, | 211 | inline .number, .allocated_number, .string, .allocated_string => |slice| slice, |
| 171 | else => return error.UnexpectedToken, | 212 | else => return error.UnexpectedToken, |
| 172 | }; | 213 | }; |
| 173 | if (isNumberFormattedLikeAnInteger(slice)) | 214 | return sliceToInt(T, slice); |
| 174 | return std.fmt.parseInt(T, slice, 10); | | |
| 175 | // Try to coerce a float to an integer. | | |
| 176 | const float = try std.fmt.parseFloat(f128, slice); | | |
| 177 | if (@round(float) != float) return error.InvalidNumber; | | |
| 178 | if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow; | | |
| 179 | return @intFromFloat(T, float); | | |
| 180 | }, | 215 | }, |
| 181 | .Optional => |optionalInfo| { | 216 | .Optional => |optionalInfo| { |
| 182 | switch (try source.peekNextTokenType()) { | 217 | switch (try source.peekNextTokenType()) { |
| ... | @@ -185,11 +220,11 @@ fn parseInternal( | ... | @@ -185,11 +220,11 @@ fn parseInternal( |
| 185 | return null; | 220 | return null; |
| 186 | }, | 221 | }, |
| 187 | else => { | 222 | else => { |
| 188 | return try parseInternal(optionalInfo.child, allocator, source, options); | 223 | return try internalParse(optionalInfo.child, allocator, source, options); |
| 189 | }, | 224 | }, |
| 190 | } | 225 | } |
| 191 | }, | 226 | }, |
| 192 | .Enum => |enumInfo| { | 227 | .Enum => { |
| 193 | if (comptime std.meta.trait.hasFn("jsonParse")(T)) { | 228 | if (comptime std.meta.trait.hasFn("jsonParse")(T)) { |
| 194 | return T.jsonParse(allocator, source, options); | 229 | return T.jsonParse(allocator, source, options); |
| 195 | } | 230 | } |
| ... | @@ -200,12 +235,7 @@ fn parseInternal( | ... | @@ -200,12 +235,7 @@ fn parseInternal( |
| 200 | inline .number, .allocated_number, .string, .allocated_string => |slice| slice, | 235 | inline .number, .allocated_number, .string, .allocated_string => |slice| slice, |
| 201 | else => return error.UnexpectedToken, | 236 | else => return error.UnexpectedToken, |
| 202 | }; | 237 | }; |
| 203 | // Check for a named value. | 238 | return sliceToEnum(T, slice); |
| 204 | if (std.meta.stringToEnum(T, slice)) |value| return value; | | |
| 205 | // Check for a numeric value. | | |
| 206 | if (!isNumberFormattedLikeAnInteger(slice)) return error.InvalidEnumTag; | | |
| 207 | const n = std.fmt.parseInt(enumInfo.tag_type, slice, 10) catch return error.InvalidEnumTag; | | |
| 208 | return try std.meta.intToEnum(T, n); | | |
| 209 | }, | 239 | }, |
| 210 | .Union => |unionInfo| { | 240 | .Union => |unionInfo| { |
| 211 | if (comptime std.meta.trait.hasFn("jsonParse")(T)) { | 241 | if (comptime std.meta.trait.hasFn("jsonParse")(T)) { |
| ... | @@ -226,7 +256,7 @@ fn parseInternal( | ... | @@ -226,7 +256,7 @@ fn parseInternal( |
| 226 | inline for (unionInfo.fields) |u_field| { | 256 | inline for (unionInfo.fields) |u_field| { |
| 227 | if (std.mem.eql(u8, u_field.name, field_name)) { | 257 | if (std.mem.eql(u8, u_field.name, field_name)) { |
| 228 | // Free the name token now in case we're using an allocator that optimizes freeing the last allocated object. | 258 | // Free the name token now in case we're using an allocator that optimizes freeing the last allocated object. |
| 229 | // (Recursing into parseInternal() might trigger more allocations.) | 259 | // (Recursing into internalParse() might trigger more allocations.) |
| 230 | freeAllocated(allocator, name_token.?); | 260 | freeAllocated(allocator, name_token.?); |
| 231 | name_token = null; | 261 | name_token = null; |
| 232 | | 262 | |
| ... | @@ -237,7 +267,7 @@ fn parseInternal( | ... | @@ -237,7 +267,7 @@ fn parseInternal( |
| 237 | result = @unionInit(T, u_field.name, {}); | 267 | result = @unionInit(T, u_field.name, {}); |
| 238 | } else { | 268 | } else { |
| 239 | // Recurse. | 269 | // Recurse. |
| 240 | result = @unionInit(T, u_field.name, try parseInternal(u_field.type, allocator, source, options)); | 270 | result = @unionInit(T, u_field.name, try internalParse(u_field.type, allocator, source, options)); |
| 241 | } | 271 | } |
| 242 | break; | 272 | break; |
| 243 | } | 273 | } |
| ... | @@ -256,10 +286,8 @@ fn parseInternal( | ... | @@ -256,10 +286,8 @@ fn parseInternal( |
| 256 | if (.array_begin != try source.next()) return error.UnexpectedToken; | 286 | if (.array_begin != try source.next()) return error.UnexpectedToken; |
| 257 | | 287 | |
| 258 | var r: T = undefined; | 288 | var r: T = undefined; |
| 259 | var fields_seen: usize = 0; | | |
| 260 | inline for (0..structInfo.fields.len) |i| { | 289 | inline for (0..structInfo.fields.len) |i| { |
| 261 | r[i] = try parseInternal(structInfo.fields[i].type, allocator, source, options); | 290 | r[i] = try internalParse(structInfo.fields[i].type, allocator, source, options); |
| 262 | fields_seen = i + 1; | | |
| 263 | } | 291 | } |
| 264 | | 292 | |
| 265 | if (.array_end != try source.next()) return error.UnexpectedToken; | 293 | if (.array_end != try source.next()) return error.UnexpectedToken; |
| ... | @@ -288,7 +316,7 @@ fn parseInternal( | ... | @@ -288,7 +316,7 @@ fn parseInternal( |
| 288 | if (field.is_comptime) @compileError("comptime fields are not supported: " ++ @typeName(T) ++ "." ++ field.name); | 316 | if (field.is_comptime) @compileError("comptime fields are not supported: " ++ @typeName(T) ++ "." ++ field.name); |
| 289 | if (std.mem.eql(u8, field.name, field_name)) { | 317 | if (std.mem.eql(u8, field.name, field_name)) { |
| 290 | // Free the name token now in case we're using an allocator that optimizes freeing the last allocated object. | 318 | // Free the name token now in case we're using an allocator that optimizes freeing the last allocated object. |
| 291 | // (Recursing into parseInternal() might trigger more allocations.) | 319 | // (Recursing into internalParse() might trigger more allocations.) |
| 292 | freeAllocated(allocator, name_token.?); | 320 | freeAllocated(allocator, name_token.?); |
| 293 | name_token = null; | 321 | name_token = null; |
| 294 | | 322 | |
| ... | @@ -297,14 +325,14 @@ fn parseInternal( | ... | @@ -297,14 +325,14 @@ fn parseInternal( |
| 297 | .use_first => { | 325 | .use_first => { |
| 298 | // Parse and ignore the redundant value. | 326 | // Parse and ignore the redundant value. |
| 299 | // We don't want to skip the value, because we want type checking. | 327 | // We don't want to skip the value, because we want type checking. |
| 300 | _ = try parseInternal(field.type, allocator, source, options); | 328 | _ = try internalParse(field.type, allocator, source, options); |
| 301 | break; | 329 | break; |
| 302 | }, | 330 | }, |
| 303 | .@"error" => return error.DuplicateField, | 331 | .@"error" => return error.DuplicateField, |
| 304 | .use_last => {}, | 332 | .use_last => {}, |
| 305 | } | 333 | } |
| 306 | } | 334 | } |
| 307 | @field(r, field.name) = try parseInternal(field.type, allocator, source, options); | 335 | @field(r, field.name) = try internalParse(field.type, allocator, source, options); |
| 308 | fields_seen[i] = true; | 336 | fields_seen[i] = true; |
| 309 | break; | 337 | break; |
| 310 | } | 338 | } |
| ... | @@ -318,16 +346,7 @@ fn parseInternal( | ... | @@ -318,16 +346,7 @@ fn parseInternal( |
| 318 | } | 346 | } |
| 319 | } | 347 | } |
| 320 | } | 348 | } |
| 321 | inline for (structInfo.fields, 0..) |field, i| { | 349 | try fillDefaultStructValues(T, &r, &fields_seen); |
| 322 | if (!fields_seen[i]) { | | |
| 323 | if (field.default_value) |default_ptr| { | | |
| 324 | const default = @ptrCast(*align(1) const field.type, default_ptr).*; | | |
| 325 | @field(r, field.name) = default; | | |
| 326 | } else { | | |
| 327 | return error.MissingField; | | |
| 328 | } | | |
| 329 | } | | |
| 330 | } | | |
| 331 | return r; | 350 | return r; |
| 332 | }, | 351 | }, |
| 333 | | 352 | |
| ... | @@ -335,7 +354,7 @@ fn parseInternal( | ... | @@ -335,7 +354,7 @@ fn parseInternal( |
| 335 | switch (try source.peekNextTokenType()) { | 354 | switch (try source.peekNextTokenType()) { |
| 336 | .array_begin => { | 355 | .array_begin => { |
| 337 | // Typical array. | 356 | // Typical array. |
| 338 | return parseInternalArray(T, arrayInfo.child, arrayInfo.len, allocator, source, options); | 357 | return internalParseArray(T, arrayInfo.child, arrayInfo.len, allocator, source, options); |
| 339 | }, | 358 | }, |
| 340 | .string => { | 359 | .string => { |
| 341 | if (arrayInfo.child != u8) return error.UnexpectedToken; | 360 | if (arrayInfo.child != u8) return error.UnexpectedToken; |
| ... | @@ -389,7 +408,7 @@ fn parseInternal( | ... | @@ -389,7 +408,7 @@ fn parseInternal( |
| 389 | .Vector => |vecInfo| { | 408 | .Vector => |vecInfo| { |
| 390 | switch (try source.peekNextTokenType()) { | 409 | switch (try source.peekNextTokenType()) { |
| 391 | .array_begin => { | 410 | .array_begin => { |
| 392 | return parseInternalArray(T, vecInfo.child, vecInfo.len, allocator, source, options); | 411 | return internalParseArray(T, vecInfo.child, vecInfo.len, allocator, source, options); |
| 393 | }, | 412 | }, |
| 394 | else => return error.UnexpectedToken, | 413 | else => return error.UnexpectedToken, |
| 395 | } | 414 | } |
| ... | @@ -399,7 +418,7 @@ fn parseInternal( | ... | @@ -399,7 +418,7 @@ fn parseInternal( |
| 399 | switch (ptrInfo.size) { | 418 | switch (ptrInfo.size) { |
| 400 | .One => { | 419 | .One => { |
| 401 | const r: *ptrInfo.child = try allocator.create(ptrInfo.child); | 420 | const r: *ptrInfo.child = try allocator.create(ptrInfo.child); |
| 402 | r.* = try parseInternal(ptrInfo.child, allocator, source, options); | 421 | r.* = try internalParse(ptrInfo.child, allocator, source, options); |
| 403 | return r; | 422 | return r; |
| 404 | }, | 423 | }, |
| 405 | .Slice => { | 424 | .Slice => { |
| ... | @@ -419,7 +438,7 @@ fn parseInternal( | ... | @@ -419,7 +438,7 @@ fn parseInternal( |
| 419 | } | 438 | } |
| 420 | | 439 | |
| 421 | try arraylist.ensureUnusedCapacity(1); | 440 | try arraylist.ensureUnusedCapacity(1); |
| 422 | arraylist.appendAssumeCapacity(try parseInternal(ptrInfo.child, allocator, source, options)); | 441 | arraylist.appendAssumeCapacity(try internalParse(ptrInfo.child, allocator, source, options)); |
| 423 | } | 442 | } |
| 424 | | 443 | |
| 425 | if (ptrInfo.sentinel) |some| { | 444 | if (ptrInfo.sentinel) |some| { |
| ... | @@ -463,7 +482,7 @@ fn parseInternal( | ... | @@ -463,7 +482,7 @@ fn parseInternal( |
| 463 | unreachable; | 482 | unreachable; |
| 464 | } | 483 | } |
| 465 | | 484 | |
| 466 | fn parseInternalArray( | 485 | fn internalParseArray( |
| 467 | comptime T: type, | 486 | comptime T: type, |
| 468 | comptime Child: type, | 487 | comptime Child: type, |
| 469 | comptime len: comptime_int, | 488 | comptime len: comptime_int, |
| ... | @@ -476,7 +495,7 @@ fn parseInternalArray( | ... | @@ -476,7 +495,7 @@ fn parseInternalArray( |
| 476 | var r: T = undefined; | 495 | var r: T = undefined; |
| 477 | var i: usize = 0; | 496 | var i: usize = 0; |
| 478 | while (i < len) : (i += 1) { | 497 | while (i < len) : (i += 1) { |
| 479 | r[i] = try parseInternal(Child, allocator, source, options); | 498 | r[i] = try internalParse(Child, allocator, source, options); |
| 480 | } | 499 | } |
| 481 | | 500 | |
| 482 | if (.array_end != try source.next()) return error.UnexpectedToken; | 501 | if (.array_end != try source.next()) return error.UnexpectedToken; |
| ... | @@ -484,6 +503,271 @@ fn parseInternalArray( | ... | @@ -484,6 +503,271 @@ fn parseInternalArray( |
| 484 | return r; | 503 | return r; |
| 485 | } | 504 | } |
| 486 | | 505 | |
| | 506 | fn internalParseFromValue( |
| | 507 | comptime T: type, |
| | 508 | allocator: Allocator, |
| | 509 | source: Value, |
| | 510 | options: ParseOptions, |
| | 511 | ) ParseFromValueError!T { |
| | 512 | switch (@typeInfo(T)) { |
| | 513 | .Bool => { |
| | 514 | switch (source) { |
| | 515 | .bool => |b| return b, |
| | 516 | else => return error.UnexpectedToken, |
| | 517 | } |
| | 518 | }, |
| | 519 | .Float, .ComptimeFloat => { |
| | 520 | switch (source) { |
| | 521 | .float => |f| return @floatCast(T, f), |
| | 522 | .integer => |i| return @floatFromInt(T, i), |
| | 523 | .number_string, .string => |s| return std.fmt.parseFloat(T, s), |
| | 524 | else => return error.UnexpectedToken, |
| | 525 | } |
| | 526 | }, |
| | 527 | .Int, .ComptimeInt => { |
| | 528 | switch (source) { |
| | 529 | .float => |f| { |
| | 530 | if (@round(f) != f) return error.InvalidNumber; |
| | 531 | if (f > std.math.maxInt(T)) return error.Overflow; |
| | 532 | if (f < std.math.minInt(T)) return error.Overflow; |
| | 533 | return @intFromFloat(T, f); |
| | 534 | }, |
| | 535 | .integer => |i| { |
| | 536 | if (i > std.math.maxInt(T)) return error.Overflow; |
| | 537 | if (i < std.math.minInt(T)) return error.Overflow; |
| | 538 | return @intCast(T, i); |
| | 539 | }, |
| | 540 | .number_string, .string => |s| { |
| | 541 | return sliceToInt(T, s); |
| | 542 | }, |
| | 543 | else => return error.UnexpectedToken, |
| | 544 | } |
| | 545 | }, |
| | 546 | .Optional => |optionalInfo| { |
| | 547 | switch (source) { |
| | 548 | .null => return null, |
| | 549 | else => return try internalParseFromValue(optionalInfo.child, allocator, source, options), |
| | 550 | } |
| | 551 | }, |
| | 552 | .Enum => { |
| | 553 | if (comptime std.meta.trait.hasFn("jsonParseFromValue")(T)) { |
| | 554 | return T.jsonParseFromValue(allocator, source, options); |
| | 555 | } |
| | 556 | |
| | 557 | switch (source) { |
| | 558 | .float => return error.InvalidEnumTag, |
| | 559 | .integer => |i| return std.meta.intToEnum(T, i), |
| | 560 | .number_string, .string => |s| return sliceToEnum(T, s), |
| | 561 | else => return error.UnexpectedToken, |
| | 562 | } |
| | 563 | }, |
| | 564 | .Union => |unionInfo| { |
| | 565 | if (comptime std.meta.trait.hasFn("jsonParseFromValue")(T)) { |
| | 566 | return T.jsonParseFromValue(allocator, source, options); |
| | 567 | } |
| | 568 | |
| | 569 | if (unionInfo.tag_type == null) @compileError("Unable to parse into untagged union '" ++ @typeName(T) ++ "'"); |
| | 570 | |
| | 571 | if (source != .object) return error.UnexpectedToken; |
| | 572 | if (source.object.count() != 1) return error.UnexpectedToken; |
| | 573 | |
| | 574 | var it = source.object.iterator(); |
| | 575 | const kv = it.next().?; |
| | 576 | const field_name = kv.key_ptr.*; |
| | 577 | |
| | 578 | inline for (unionInfo.fields) |u_field| { |
| | 579 | if (std.mem.eql(u8, u_field.name, field_name)) { |
| | 580 | if (u_field.type == void) { |
| | 581 | // void isn't really a json type, but we can support void payload union tags with {} as a value. |
| | 582 | if (kv.value_ptr.* != .object) return error.UnexpectedToken; |
| | 583 | if (kv.value_ptr.*.object.count() != 0) return error.UnexpectedToken; |
| | 584 | return @unionInit(T, u_field.name, {}); |
| | 585 | } |
| | 586 | // Recurse. |
| | 587 | return @unionInit(T, u_field.name, try internalParseFromValue(u_field.type, allocator, kv.value_ptr.*, options)); |
| | 588 | } |
| | 589 | } |
| | 590 | // Didn't match anything. |
| | 591 | return error.UnknownField; |
| | 592 | }, |
| | 593 | |
| | 594 | .Struct => |structInfo| { |
| | 595 | if (structInfo.is_tuple) { |
| | 596 | if (source != .array) return error.UnexpectedToken; |
| | 597 | if (source.array.items.len != structInfo.fields.len) return error.UnexpectedToken; |
| | 598 | |
| | 599 | var r: T = undefined; |
| | 600 | inline for (0..structInfo.fields.len, source.array.items) |i, item| { |
| | 601 | r[i] = try internalParseFromValue(structInfo.fields[i].type, allocator, item, options); |
| | 602 | } |
| | 603 | |
| | 604 | return r; |
| | 605 | } |
| | 606 | |
| | 607 | if (comptime std.meta.trait.hasFn("jsonParseFromValue")(T)) { |
| | 608 | return T.jsonParseFromValue(allocator, source, options); |
| | 609 | } |
| | 610 | |
| | 611 | if (source != .object) return error.UnexpectedToken; |
| | 612 | |
| | 613 | var r: T = undefined; |
| | 614 | var fields_seen = [_]bool{false} ** structInfo.fields.len; |
| | 615 | |
| | 616 | var it = source.object.iterator(); |
| | 617 | while (it.next()) |kv| { |
| | 618 | const field_name = kv.key_ptr.*; |
| | 619 | |
| | 620 | inline for (structInfo.fields, 0..) |field, i| { |
| | 621 | if (field.is_comptime) @compileError("comptime fields are not supported: " ++ @typeName(T) ++ "." ++ field.name); |
| | 622 | if (std.mem.eql(u8, field.name, field_name)) { |
| | 623 | if (fields_seen[i]) { |
| | 624 | switch (options.duplicate_field_behavior) { |
| | 625 | .use_first => { |
| | 626 | // Parse and ignore the redundant value. |
| | 627 | // We don't want to skip the value, because we want type checking. |
| | 628 | _ = try internalParseFromValue(field.type, allocator, kv.value_ptr.*, options); |
| | 629 | break; |
| | 630 | }, |
| | 631 | .@"error" => return error.DuplicateField, |
| | 632 | .use_last => {}, |
| | 633 | } |
| | 634 | } |
| | 635 | @field(r, field.name) = try internalParseFromValue(field.type, allocator, kv.value_ptr.*, options); |
| | 636 | fields_seen[i] = true; |
| | 637 | break; |
| | 638 | } |
| | 639 | } else { |
| | 640 | // Didn't match anything. |
| | 641 | if (!options.ignore_unknown_fields) return error.UnknownField; |
| | 642 | } |
| | 643 | } |
| | 644 | try fillDefaultStructValues(T, &r, &fields_seen); |
| | 645 | return r; |
| | 646 | }, |
| | 647 | |
| | 648 | .Array => |arrayInfo| { |
| | 649 | switch (source) { |
| | 650 | .array => |array| { |
| | 651 | // Typical array. |
| | 652 | return internalParseArrayFromArrayValue(T, arrayInfo.child, arrayInfo.len, allocator, array, options); |
| | 653 | }, |
| | 654 | .string => |s| { |
| | 655 | if (arrayInfo.child != u8) return error.UnexpectedToken; |
| | 656 | // Fixed-length string. |
| | 657 | |
| | 658 | if (s.len != arrayInfo.len) return error.LengthMismatch; |
| | 659 | |
| | 660 | var r: T = undefined; |
| | 661 | @memcpy(r[0..], s); |
| | 662 | return r; |
| | 663 | }, |
| | 664 | |
| | 665 | else => return error.UnexpectedToken, |
| | 666 | } |
| | 667 | }, |
| | 668 | |
| | 669 | .Vector => |vecInfo| { |
| | 670 | switch (source) { |
| | 671 | .array => |array| { |
| | 672 | return internalParseArrayFromArrayValue(T, vecInfo.child, vecInfo.len, allocator, array, options); |
| | 673 | }, |
| | 674 | else => return error.UnexpectedToken, |
| | 675 | } |
| | 676 | }, |
| | 677 | |
| | 678 | .Pointer => |ptrInfo| { |
| | 679 | switch (ptrInfo.size) { |
| | 680 | .One => { |
| | 681 | const r: *ptrInfo.child = try allocator.create(ptrInfo.child); |
| | 682 | r.* = try internalParseFromValue(ptrInfo.child, allocator, source, options); |
| | 683 | return r; |
| | 684 | }, |
| | 685 | .Slice => { |
| | 686 | switch (source) { |
| | 687 | .array => |array| { |
| | 688 | const r = if (ptrInfo.sentinel) |sentinel_ptr| |
| | 689 | try allocator.allocSentinel(ptrInfo.child, array.items.len, @ptrCast(*align(1) const ptrInfo.child, sentinel_ptr).*) |
| | 690 | else |
| | 691 | try allocator.alloc(ptrInfo.child, array.items.len); |
| | 692 | |
| | 693 | for (array.items, r) |item, *dest| { |
| | 694 | dest.* = try internalParseFromValue(ptrInfo.child, allocator, item, options); |
| | 695 | } |
| | 696 | |
| | 697 | return r; |
| | 698 | }, |
| | 699 | .string => |s| { |
| | 700 | if (ptrInfo.child != u8) return error.UnexpectedToken; |
| | 701 | // Dynamic length string. |
| | 702 | |
| | 703 | const r = if (ptrInfo.sentinel) |sentinel_ptr| |
| | 704 | try allocator.allocSentinel(ptrInfo.child, s.len, @ptrCast(*align(1) const ptrInfo.child, sentinel_ptr).*) |
| | 705 | else |
| | 706 | try allocator.alloc(ptrInfo.child, s.len); |
| | 707 | @memcpy(r[0..], s); |
| | 708 | |
| | 709 | return r; |
| | 710 | }, |
| | 711 | else => return error.UnexpectedToken, |
| | 712 | } |
| | 713 | }, |
| | 714 | else => @compileError("Unable to parse into type '" ++ @typeName(T) ++ "'"), |
| | 715 | } |
| | 716 | }, |
| | 717 | else => @compileError("Unable to parse into type '" ++ @typeName(T) ++ "'"), |
| | 718 | } |
| | 719 | } |
| | 720 | |
| | 721 | fn internalParseArrayFromArrayValue( |
| | 722 | comptime T: type, |
| | 723 | comptime Child: type, |
| | 724 | comptime len: comptime_int, |
| | 725 | allocator: Allocator, |
| | 726 | array: Array, |
| | 727 | options: ParseOptions, |
| | 728 | ) !T { |
| | 729 | if (array.items.len != len) return error.LengthMismatch; |
| | 730 | |
| | 731 | var r: T = undefined; |
| | 732 | for (array.items, 0..) |item, i| { |
| | 733 | r[i] = try internalParseFromValue(Child, allocator, item, options); |
| | 734 | } |
| | 735 | |
| | 736 | return r; |
| | 737 | } |
| | 738 | |
| | 739 | fn sliceToInt(comptime T: type, slice: []const u8) !T { |
| | 740 | if (isNumberFormattedLikeAnInteger(slice)) |
| | 741 | return std.fmt.parseInt(T, slice, 10); |
| | 742 | // Try to coerce a float to an integer. |
| | 743 | const float = try std.fmt.parseFloat(f128, slice); |
| | 744 | if (@round(float) != float) return error.InvalidNumber; |
| | 745 | if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow; |
| | 746 | return @intCast(T, @intFromFloat(i128, float)); |
| | 747 | } |
| | 748 | |
| | 749 | fn sliceToEnum(comptime T: type, slice: []const u8) !T { |
| | 750 | // Check for a named value. |
| | 751 | if (std.meta.stringToEnum(T, slice)) |value| return value; |
| | 752 | // Check for a numeric value. |
| | 753 | if (!isNumberFormattedLikeAnInteger(slice)) return error.InvalidEnumTag; |
| | 754 | const n = std.fmt.parseInt(@typeInfo(T).Enum.tag_type, slice, 10) catch return error.InvalidEnumTag; |
| | 755 | return std.meta.intToEnum(T, n); |
| | 756 | } |
| | 757 | |
| | 758 | fn fillDefaultStructValues(comptime T: type, r: *T, fields_seen: *[@typeInfo(T).Struct.fields.len]bool) !void { |
| | 759 | inline for (@typeInfo(T).Struct.fields, 0..) |field, i| { |
| | 760 | if (!fields_seen[i]) { |
| | 761 | if (field.default_value) |default_ptr| { |
| | 762 | const default = @ptrCast(*align(1) const field.type, default_ptr).*; |
| | 763 | @field(r, field.name) = default; |
| | 764 | } else { |
| | 765 | return error.MissingField; |
| | 766 | } |
| | 767 | } |
| | 768 | } |
| | 769 | } |
| | 770 | |
| 487 | fn freeAllocated(allocator: Allocator, token: Token) void { | 771 | fn freeAllocated(allocator: Allocator, token: Token) void { |
| 488 | switch (token) { | 772 | switch (token) { |
| 489 | .allocated_number, .allocated_string => |slice| { | 773 | .allocated_number, .allocated_string => |slice| { |