mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json2: encode array of all and verify sum type (#17051)
This commit is contained in:
parent
9809427f40
commit
ed58b95a9d
@ -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)!
|
// e.encode_array(val[i], level + 1, mut wr)!
|
||||||
} $else $if U is $Struct {
|
} $else $if U is $Struct {
|
||||||
e.encode_struct(val[i], level + 1, mut wr)!
|
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 {
|
} $else $if U is $Enum {
|
||||||
e.encode_any(i64(val[i]), level + 1, mut wr)!
|
e.encode_any(i64(val[i]), level + 1, mut wr)!
|
||||||
} $else {
|
} $else {
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import x.json2 as json
|
import x.json2 as json
|
||||||
import strings
|
import strings
|
||||||
|
|
||||||
|
struct StructType[T] {
|
||||||
|
mut:
|
||||||
|
val T
|
||||||
|
}
|
||||||
|
|
||||||
fn test_json_string_characters() {
|
fn test_json_string_characters() {
|
||||||
text := json.raw_decode(r'"\n\r\b\f\t\\\"\/"') or { '' }
|
text := json.raw_decode(r'"\n\r\b\f\t\\\"\/"') or { '' }
|
||||||
assert text.json_str() == '"\\n\\r\\b\\f\\t\\\\\\"\\/"'
|
assert text.json_str() == '"\\n\\r\\b\\f\\t\\\\\\"\\/"'
|
||||||
@ -116,7 +121,15 @@ fn test_encode_encodable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_encode_array() {
|
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() {
|
fn test_encode_simple() {
|
||||||
|
@ -79,25 +79,36 @@ pub fn decode[T](src string) !T {
|
|||||||
|
|
||||||
// encode is a generic function that encodes a type into a JSON string.
|
// encode is a generic function that encodes a type into a JSON string.
|
||||||
pub fn encode[T](val T) 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)
|
mut sb := strings.new_builder(64)
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
unsafe { sb.free() }
|
unsafe { sb.free() }
|
||||||
}
|
}
|
||||||
$if T is $Array {
|
|
||||||
mut array_of_any := []Any{}
|
default_encoder.encode_value(val, mut sb) or {
|
||||||
for value in val {
|
dump(err)
|
||||||
array_of_any << value
|
default_encoder.encode_value[Null](null, mut sb) or {}
|
||||||
}
|
|
||||||
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 {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
return sb.str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user