diff --git a/src/json.zig b/src/json.zig index 8aee181..d54dcf4 100644 --- a/src/json.zig +++ b/src/json.zig @@ -950,6 +950,14 @@ fn convertJSONtoStructField(field_type1: type, field: *field_type1, value_or_nul } else { return JSONConvertError.UnsupportedType; } + } else if (field_type_info.Pointer.size == .One) { + if (field_type_info.Pointer.child == JSONObject and value.type == .Object) { + field.* = value.getObject() catch @panic("JSON value type does not match what was reported"); + } else { + return JSONConvertError.UnsupportedType; + } + } else { + return JSONConvertError.UnsupportedType; } }, .Array => { @@ -967,19 +975,11 @@ fn convertJSONtoStructField(field_type1: type, field: *field_type1, value_or_nul } }, .Struct => { - if (field_type == JSONObject) { - if (value.type == .Object) { - field.* = value.getObject() catch @panic("JSON value type does not match what was reported"); - } else { - return JSONConvertError.IncorrectType; - } + if (value.type == .Object) { + const subObject = value.getObject() catch @panic("JSON value type does not match what was reported"); + try JSONtoStruct(subObject, field_type, field); } else { - if (value.type == .Object) { - const subObject = value.getObject() catch @panic("JSON value type does not match what was reported"); - try JSONtoStruct(subObject, field_type, field); - } else { - return JSONConvertError.IncorrectType; - } + return JSONConvertError.IncorrectType; } }, else => { @@ -1031,13 +1031,9 @@ fn convertStructFieldtoJSON(allocator: std.mem.Allocator, field_type: type, valu } }, .Struct => { - if (field_type == JSONObject) { - return try JSONObjectValue.create(allocator, value); - } else { - const obj = try structToJSON(allocator, value.*); - errdefer obj.deinit(); - return try JSONObjectValue.create(allocator, obj); - } + const obj = try structToJSON(allocator, value.*); + errdefer obj.deinit(); + return try JSONObjectValue.create(allocator, obj); }, .Array => { var values = std.ArrayList(*JSONValue).init(allocator); @@ -1079,6 +1075,10 @@ fn convertStructFieldtoJSON(allocator: std.mem.Allocator, field_type: type, valu errdefer allocator.free(values_slice); return try JSONArrayValue.create(allocator, values_slice); } + } else if (field_type_info.Pointer.size == .One) { + if (field_type_info.Pointer.child == JSONObject) { + return try JSONObjectValue.create(allocator, value.*); + } } else { return JSONConvertError.UnsupportedType; } diff --git a/src/main.zig b/src/main.zig index 14c57f1..dcd9b6e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const json = @import("json.zig"); const TestStruct = struct { + item: ?*json.JSONObject, hello: []const u8, item2: struct { subitem: isize,