diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-12-31 15:01:08 +0000 |
|---|---|---|
| committer | gingerBill <gingerBill@users.noreply.github.com> | 2025-12-31 15:01:08 +0000 |
| commit | 3c8b1ab414f251b12fc59ce17cab074185584bc6 (patch) | |
| tree | 0d32eba065bd777f3cb449f022591331f2b5f3da /core/encoding | |
| parent | 6f396ac49b159e5741579d3b5bb50318e8e75851 (diff) | |
| parent | 3acb62210fcdc48d7a280a1c160ac7ff9063902f (diff) | |
Merge branch 'master' of https://github.com/odin-lang/Odin
Diffstat (limited to 'core/encoding')
| -rw-r--r-- | core/encoding/json/marshal.odin | 32 | ||||
| -rw-r--r-- | core/encoding/json/types.odin | 1 | ||||
| -rw-r--r-- | core/encoding/json/unmarshal.odin | 9 |
3 files changed, 39 insertions, 3 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index e563c326a..c4e348aa8 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -122,9 +122,9 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: case runtime.Type_Info_Rune: r := a.(rune) - io.write_byte(w, '"') or_return - io.write_escaped_rune(w, r, '"', true) or_return - io.write_byte(w, '"') or_return + io.write_byte(w, '"') or_return + io.write_escaped_rune(w, r, '"', for_json = true) or_return + io.write_byte(w, '"') or_return case runtime.Type_Info_Float: switch f in a { @@ -414,6 +414,12 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: opt_write_iteration(w, opt, first_iteration) or_return first_iteration = false + + if opt.pretty { + comment := reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "jsoncomment") + opt_write_comment(w, opt, &comment) or_return + } + if json_name != "" { opt_write_key(w, opt, json_name) or_return } else { @@ -533,6 +539,26 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: return } +// Newlines are split into multiple comment lines +opt_write_comment :: proc(w: io.Writer, opt: ^Marshal_Options, comment: ^string) -> (err: io.Error) { + if comment^ == "" { + return nil + } + + switch opt.spec { + case .JSON5, .MJSON: + for line in strings.split_iterator(comment, "\n") { + io.write_string(w, "// ") or_return + io.write_string(w, line) or_return + io.write_rune(w, '\n') or_return + opt_write_indentation(w, opt) or_return + } + case .JSON: return nil + } + + return nil +} + // write key as quoted string or with optional quotes in mjson opt_write_key :: proc(w: io.Writer, opt: ^Marshal_Options, name: string) -> (err: io.Error) { switch opt.spec { diff --git a/core/encoding/json/types.odin b/core/encoding/json/types.odin index 1da17a0db..77cc7db85 100644 --- a/core/encoding/json/types.odin +++ b/core/encoding/json/types.odin @@ -76,6 +76,7 @@ Error :: enum { Invalid_Number, String_Not_Terminated, Invalid_String, + Invalid_Rune, // Parsing Errors diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin index 3cdc6429d..58365b684 100644 --- a/core/encoding/json/unmarshal.odin +++ b/core/encoding/json/unmarshal.odin @@ -225,6 +225,15 @@ unmarshal_string_token :: proc(p: ^Parser, val: any, token: Token, ti: ^reflect. } ok = true return + case rune: + for rne, i in str { + if i > 0 { + dst = {} + return false, .Invalid_Rune + } + dst = rne + } + return true, nil } #partial switch variant in ti.variant { |