Skip to content

Conversation

keynmol
Copy link
Contributor

@keynmol keynmol commented Aug 29, 2022

Closes #16

I also removed the Try approach because, perhaps naively, on something like id decoding we can save allocations (but not on exception throwing)

Comment on lines 13 to 25
try {
NumberId(in.readLong())
} catch {
case _: JsonReaderException =>
in.rollbackToken()
try {
StringId(in.readString(null))
} catch {
case _: JsonReaderException =>
in.readNullOrError(default, "expected null")
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plokhotnyuk is this the way to write codecs like this? I'm not super familiar with jsoniter, so might need some guidance.

NumberId(in.readLong())
} catch {
case _: JsonReaderException =>
in.rollbackToken()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key thing - 1 token is already consumed, it's the wrong kind (not number), so we need to roll back, so that a string can be read

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, yeah, my bad. I think the implementation is fine, though you could optimise by using isNextToken to check whether the first char is a '"' or 'n'and dispatch to readString, readNull or readLong depending on the result. Not sure you need to rollback after peeking at the next token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - I was originally thinking to reject it because both nextToken and isNextToken actually move the pointer (there's no peeking it seems), but implementation in c9f19e6 does the same thing and doesn't rely on costly exceptions.

@Baccata Baccata merged commit 6f33c7c into main Aug 29, 2022
@keynmol keynmol deleted the fix-callid-codec branch August 29, 2022 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants