diff options
| author | korvahkh <92224397+korvahkh@users.noreply.github.com> | 2024-05-11 14:36:56 -0500 |
|---|---|---|
| committer | korvahkh <92224397+korvahkh@users.noreply.github.com> | 2024-05-11 14:36:56 -0500 |
| commit | 9b759f39fcf9fad941c390700358a06ecf96bd72 (patch) | |
| tree | f0cd877f6744f629670a369317457495e6ff29d1 /core/encoding/json | |
| parent | 3a3ae6d0dfe87e66a3465930ec2c3964543597ba (diff) | |
encoding/json: Properly marshal `#no_nil` unions
Previously the first variant of a `#no_nil` would always be output as
`null`, and following variants would be treated as the wrong type.
Diffstat (limited to 'core/encoding/json')
| -rw-r--r-- | core/encoding/json/marshal.odin | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index f45cdb1f1..83c0f8f79 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -469,12 +469,15 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: case: panic("Invalid union tag type") } - if v.data == nil || tag == 0 { - io.write_string(w, "null") or_return - } else { - id := info.variants[tag-1].id - return marshal_to_writer(w, any{v.data, id}, opt) + if !info.no_nil { + if tag == 0 { + io.write_string(w, "null") or_return + return nil + } + tag -= 1 } + id := info.variants[tag].id + return marshal_to_writer(w, any{v.data, id}, opt) case runtime.Type_Info_Enum: if !opt.use_enum_names || len(info.names) == 0 { |