x.json2.decoder2: support nesteded maps (#23027)

This commit is contained in:
Hitalo Souza 2024-12-01 08:08:37 -04:00 committed by GitHub
parent e421cb2bc2
commit d8021bd459
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 11 deletions

View File

@ -322,7 +322,7 @@ fn (mut checker Decoder) check_json_format(val string) ! {
}
}
}
if checker.checker_idx < checker_end - 1 {
if checker.checker_idx < checker_end - 2 {
checker.checker_idx++
}
}
@ -748,9 +748,12 @@ fn (mut decoder Decoder) decode_map[K, V](mut val map[K]V) ! {
mut map_value := V{}
decoder.decode_value(mut map_value)!
val[key] = map_value
$if V is $map {
val[key] = map_value.move()
} $else {
val[key] = map_value
}
decoder.decode_value(mut val[key])!
}
}
}

View File

@ -39,14 +39,16 @@ fn test_array_of_strings() {
assert json.decode[map[string]string]('{"val": "2"}')! == {
'val': '2'
}
// assert json.decode[map[string]int]('{"val": 2}')! == {"val": 2}
assert json.decode[map[string]int]('{"val": 2}')! == {
'val': 2
}
// // nested map
// assert json.decode[map[string]map[string]string]('{"val": {"val2": "2"}}')! == {
// 'val': {
// 'val2': '2'
// }
// }
// nested map
assert json.decode[map[string]map[string]string]('{"val": {"val2": "2"}}')! == {
'val': {
'val2': '2'
}
}
// nested struct
assert json.decode[Stru]('{"val": 1, "val2": "lala", "val3": {"a": 2, "brazilian_steak": "leleu"}}')! == Stru{