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

Allow deserialization of very large integers #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

logiclrd
Copy link

@logiclrd logiclrd commented Oct 6, 2016

While I was working on a PR for RestSharp, I was reading through the SimpleJson code and discovered that the way in which it parses integers might reject some syntactically-valid JSON documents. In particular, JSON allows for very large integer values to be specified (values large enough that a 64-bit integer cannot represent them) with no punctuation or exponentiation. These values would, in typical implementations, need to be represented as floating-point values (double), but because they lack periods and e signifiers, the SimpleJson code will only attempt to parse them as long. This means that the (valid) JSON string produced by (long.MaxValue + 1M).ToString() cannot be parsed. This PR addresses the issue (using decimal and then double as fallbacks where long fails), and adds corresponding unit tests.

…building a System.String of the number substring.

Reworked the integer parsing path in SimpleJson.cs to try decimal and double parsers if long fails. This allows numbers such as long.MaxValue + 1M to be parsed (yielding a decimal instead of a long). Numbers too large for System.Decimal become System.Double (at, of course, a loss of precision).
Added tests GivenNumberWithoutDecimalTooLargeForLongTypeIsDecimal and GivenNumberWithoutDecimalTooLargeForDecimalTypeIsDouble to JsonDecodeTypeTests.cs.
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.

1 participant