aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMichael Kutowski <skytrias@protonmail.com>2022-08-08 18:28:28 +0200
committerGitHub <noreply@github.com>2022-08-08 18:28:28 +0200
commit425dec8bb8cc4fe2cf25a008de199d3084ecb510 (patch)
tree97f756abdbf950351c19197b4118ed7d852fbe15 /core
parent53e0d182af4ec8858a8162aa62e7a7f8726fd131 (diff)
add uint as hex option
Diffstat (limited to 'core')
-rw-r--r--core/encoding/json/marshal.odin31
1 files changed, 21 insertions, 10 deletions
diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin
index 095d290f5..2a92cec28 100644
--- a/core/encoding/json/marshal.odin
+++ b/core/encoding/json/marshal.odin
@@ -17,7 +17,7 @@ Marshal_Error :: union #shared_nil {
io.Error,
}
-// supports json specs
+// NOTE careful with MJSON maps & non quotes usage as keys without whitespace will lead to bad results
Marshal_Options :: struct {
// output based on spec
spec: Specification,
@@ -28,11 +28,13 @@ Marshal_Options :: struct {
// spacing
use_spaces: bool,
spaces: int,
- tabs: int,
// state
indentation: int,
+ // option to output uint in JSON5 & MJSON
+ write_uint_as_hex: bool,
+
// mjson output options
mjson_keys_use_quotes: bool,
mjson_keys_use_equal_sign: bool,
@@ -109,7 +111,23 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
case u128be: u = u128(i)
}
- s := strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
+ s: string
+
+ // allow uints to be printed as hex
+ if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) {
+ switch i in a {
+ case u8, u16, u32, u64, u128: {
+ s = strconv.append_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })
+ }
+
+ case: {
+ s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
+ }
+ }
+ } else {
+ s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
+ }
+
io.write_string(w, s) or_return
@@ -261,18 +279,11 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
#partial switch info in ti.variant {
case runtime.Type_Info_String: {
- // fmt.eprintln("WAS STRING")
-
switch s in a {
case string: name = s
case cstring: name = string(s)
}
- // NOTE need to ensure that map keys are valid for mjson and contain no whitespace
- if opt.spec == .MJSON && !opt.mjson_keys_use_quotes {
- name, _ = strings.replace_all(name, " ", "_", context.temp_allocator)
- }
-
opt_write_key(w, opt, name) or_return
}