mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
json: fix raw json string decoding crash when expected key is missing (#7206)
This commit is contained in:
parent
032ea0f4f8
commit
4a35a75b64
3 changed files with 59 additions and 2 deletions
|
@ -111,6 +111,15 @@ fn test_raw_json_field() {
|
|||
assert color.space == 'YCbCr'
|
||||
}
|
||||
|
||||
fn test_bad_raw_json_field() {
|
||||
color := json.decode(Color, '{"space": "YCbCr"}') or {
|
||||
println('text')
|
||||
return
|
||||
}
|
||||
assert color.point == ''
|
||||
assert color.space == 'YCbCr'
|
||||
}
|
||||
|
||||
struct City {
|
||||
name string
|
||||
}
|
||||
|
@ -310,3 +319,35 @@ fn test_encode_alias_struct() {
|
|||
out := json.encode(msg)
|
||||
assert out == expected
|
||||
}
|
||||
|
||||
struct List {
|
||||
id int
|
||||
items []string
|
||||
}
|
||||
|
||||
fn test_list() {
|
||||
list := json.decode(List, '{"id": 1, "items": ["1", "2"]}') or {
|
||||
println('error')
|
||||
return
|
||||
}
|
||||
assert list.id == 1
|
||||
assert list.items == ["1", "2"]
|
||||
}
|
||||
|
||||
fn test_list_no_id() {
|
||||
list := json.decode(List, '{"items": ["1", "2"]}') or {
|
||||
println('error')
|
||||
return
|
||||
}
|
||||
assert list.id == 0
|
||||
assert list.items == ["1", "2"]
|
||||
}
|
||||
|
||||
fn test_list_no_items() {
|
||||
list := json.decode(List, '{"id": 1}') or {
|
||||
println('error')
|
||||
return
|
||||
}
|
||||
assert list.id == 1
|
||||
assert list.items == []
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue