io.write(w, "}");
}
+ case .Union {
+ if !union_constructed_from(data.type, Optional) {
+ return .Unsupported_Type;
+ }
+
+ u := info->as_union();
+ tag := *cast(&Optional(void).tag_enum) data.data;
+ if tag != .Some {
+ io.write(w, "null");
+ } else {
+ encode(w, misc.any_member(data, u.variants[1].type, u.alignment));
+ }
+ }
+
case #default {
return .Unsupported_Type;
}
d_info := cast(^Type_Info_Distinct) t_info;
return from_any(d_info.base_type, in);
}
+
+ case .Union {
+ if !union_constructed_from(type, Optional) {
+ return null_value();
+ }
+
+ u := t_info->as_union();
+ tag := *cast(&Optional(void).tag_enum) in;
+ if tag != .Some {
+ return null_value();
+ } else {
+ return from_any(u.variants[1].type, memory.ptr_add(in, u.alignment), allocator);
+ }
+ }
}
return null_value();
d_info := cast(^Type_Info_Distinct) t_info;
to_any(value, d_info.base_type, out);
}
+
+ case .Union {
+ if !union_constructed_from(type, Optional) {
+ return;
+ }
+
+ u_info := t_info->as_union();
+ if value->is_null() {
+ *cast(& ? void) out = .{};
+ return;
+ } else {
+ *cast(&Optional(void).tag_enum) out = .Some;
+ to_any(value, u_info.variants[1].type, memory.ptr_add(out, u_info.alignment));
+ }
+ }
}
}
#doc """
"""
any_member :: #match #locked {
- macro (v: any, member_type: type_expr, member_offset: u32) -> any {
+ (v: any, member_type: type_expr, member_offset: u32) -> any {
return any.{
cast([&] u8, v.data) + member_offset,
member_type
return first_member.used && first_member.type == base_type;
}
+union_constructed_from :: (union_type: type_expr, base_type: type_expr) -> bool {
+ union_info := get_type_info(union_type);
+ if union_info.kind != .Union do return false;
+
+ return (cast(&Type_Info_Union) union_info).constructed_from == base_type;
+}
+
+
#operator == (t1: Type_Info_Function, t2: Type_Info_Function) -> bool {
if t1.parameter_types.count != t2.parameter_types.count do return false;
if t1.return_type != t2.return_type do return false;
{"test":"Wow!","working":123,"data":[1,2,3,4,5],"people":[{"name":"Joe"},{"name":"Bob"}],"other":[{"x":1},{"x":2}]}
{"foo":{"data":5}}
🂡
+main.Union_Test { v1 = None, v2 = Some(123) }
+{"v1":null,"v2":123}
+{"v1":null,"v2":123}
json.set(o, "foo", json.from_any(^.{data=5}));
json.encode(^core.stdio.print_writer, o);
- v := json.decode_with_error("""{ "key1": "\\uD83C\\uDCA1", "key2": "\\u264C" }""");
+ v := json.decode_with_error("{ \"key1\": \"\\uD83C\\uDCA1\", \"key2\": \"\\u264C\" }");
core.print("\n");
core.println(v.root["key1"]->as_str());
+
+
+ Union_Test :: struct {
+ v1: ? i32;
+ v2: ? i32;
+ }
+
+ ut_json := json.decode_with_error("""{ "v1": null, "v2": 123 }""");
+ ut: Union_Test;
+ json.as_any(ut_json.root, &ut);
+ core.println(ut);
+
+ json.encode(&core.stdio.print_writer, ut_json.root);
+ core.print("\n");
+ json.encode(&core.stdio.print_writer, ut);
+ core.print("\n");
}