Fix converting JSON objects into *JSONObject struct fields

This commit is contained in:
Cameron Reed 2024-11-04 17:01:26 -07:00
parent 310d40be12
commit 189d95ae72
2 changed files with 20 additions and 19 deletions

View File

@ -950,6 +950,14 @@ fn convertJSONtoStructField(field_type1: type, field: *field_type1, value_or_nul
} else { } else {
return JSONConvertError.UnsupportedType; 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 => { .Array => {
@ -967,19 +975,11 @@ fn convertJSONtoStructField(field_type1: type, field: *field_type1, value_or_nul
} }
}, },
.Struct => { .Struct => {
if (field_type == JSONObject) { if (value.type == .Object) {
if (value.type == .Object) { const subObject = value.getObject() catch @panic("JSON value type does not match what was reported");
field.* = value.getObject() catch @panic("JSON value type does not match what was reported"); try JSONtoStruct(subObject, field_type, field);
} else {
return JSONConvertError.IncorrectType;
}
} else { } else {
if (value.type == .Object) { return JSONConvertError.IncorrectType;
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;
}
} }
}, },
else => { else => {
@ -1031,13 +1031,9 @@ fn convertStructFieldtoJSON(allocator: std.mem.Allocator, field_type: type, valu
} }
}, },
.Struct => { .Struct => {
if (field_type == JSONObject) { const obj = try structToJSON(allocator, value.*);
return try JSONObjectValue.create(allocator, value); errdefer obj.deinit();
} else { return try JSONObjectValue.create(allocator, obj);
const obj = try structToJSON(allocator, value.*);
errdefer obj.deinit();
return try JSONObjectValue.create(allocator, obj);
}
}, },
.Array => { .Array => {
var values = std.ArrayList(*JSONValue).init(allocator); 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); errdefer allocator.free(values_slice);
return try JSONArrayValue.create(allocator, 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 { } else {
return JSONConvertError.UnsupportedType; return JSONConvertError.UnsupportedType;
} }

View File

@ -2,6 +2,7 @@ const std = @import("std");
const json = @import("json.zig"); const json = @import("json.zig");
const TestStruct = struct { const TestStruct = struct {
item: ?*json.JSONObject,
hello: []const u8, hello: []const u8,
item2: struct { item2: struct {
subitem: isize, subitem: isize,