Skip to content

Commit

Permalink
Deserialize small numbers as integers in arbitrary_precision
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 16, 2022
1 parent 0ca5a69 commit d541381
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/de.rs
Expand Up @@ -864,6 +864,15 @@ impl<'de, R: Read<'de>> Deserializer<R> {
buf.push('-');
}
self.scan_integer(&mut buf)?;
if positive {
if let Ok(unsigned) = buf.parse() {
return Ok(ParserNumber::U64(unsigned));
}
} else {
if let Ok(signed) = buf.parse() {
return Ok(ParserNumber::I64(signed));
}
}
Ok(ParserNumber::String(buf))
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -303,6 +303,7 @@
#![doc(html_root_url = "https://docs.rs/serde_json/1.0.74")]
// Ignored clippy lints
#![allow(
clippy::collapsible_else_if,
clippy::comparison_chain,
clippy::deprecated_cfg_attr,
clippy::doc_markdown,
Expand Down
12 changes: 2 additions & 10 deletions tests/test.rs
Expand Up @@ -715,11 +715,7 @@ fn test_parse_char() {
),
(
"10",
if cfg!(feature = "arbitrary_precision") {
"invalid type: number, expected a character at line 1 column 2"
} else {
"invalid type: integer `10`, expected a character at line 1 column 2"
},
"invalid type: integer `10`, expected a character at line 1 column 2",
),
]);

Expand Down Expand Up @@ -1203,11 +1199,7 @@ fn test_parse_struct() {
test_parse_err::<Outer>(&[
(
"5",
if cfg!(feature = "arbitrary_precision") {
"invalid type: number, expected struct Outer at line 1 column 1"
} else {
"invalid type: integer `5`, expected struct Outer at line 1 column 1"
},
"invalid type: integer `5`, expected struct Outer at line 1 column 1",
),
(
"\"hello\"",
Expand Down

0 comments on commit d541381

Please sign in to comment.