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

Fix number deserialization with arbitrary_precision #586

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ raw_value = []
# overflow the stack after deserialization has completed, including, but not
# limited to, Display and Debug and Drop impls.
unbounded_depth = []

[patch.crates-io]
serde = { git = "https://github.com/bkchr/serde.git", branch = "bkchr-tagged-content-u128-i128" }
41 changes: 38 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use read::{self, Reference};
pub use read::{IoRead, Read, SliceRead, StrRead};

use number::Number;
#[cfg(feature = "arbitrary_precision")]
use number::NumberDeserializer;

//////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -107,6 +105,43 @@ pub enum ParserNumber {
String(String),
}

#[cfg(feature = "arbitrary_precision")]
impl ParserNumber {
fn parse_arbitrary_precision<'de, V>(number: &str, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
let err = || Err(super::number::invalid_number());

serde_if_integer128! {
let res = if let Some(val) = number.parse::<i128>().ok() {
visitor.visit_i128(val)
} else if let Some(val) = number.parse::<u128>().ok() {
visitor.visit_u128(val)
} else if let Some(val) = number.parse::<f64>().ok() {
visitor.visit_f64(val)
} else {
err()
};

return res;
}

#[allow(unreachable_code)]
{
if let Some(val) = number.parse::<i64>().ok() {
visitor.visit_i64(val)
} else if let Some(val) = number.parse::<u64>().ok() {
visitor.visit_u64(val)
} else if let Some(val) = number.parse::<f64>().ok() {
visitor.visit_f64(val)
} else {
err()
}
}
}
}

impl ParserNumber {
fn visit<'de, V>(self, visitor: V) -> Result<V::Value>
where
Expand All @@ -117,7 +152,7 @@ impl ParserNumber {
ParserNumber::U64(x) => visitor.visit_u64(x),
ParserNumber::I64(x) => visitor.visit_i64(x),
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(x) => visitor.visit_map(NumberDeserializer { number: x.into() }),
ParserNumber::String(x) => Self::parse_arbitrary_precision(&x, visitor),
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,6 @@ impl Number {
None
}
}

#[cfg(feature = "arbitrary_precision")]
/// Not public API. Only tests use this.
#[doc(hidden)]
#[inline]
pub fn from_string_unchecked(n: String) -> Self {
Number { n: n }
}
}

impl fmt::Display for Number {
Expand Down Expand Up @@ -444,7 +436,7 @@ impl<'de> de::Deserialize<'de> for NumberFromString {
}

#[cfg(feature = "arbitrary_precision")]
fn invalid_number() -> Error {
pub(crate) fn invalid_number() -> Error {
Error::syntax(ErrorCode::InvalidNumber, 0, 0)
}

Expand Down
13 changes: 13 additions & 0 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ impl<'de> Deserialize<'de> for Value {
Ok(Value::Number(value.into()))
}

#[cfg(feature = "arbitrary_precision")]
serde_if_integer128! {
#[inline]
fn visit_i128<E>(self, value: i128) -> Result<Value, E> {
Ok(Value::Number(value.into()))
}

#[inline]
fn visit_u128<E>(self, value: u128) -> Result<Value, E> {
Ok(Value::Number(value.into()))
}
}

#[inline]
fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
Ok(Number::from_f64(value).map_or(Value::Null, Value::Number))
Expand Down
22 changes: 22 additions & 0 deletions tests/regression/issue505.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde_derive::Deserialize;

#[derive(Deserialize)]
struct Data {
_value: i32,
_value2: i128,
}

#[derive(Deserialize)]
#[serde(tag = "type")]
enum Wrapper {
Data(Data),
}

#[test]
fn test() {
let json = r#"{"type":"Data","_value":123,"_value2":123244235436463}"#;
// Okay
let _data1: Data = serde_json::from_str(json).unwrap();
// Fails!
let _data2: Wrapper = serde_json::from_str(json).unwrap();
}
15 changes: 0 additions & 15 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,21 +956,6 @@ fn test_parse_number() {
("\t1.0", "invalid number at line 1 column 1"),
("1.0\t", "invalid number at line 1 column 4"),
]);

#[cfg(feature = "arbitrary_precision")]
Copy link
Author

Choose a reason for hiding this comment

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

I removed this test + the extra added function from_string_unchecked. In my opinion this test does not makes that much sense at all. Even if we would have parsed the 1e999 correctly into the Number, there is not way to get this value out of the object.

test_parse_ok(vec![
("1e999", Number::from_string_unchecked("1e999".to_owned())),
("-1e999", Number::from_string_unchecked("-1e999".to_owned())),
("1e-999", Number::from_string_unchecked("1e-999".to_owned())),
(
"2.3e999",
Number::from_string_unchecked("2.3e999".to_owned()),
),
(
"-2.3e999",
Number::from_string_unchecked("-2.3e999".to_owned()),
),
]);
}

#[test]
Expand Down