Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check length before converting to range type #102

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion json_serialization.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
mode = ScriptMode.Verbose

packageName = "json_serialization"
version = "0.2.8"
version = "0.2.9"
author = "Status Research & Development GmbH"
description = "Flexible JSON serialization not relying on run-time type information"
license = "Apache License 2.0"
Expand Down
8 changes: 4 additions & 4 deletions json_serialization/reader_impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ proc readValue*[T](r: var JsonReader, value: var T)
elif value is array:
type IDX = typeof low(value)
r.parseArray(idx):
let i = IDX(idx + low(value).int)
if i <= high(value):
# TODO: dont's ask. this makes the code compile
if false: value[i] = value[i]
if idx < value.len:
let i = IDX(idx + low(value).int)
readValue(r, value[i])
else:
r.raiseUnexpectedValue("Too many items for " & $(typeof(value)))

elif value is (object or tuple):
mixin flavorUsesAutomaticObjectSerialization
Expand Down
9 changes: 9 additions & 0 deletions tests/test_reader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,12 @@ suite "JsonReader basic test":

var z = toReaderNullFields("""{"something":null,"bool":999,"string":100}""")
check execReadObject(z) == 2

test "readValue of array":
var r = toReader "[false, true, false]"
check r.readValue(array[3, bool]) == [false, true, false]

test "readValue of array error":
var r = toReader "[false, true, false]"
expect JsonReaderError:
discard r.readValue(array[2, bool])