aboutsummaryrefslogtreecommitdiff
path: root/core/encoding/json
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-12-30 13:42:19 +0000
committerGitHub <noreply@github.com>2025-12-30 13:42:19 +0000
commit107104ab4b199867bcf1c0404c82ec0d18cbc71d (patch)
treeb399202bb9c4c196a748f903738d43f645ae7b6c /core/encoding/json
parent047ceb2c81f0ba7da0beed51c4e7345cdbcaa139 (diff)
parentaffaefc13ace1ac95b97c043314da13d4587eb44 (diff)
Merge pull request #6081 from dozn/json5-and-sjson-comments
Add JSON5/SJSON Comments When Marshalling
Diffstat (limited to 'core/encoding/json')
-rw-r--r--core/encoding/json/marshal.odin26
1 files changed, 26 insertions, 0 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin
index e563c326a..acf12331a 100644
--- a/core/encoding/json/marshal.odin
+++ b/core/encoding/json/marshal.odin
@@ -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 {