Fix converting JSON objects into *JSONObject struct fields
This commit is contained in:
parent
310d40be12
commit
189d95ae72
24
src/json.zig
24
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,20 +975,12 @@ 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;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
},
|
||||
else => {
|
||||
@compileLog(field_type_info);
|
||||
@ -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);
|
||||
}
|
||||
},
|
||||
.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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user