json: fix raw json string decoding crash when expected key is missing (#7206)

This commit is contained in:
Seven Du 2020-12-10 03:10:41 +08:00 committed by GitHub
parent 032ea0f4f8
commit 4a35a75b64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 2 deletions

View file

@ -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 == []
}