Skip to content

Commit

Permalink
x.json2.decoder2: support nesteded maps (#23027)
Browse files Browse the repository at this point in the history
  • Loading branch information
enghitalo authored Dec 1, 2024
1 parent e421cb2 commit d8021bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 7 additions & 4 deletions vlib/x/json2/decoder2/decode.v
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}
Expand Down Expand Up @@ -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])!
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions vlib/x/json2/decoder2/tests/decode_object_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d8021bd

Please sign in to comment.