Skip to content

serialize: add json::{Integer,Floating} to parse large integers properly #16201

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

Merged
merged 3 commits into from
Aug 19, 2014

Conversation

erickt
Copy link
Contributor

@erickt erickt commented Aug 2, 2014

This patch allows json to deserialize integers larger than 2^53 without losing precision. It does this by first keeping the integer portion of a number as a i64, and only casting it over to a f64 if we have a decimal or exponent.

@apoelstra
Copy link
Contributor

I really like this, it is my biggest frustration with many C Json libraries that they do not handle integers precisely when the underlying language is perfectly able to.

@apoelstra
Copy link
Contributor

There is another problem with Json-decoded integers: if you try to read an unsigned integer, it'll read an f64 and cast this to a uint, resulting in wraparound. I think the decoder ought to check for negative numbers and signal an error.

Maybe this is a job for another PR though?

@erickt
Copy link
Contributor Author

erickt commented Aug 4, 2014

@apoelstra: Right now this patch can properly deserialize i64s, although it doesn't check for overflow. I should probably add that. I could also add support for u64s, which would let us get that last bit of size.

@apoelstra
Copy link
Contributor

@erickt Full-range support for u64's would be awesome, though personally I would only use it for the side-effect of rejecting negative numbers :)

@erickt
Copy link
Contributor Author

erickt commented Aug 7, 2014

This is ready again for review. I added support for u64, added bounds checks, and added a couple tests for boundary conditions.

@apoelstra
Copy link
Contributor

This looks great -- I like your use of std::num::cast to do range checks, I didn't know about that one.

macro_rules! read_primitive {
($name:ident, $ty:ty) => {
fn $name(&mut self) -> DecodeResult<$ty> {
println!("$name {}", self.stack);
Copy link
Member

Choose a reason for hiding this comment

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

stray println?

@alexcrichton
Copy link
Member

Sorry for the delay @erickt, but looks good to me! r=me with two minor things addressed.

bors added a commit that referenced this pull request Aug 19, 2014
This patch allows json to deserialize integers larger than 2^53 without losing precision. It does this by first keeping the integer portion of a number as a `i64`, and only casting it over to a `f64` if we have a decimal or exponent.
@bors bors closed this Aug 19, 2014
@bors bors merged commit 9b23287 into rust-lang:master Aug 19, 2014
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.

4 participants