diff options
| author | blob1807 <12388588+blob1807@users.noreply.github.com> | 2024-02-21 11:07:03 +1000 |
|---|---|---|
| committer | blob1807 <12388588+blob1807@users.noreply.github.com> | 2024-02-21 11:07:03 +1000 |
| commit | b2b8b14955e680543fecbbf397fc949b1ce121cf (patch) | |
| tree | 4dd161529195df320f347662fbc869fc90dd0191 /core/encoding/json | |
| parent | 7e0473dded188a18a19f5fc4aa9b45bc31934e0e (diff) | |
Add better support for Enums in json
Can now output enum value's name instead of its underlineing value
Diffstat (limited to 'core/encoding/json')
| -rw-r--r-- | core/encoding/json/marshal.odin | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index e9285364b..d3d046b33 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -51,6 +51,9 @@ 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 underlineing value + use_enum_value_names: bool + // Internal state indentation: int, mjson_skipped_first_braces_start: bool, @@ -401,7 +404,24 @@ 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_value_names || len(info.names) == 0 { + return marshal_to_writer(w, any{v.data, info.base.id}, opt) + } else { + enum_a := any{v.data, info.base.id} + u: runtime.Type_Info_Enum_Value + + switch i in enum_a { + case int: u = runtime.Type_Info_Enum_Value(i) + case: panic("Invalid enum base type") + } + + pos, found := slice.binary_search(info.values, u) + if found { + return marshal_to_writer(w, any(info.names[pos]), opt) + } else { + panic("Unable to find value in enum's values") + } + } case runtime.Type_Info_Bit_Set: is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool { |