From 4d4d76d65bc45d49117cf85fd086ad5401a69fda Mon Sep 17 00:00:00 2001 From: Hitalo Souza <63821277+enghitalo@users.noreply.github.com> Date: Mon, 10 Apr 2023 12:57:02 -0300 Subject: [PATCH] json2: fix encoding of nested maps like map[string]map[string]int (#17924) --- vlib/x/json2/encode_struct_test.v | 14 +++++------ vlib/x/json2/encoder.v | 40 +++++++++++++++++-------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/vlib/x/json2/encode_struct_test.v b/vlib/x/json2/encode_struct_test.v index bcc942a6f9..a1274285bd 100644 --- a/vlib/x/json2/encode_struct_test.v +++ b/vlib/x/json2/encode_struct_test.v @@ -265,11 +265,11 @@ fn test_maps() { '1': 1 } }) == '{"val":{"1":1}}' - // assert json.encode(StructType[map[string]map[string]int]{ - // val: { - // 'a': { - // '1': 1 - // } - // } - // }) == '{"val":{"a":{"1":1}}}' + assert json.encode(StructType[map[string]map[string]int]{ + val: { + 'a': { + '1': 1 + } + } + }) == '{"val":{"a":{"1":1}}}' } diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 2b200e172e..4014c181a9 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -124,6 +124,26 @@ fn (e &Encoder) encode_any(val Any, level int, mut wr io.Writer) ! { } } +fn (e &Encoder) encode_map[T](value T, level int, mut wr io.Writer) ! { + wr.write(json2.curly_open)! + mut idx := 0 + for k, v in value { + e.encode_newline(level, mut wr)! + e.encode_string(k.str(), mut wr)! + wr.write(json2.colon_bytes)! + if e.newline != 0 { + wr.write(json2.space_bytes)! + } + e.encode_value_with_level(v, level + 1, mut wr)! + if idx < value.len - 1 { + wr.write(json2.comma_bytes)! + } + idx++ + } + e.encode_newline(level, mut wr)! + wr.write(json2.curly_close)! +} + fn (e &Encoder) encode_value_with_level[T](val T, level int, mut wr io.Writer) ! { $if T is string { e.encode_string(val, mut wr)! @@ -133,7 +153,7 @@ fn (e &Encoder) encode_value_with_level[T](val T, level int, mut wr io.Writer) ! // weird quirk but val is destructured immediately to Any e.encode_any(val, level, mut wr)! } $else $if T is $map { - // FIXME - `e.encode_struct` can not encode `map[string]map[string]int` type + e.encode_map(val, level, mut wr)! } $else $if T is []Any { e.encode_any(val, level, mut wr)! } $else $if T is Encodable { @@ -261,23 +281,7 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! { } $else $if field.typ is $struct { e.encode_struct(value, level + 1, mut wr)! } $else $if field.is_map { - wr.write(json2.curly_open)! - mut idx := 0 - for k, v in value { - e.encode_newline(level, mut wr)! - e.encode_string(k.str(), mut wr)! - wr.write(json2.colon_bytes)! - if e.newline != 0 { - wr.write(json2.space_bytes)! - } - e.encode_value_with_level(v, level + 1, mut wr)! - if idx < value.len - 1 { - wr.write(json2.comma_bytes)! - } - idx++ - } - e.encode_newline(level, mut wr)! - wr.write(json2.curly_close)! + e.encode_map(value, level + 1, mut wr)! } $else $if field.is_enum { // TODO - replace for `field.typ is $enum` wr.write(int(val.$(field.name)).str().bytes())!