Skip to content

Conversation

ire4ever1190
Copy link
Contributor

@ire4ever1190 ire4ever1190 commented Jan 3, 2022

This currently fails

import jsony

let json = """[{"name": "John"}]"""

echo json.fromJson(seq[tuple[name: string]])

with message

Error: unhandled exception: Expected ] but got } instead. At offset: 16 [JsonError]

Issue was parseObject missing a final eatChar for the closing brace.
Fixed by copying the final bits of the parseHook for normal objects

@ire4ever1190
Copy link
Contributor Author

ire4ever1190 commented Jan 3, 2022

The loop in parseObject

jsony/src/jsony.nim

Lines 335 to 359 in c845657

while i < s.len:
eatSpace(s, i)
if i < s.len and s[i] == '}':
break
var key: string
parseHook(s, i, key)
eatChar(s, i, ':')
when compiles(renameHook(v, key)):
renameHook(v, key)
block all:
for k, v in v.fieldPairs:
if k == key or snakeCase(k) == key:
var v2: type(v)
parseHook(s, i, v2)
v = v2
break all
skipValue(s, i)
eatSpace(s, i)
if i < s.len and s[i] == ',':
inc i
else:
break
when compiles(postHook(v)):
postHook(v)
eatChar(s, i, '}')
is the same as the final loop in parseHook for objects

jsony/src/jsony.nim

Lines 429 to 453 in c845657

while i < s.len:
eatSpace(s, i)
if i < s.len and s[i] == '}':
break
var key: string
parseHook(s, i, key)
eatChar(s, i, ':')
when compiles(renameHook(v, key)):
renameHook(v, key)
block all:
for k, v in v.fieldPairs:
if k == key or snakeCase(k) == key:
var v2: type(v)
parseHook(s, i, v2)
v = v2
break all
skipValue(s, i)
eatSpace(s, i)
if i < s.len and s[i] == ',':
inc i
else:
break
when compiles(postHook(v)):
postHook(v)
eatChar(s, i, '}')

Might it be better if I instead use a template and call it in both places so they stay in sync?

@treeform treeform merged commit eb8b14f into treeform:master Jan 3, 2022
@treeform
Copy link
Owner

treeform commented Jan 3, 2022

Looks like a good PR! Thanks!

I don't want to use templates to save some code duplication. Maybe there is a way to pass both tuples and obj to same function.

@treeform
Copy link
Owner

treeform commented Jan 3, 2022

I have fixed the code duplication here: 37d5268

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants