From 64ae99f016998fe78a5a0304767463c46050567a Mon Sep 17 00:00:00 2001 From: VladPavliuk Date: Sat, 13 Jul 2024 14:13:59 +0300 Subject: [PATCH 1/2] Add support of `ignore` tag for `json.marshal` --- core/encoding/json/marshal.odin | 4 +++- tests/core/encoding/json/test_core_json.odin | 21 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 0464c24d1..99ae81485 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -406,7 +406,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: ti := runtime.type_info_base(type_info_of(v.id)) info := ti.variant.(runtime.Type_Info_Struct) first_iteration := true - for name, i in info.names { + fields_loop: for name, i in info.names { omitempty := false json_name, extra := json_name_from_tag_value(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json")) @@ -414,6 +414,8 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: switch flag { case "omitempty": omitempty = true + case "ignore": + continue fields_loop } } diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin index 92c050952..9fde5a443 100644 --- a/tests/core/encoding/json/test_core_json.odin +++ b/tests/core/encoding/json/test_core_json.odin @@ -368,4 +368,25 @@ utf8_string_of_multibyte_characters :: proc(t: ^testing.T) { val, err := json.parse_string(`"🐛✅"`) defer json.destroy_value(val) testing.expectf(t, err == nil, "Expected `json.parse` to return nil, got %v", err) +} + +@test +struct_with_ignore_tags :: proc(t: ^testing.T) { + My_Struct :: struct { + a: string `json:"_,ignore"`, + } + + my_struct := My_Struct{ + a = "test", + } + + my_struct_marshaled, marshal_err := json.marshal(my_struct) + defer delete(my_struct_marshaled) + + testing.expectf(t, marshal_err == nil, "Expected `json.marshal` to return nil error, got %v", marshal_err) + + my_struct_json := transmute(string)my_struct_marshaled + expected_json := `{}` + + testing.expectf(t, expected_json == my_struct_json, "Expected `json.marshal` to return %s, got %s", expected_json, my_struct_json) } \ No newline at end of file From 76fe5d1346e623b535d5168ec36e1254ef769c68 Mon Sep 17 00:00:00 2001 From: VladPavliuk Date: Sun, 14 Jul 2024 00:21:05 +0300 Subject: [PATCH 2/2] Align ignore syntax of json tags with fmt, cbor --- core/encoding/json/marshal.odin | 9 ++++++--- tests/core/encoding/json/test_core_json.odin | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 99ae81485..30426f911 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -406,16 +406,19 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: ti := runtime.type_info_base(type_info_of(v.id)) info := ti.variant.(runtime.Type_Info_Struct) first_iteration := true - fields_loop: for name, i in info.names { + for name, i in info.names { omitempty := false json_name, extra := json_name_from_tag_value(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json")) + + if json_name == "-" { + continue + } + for flag in strings.split_iterator(&extra, ",") { switch flag { case "omitempty": omitempty = true - case "ignore": - continue fields_loop } } diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin index 9fde5a443..a50dd7fe0 100644 --- a/tests/core/encoding/json/test_core_json.odin +++ b/tests/core/encoding/json/test_core_json.odin @@ -373,7 +373,7 @@ utf8_string_of_multibyte_characters :: proc(t: ^testing.T) { @test struct_with_ignore_tags :: proc(t: ^testing.T) { My_Struct :: struct { - a: string `json:"_,ignore"`, + a: string `json:"-"`, } my_struct := My_Struct{