Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

sandwwraith
Copy link
Member

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

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
@sandwwraith sandwwraith requested a review from shanshin November 8, 2024 14:59
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

println?

Copy link
Member Author

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

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)
Copy link
Contributor

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?

Copy link
Member Author

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")
Copy link
Contributor

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?

Copy link
Member Author

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 SerializationExceptions are accepted

@sandwwraith sandwwraith merged commit 21c9e97 into dev Nov 19, 2024
4 checks passed
@sandwwraith sandwwraith deleted the json-primitive-parse-num branch November 19, 2024 16:22
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