Merge pull request #3215 from blob1807/json-better-enum-support

core:encoding/json Add support for writing enum value names
This commit is contained in:
gingerBill
2024-03-06 14:56:19 +00:00
committed by GitHub

View File

@@ -51,6 +51,11 @@ Marshal_Options :: struct {
// NOTE: This will temp allocate and sort a list for each map.
sort_maps_by_key: bool,
// Output enum value's name instead of its underlying value.
//
// NOTE: If a name isn't found it'll use the underlying value.
use_enum_names: bool,
// Internal state
indentation: int,
mjson_skipped_first_braces_start: bool,
@@ -411,7 +416,16 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
}
case runtime.Type_Info_Enum:
return marshal_to_writer(w, any{v.data, info.base.id}, opt)
if !opt.use_enum_names || len(info.names) == 0 {
return marshal_to_writer(w, any{v.data, info.base.id}, opt)
} else {
name, found := reflect.enum_name_from_value_any(v)
if found {
return marshal_to_writer(w, name, opt)
} else {
return marshal_to_writer(w, any{v.data, info.base.id}, opt)
}
}
case runtime.Type_Info_Bit_Set:
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {