From 425dec8bb8cc4fe2cf25a008de199d3084ecb510 Mon Sep 17 00:00:00 2001 From: Michael Kutowski Date: Mon, 8 Aug 2022 18:28:28 +0200 Subject: [PATCH] add uint as hex option --- core/encoding/json/marshal.odin | 31 +++++++++++++++++++++---------- 1 file 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 }