mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 20:58:15 +00:00
Merge pull request #3914 from VladPavliuk/json-add-ignore-tag-support
Add support of `ignore` tag for `json.marshal`
This commit is contained in:
@@ -410,6 +410,11 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
|
|||||||
omitempty := false
|
omitempty := false
|
||||||
|
|
||||||
json_name, extra := json_name_from_tag_value(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json"))
|
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, ",") {
|
for flag in strings.split_iterator(&extra, ",") {
|
||||||
switch flag {
|
switch flag {
|
||||||
case "omitempty":
|
case "omitempty":
|
||||||
|
|||||||
@@ -368,4 +368,25 @@ utf8_string_of_multibyte_characters :: proc(t: ^testing.T) {
|
|||||||
val, err := json.parse_string(`"🐛✅"`)
|
val, err := json.parse_string(`"🐛✅"`)
|
||||||
defer json.destroy_value(val)
|
defer json.destroy_value(val)
|
||||||
testing.expectf(t, err == nil, "Expected `json.parse` to return nil, got %v", err)
|
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:"-"`,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user