mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix json encode struct with optional field (#16866)
This commit is contained in:
20
vlib/json/json_encode_struct_with_optional_field_test.v
Normal file
20
vlib/json/json_encode_struct_with_optional_field_test.v
Normal file
@ -0,0 +1,20 @@
|
||||
import json
|
||||
|
||||
struct Foo {
|
||||
name string
|
||||
num ?int
|
||||
}
|
||||
|
||||
fn test_json_encode_struct_with_optional_field() {
|
||||
f1 := Foo{
|
||||
name: 'hello'
|
||||
}
|
||||
ret1 := json.encode(f1)
|
||||
println(ret1)
|
||||
assert ret1 == '{"name":"hello","num":null}'
|
||||
|
||||
f2 := Foo{'hello', 22}
|
||||
ret2 := json.encode(f2)
|
||||
println(ret2)
|
||||
assert ret2 == '{"name":"hello","num":22}'
|
||||
}
|
Reference in New Issue
Block a user