-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
Conversation
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. |
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? |
@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. |
@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 :) |
This is ready again for review. I added support for |
This looks great -- I like your use of |
macro_rules! read_primitive { | ||
($name:ident, $ty:ty) => { | ||
fn $name(&mut self) -> DecodeResult<$ty> { | ||
println!("$name {}", self.stack); |
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.
stray println?
Sorry for the delay @erickt, but looks good to me! r=me with two minor things addressed. |
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.
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 af64
if we have a decimal or exponent.