Allow for comma-separate json names json:"name,flag"

This commit is contained in:
gingerBill
2024-04-10 13:29:10 +01:00
parent 97e2d8916a
commit d0dc7395e9
2 changed files with 11 additions and 1 deletions

View File

@@ -371,6 +371,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
info := ti.variant.(runtime.Type_Info_Struct)
for name, i in info.names {
json_name := reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json")
json_name = json_name_from_tag_value(json_name)
opt_write_iteration(w, opt, i) or_return
if json_name != "" {

View File

@@ -343,6 +343,15 @@ unmarshal_expect_token :: proc(p: ^Parser, kind: Token_Kind, loc := #caller_loca
return prev
}
@(private)
json_name_from_tag_value :: proc(value: string) -> (json_name: string) {
json_name = value
if comma_index := strings.index_byte(json_name, ','); comma_index >= 0 {
json_name = json_name[:comma_index]
}
return
}
@(private)
unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unmarshal_Error) {
@@ -384,7 +393,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
for field, field_idx in fields {
tag_value := string(reflect.struct_tag_get(field.tag, "json"))
if key == tag_value {
if key == json_name_from_tag_value(tag_value) {
use_field_idx = field_idx
break
}