mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: fix json decode/encode with option type (#17393)
This commit is contained in:
68
vlib/json/json_option_test.v
Normal file
68
vlib/json/json_option_test.v
Normal file
@@ -0,0 +1,68 @@
|
||||
import json
|
||||
|
||||
pub struct MyStruct {
|
||||
pub mut:
|
||||
valuea int
|
||||
}
|
||||
|
||||
pub struct MyStruct2 {
|
||||
pub mut:
|
||||
valuea int
|
||||
valueb ?MyStruct
|
||||
}
|
||||
|
||||
struct Node {
|
||||
location NodeLocation [json: 'loc']
|
||||
}
|
||||
|
||||
struct NodeLocation {
|
||||
source_file ?SourceFile [json: 'includedFrom']
|
||||
}
|
||||
|
||||
struct SourceFile {
|
||||
path string [json: 'file']
|
||||
}
|
||||
|
||||
fn test_encode_decode() {
|
||||
assert json.encode(MyStruct2{ valuea: 1 }) == '{"valuea":1}'
|
||||
|
||||
assert json.decode(MyStruct2, '{"valuea": 1}')! == MyStruct2{
|
||||
valuea: 1
|
||||
valueb: none
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode_decode2() {
|
||||
assert json.encode(MyStruct2{ valuea: 1, valueb: none }) == '{"valuea":1}'
|
||||
|
||||
assert json.decode(MyStruct2, '{"valuea": 1}')! == MyStruct2{
|
||||
valuea: 1
|
||||
valueb: none
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode_decode3() {
|
||||
assert json.encode(MyStruct2{
|
||||
valuea: 1
|
||||
valueb: MyStruct{
|
||||
valuea: 123
|
||||
}
|
||||
}) == '{"valuea":1,"valueb":{"valuea":123}}'
|
||||
|
||||
assert json.decode(MyStruct2, '{"valuea": 1}')! == MyStruct2{
|
||||
valuea: 1
|
||||
valueb: none
|
||||
}
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
node := json.decode(Node, '{"loc": { "includedFrom": { "file": "/bin/foo" } } }')!
|
||||
|
||||
source_file := node.location.source_file or {
|
||||
SourceFile{
|
||||
path: '-'
|
||||
}
|
||||
}
|
||||
|
||||
assert source_file.path == '/bin/foo'
|
||||
}
|
||||
Reference in New Issue
Block a user