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 {
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;
}

View File

@ -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,