From ed58b95a9ddd1f386dd99b0538996e7ff0a42ccb Mon Sep 17 00:00:00 2001 From: Hitalo Souza <63821277+enghitalo@users.noreply.github.com> Date: Wed, 1 Feb 2023 11:52:58 -0300 Subject: [PATCH] json2: encode array of all and verify sum type (#17051) --- vlib/x/json2/encoder.v | 6 ++++++ vlib/x/json2/encoder_test.v | 15 +++++++++++++- vlib/x/json2/json2.v | 39 ++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 98df0e41b..23798a0f6 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -390,6 +390,12 @@ fn (e &Encoder) encode_array[U](val []U, level int, mut wr io.Writer) ! { // e.encode_array(val[i], level + 1, mut wr)! } $else $if U is $Struct { e.encode_struct(val[i], level + 1, mut wr)! + } $else $if U is $Sumtype { + $if U is Any { + e.encode_any(val[i], level + 1, mut wr)! + } $else { + // TODO + } } $else $if U is $Enum { e.encode_any(i64(val[i]), level + 1, mut wr)! } $else { diff --git a/vlib/x/json2/encoder_test.v b/vlib/x/json2/encoder_test.v index 43fbedef5..1033a5fc0 100644 --- a/vlib/x/json2/encoder_test.v +++ b/vlib/x/json2/encoder_test.v @@ -1,6 +1,11 @@ import x.json2 as json import strings +struct StructType[T] { +mut: + val T +} + fn test_json_string_characters() { text := json.raw_decode(r'"\n\r\b\f\t\\\"\/"') or { '' } assert text.json_str() == '"\\n\\r\\b\\f\\t\\\\\\"\\/"' @@ -116,7 +121,15 @@ fn test_encode_encodable() { } fn test_encode_array() { - assert json.encode([1, 2, 3]) == '[1,2,3]' + array_of_struct := [StructType[[]bool]{ + val: [false, true] + }, StructType[[]bool]{ + val: [true, false] + }] + + assert json.encode_array([1, 2, 3]) == '[1,2,3]' + + assert json.encode_array(array_of_struct) == '[{"val":[false,true]},{"val":[true,false]}]' } fn test_encode_simple() { diff --git a/vlib/x/json2/json2.v b/vlib/x/json2/json2.v index 19d654c8b..7090e1e7d 100644 --- a/vlib/x/json2/json2.v +++ b/vlib/x/json2/json2.v @@ -79,25 +79,36 @@ pub fn decode[T](src string) !T { // encode is a generic function that encodes a type into a JSON string. pub fn encode[T](val T) string { + $if T is $Array { + $compile_error('Cannot use `json.encode` to encode array. Try `json.encode_array` instead') + } mut sb := strings.new_builder(64) + defer { unsafe { sb.free() } } - $if T is $Array { - mut array_of_any := []Any{} - for value in val { - array_of_any << value - } - default_encoder.encode_value(array_of_any, mut sb) or { - dump(err) - default_encoder.encode_value[Null](null, mut sb) or {} - } - } $else { - default_encoder.encode_value(val, mut sb) or { - dump(err) - default_encoder.encode_value[Null](null, mut sb) or {} - } + + default_encoder.encode_value(val, mut sb) or { + dump(err) + default_encoder.encode_value[Null](null, mut sb) or {} + } + + return sb.str() +} + +// encode_array is a generic function that encodes a array into a JSON string. +pub fn encode_array[T](val []T) string { + mut sb := strings.new_builder(64) + + defer { + unsafe { sb.free() } } + + default_encoder.encode_array(val, 1, mut sb) or { + dump(err) + default_encoder.encode_value[Null](null, mut sb) or {} + } + return sb.str() } -- 2.30.2