-
Notifications
You must be signed in to change notification settings - Fork 635
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
Correctly parse invalid numbers in JsonLiteral.long and other extensions #2852
Conversation
Content must be consumed fully, with no leftovers after number. Also simplify try/catching logic — JsonLiteral.long should throw NumberFormatException, while decoding from JsonElement should throw JsonDecodingException Fixes #2849
val str = """{"a": "3 digit then random string"}""" | ||
val obj = Json.decodeFromString<JsonObject>(str) | ||
assertFailsWithMessage<NumberFormatException>("Expected input to contain a single valid number") { | ||
println(obj.getValue("a").jsonPrimitive.long) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
println?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll throw exception before that anyway
// Slow path, never called in normal code, can avoid optimizing it | ||
val expected = tokenDescription(expectedToken) | ||
val position = if (wasConsumed) currentPosition - 1 else currentPosition | ||
val s = if (currentPosition == source.length || position < 0) "EOF" else source[position].toString() | ||
fail("Expected $expected, but had '$s' instead", position) | ||
fail(message(expected, s), position) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it expected to print in exception only first wrong character?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because generally, we do not know where input ends (e.g. in the case of {"a": 12312:xdwadaw213123123413.....}
, incorrect part may potentially be as long as whole source itself
checkSerializationException({ | ||
default.decodeFromString(OuterLong.serializer(), """{"a":"12:34:45"}""", mode) | ||
}, { | ||
if (mode == JsonTestingMode.TREE) assertContains(it, "Failed to parse literal '\"12:34:45\"' as a long value at element: \$.a") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check exception type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkSerializationException
makes sure that only SerializationException
s are accepted
Content must be consumed fully, with no leftovers after number. Also simplify try/catching logic — JsonLiteral.long should throw NumberFormatException, while decoding from JsonElement should throw JsonDecodingException
Fixes #2849