From 866a68777e76c3f9b652407b3cd694b1359762aa Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 23 Aug 2021 13:35:26 -0700 Subject: [PATCH] teach json parsing about map patterns --- src/json.zig | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/json.zig b/src/json.zig index 41e0015..58aa632 100644 --- a/src/json.zig +++ b/src/json.zig @@ -1806,6 +1806,35 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: }, } }, + .ObjectBegin => { + // We are parsing into a slice, but we have an + // ObjectBegin. This might be ok, iff the type + // follows this pattern: []struct { key: []const u8, value: anytype } + // (could key be anytype?). + if (!isMapPattern(T)) + return error.UnexpectedToken; + var arraylist = std.ArrayList(ptrInfo.child).init(allocator); + errdefer { + while (arraylist.popOrNull()) |v| { + parseFree(ptrInfo.child, v, options); + } + arraylist.deinit(); + } + while (true) { + const key = (try tokens.next()) orelse return error.UnexpectedEndOfJson; + switch (key) { + .ObjectEnd => break, + else => {}, + } + + try arraylist.ensureCapacity(arraylist.items.len + 1); + const key_val = try parseInternal(try typeForField(ptrInfo.child, "key"), key, tokens, options); + const val = (try tokens.next()) orelse return error.UnexpectedEndOfJson; + const val_val = try parseInternal(try typeForField(ptrInfo.child, "value"), val, tokens, options); + arraylist.appendAssumeCapacity(.{ .key = key_val, .value = val_val }); + } + return arraylist.toOwnedSlice(); + }, else => return error.UnexpectedToken, } }, @@ -1817,6 +1846,40 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: unreachable; } +fn typeForField(comptime T: type, field_name: []const u8) !type { + const ti = @typeInfo(T); + switch (ti) { + .Struct => { + inline for (ti.Struct.fields) |field| { + if (std.mem.eql(u8, field.name, field_name)) + return field.field_type; + } + }, + else => return error.TypeIsNotAStruct, // should not hit this + } + return error.FieldNotFound; +} + +fn isMapPattern(comptime T: type) bool { + // We should be getting a type that is a pointer to a slice. + // Let's just double check before proceeding + const ti = @typeInfo(T); + if (ti != .Pointer) return false; + if (ti.Pointer.size != .Slice) return false; + const ti_child = @typeInfo(ti.Pointer.child); + if (ti_child != .Struct) return false; + if (ti_child.Struct.fields.len != 2) return false; + var key_found = false; + var value_found = false; + inline for (ti_child.Struct.fields) |field| { + if (std.mem.eql(u8, "key", field.name)) + key_found = true; + if (std.mem.eql(u8, "value", field.name)) + value_found = true; + } + return key_found and value_found; +} + pub fn parse(comptime T: type, tokens: *TokenStream, options: ParseOptions) !T { const token = (try tokens.next()) orelse return error.UnexpectedEndOfJson; return parseInternal(T, token, tokens, options); @@ -1922,6 +1985,15 @@ test "parse into that allocates a slice" { } } +test "parse into that uses a map pattern" { + const options = ParseOptions{ .allocator = testing.allocator }; + const Map = []struct { key: []const u8, value: []const u8 }; + const r = try parse(Map, &TokenStream.init("{\"foo\": \"bar\"}"), options); + defer parseFree(Map, r, options); + try testing.expectEqualSlices(u8, "foo", r[0].key); + try testing.expectEqualSlices(u8, "bar", r[0].value); +} + test "parse into tagged union" { { const T = union(enum) {