Skip to content

Commit eeb2bcd

Browse files
committed
Align arbitrary_precision number strings with zmij’s formatting
1 parent 8b291c4 commit eeb2bcd

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/de.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
993993
fn scan_number(&mut self, buf: &mut String) -> Result<()> {
994994
match tri!(self.peek_or_null()) {
995995
b'.' => self.scan_decimal(buf),
996-
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
996+
b'e' | b'E' => self.scan_exponent(buf),
997997
_ => Ok(()),
998998
}
999999
}
@@ -1018,26 +1018,32 @@ impl<'de, R: Read<'de>> Deserializer<R> {
10181018
}
10191019

10201020
match tri!(self.peek_or_null()) {
1021-
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
1021+
b'e' | b'E' => self.scan_exponent(buf),
10221022
_ => Ok(()),
10231023
}
10241024
}
10251025

10261026
#[cfg(feature = "arbitrary_precision")]
1027-
fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> {
1027+
fn scan_exponent(&mut self, buf: &mut String) -> Result<()> {
10281028
self.eat_char();
1029-
buf.push(e);
1029+
buf.push('e');
10301030

1031-
match tri!(self.peek_or_null()) {
1031+
let has_sign = match tri!(self.peek_or_null()) {
10321032
b'+' => {
10331033
self.eat_char();
10341034
buf.push('+');
1035+
true
10351036
}
10361037
b'-' => {
10371038
self.eat_char();
10381039
buf.push('-');
1040+
true
10391041
}
1040-
_ => {}
1042+
_ => false,
1043+
};
1044+
1045+
if !has_sign {
1046+
buf.push('+');
10411047
}
10421048

10431049
// Make sure a digit follows the exponent place.

src/number.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,11 @@ impl Number {
279279
Some(Number { n })
280280
}
281281

282-
/// Returns the exact original JSON representation that this Number was
283-
/// parsed from.
282+
/// Returns the JSON representation that this Number was parsed from.
283+
///
284+
/// When parsing with serde_json's `arbitrary_precision` feature enabled,
285+
/// positive exponents are normalized to include an explicit `+` sign so
286+
/// that `1e140` becomes `1e+140`.
284287
///
285288
/// For numbers constructed not via parsing, such as by `From<i32>`, returns
286289
/// the JSON representation that serde\_json would serialize for this

tests/test.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,22 +1037,28 @@ fn test_parse_number() {
10371037

10381038
#[cfg(feature = "arbitrary_precision")]
10391039
test_parse_ok(vec![
1040-
("1e999", Number::from_string_unchecked("1e999".to_owned())),
1040+
("1e999", Number::from_string_unchecked("1e+999".to_owned())),
10411041
("1e+999", Number::from_string_unchecked("1e+999".to_owned())),
1042-
("-1e999", Number::from_string_unchecked("-1e999".to_owned())),
1042+
(
1043+
"-1e999",
1044+
Number::from_string_unchecked("-1e+999".to_owned()),
1045+
),
10431046
("1e-999", Number::from_string_unchecked("1e-999".to_owned())),
1044-
("1E999", Number::from_string_unchecked("1E999".to_owned())),
1045-
("1E+999", Number::from_string_unchecked("1E+999".to_owned())),
1046-
("-1E999", Number::from_string_unchecked("-1E999".to_owned())),
1047-
("1E-999", Number::from_string_unchecked("1E-999".to_owned())),
1048-
("1E+000", Number::from_string_unchecked("1E+000".to_owned())),
1047+
("1E999", Number::from_string_unchecked("1e+999".to_owned())),
1048+
("1E+999", Number::from_string_unchecked("1e+999".to_owned())),
1049+
(
1050+
"-1E999",
1051+
Number::from_string_unchecked("-1e+999".to_owned()),
1052+
),
1053+
("1E-999", Number::from_string_unchecked("1e-999".to_owned())),
1054+
("1E+000", Number::from_string_unchecked("1e+000".to_owned())),
10491055
(
10501056
"2.3e999",
1051-
Number::from_string_unchecked("2.3e999".to_owned()),
1057+
Number::from_string_unchecked("2.3e+999".to_owned()),
10521058
),
10531059
(
10541060
"-2.3e999",
1055-
Number::from_string_unchecked("-2.3e999".to_owned()),
1061+
Number::from_string_unchecked("-2.3e+999".to_owned()),
10561062
),
10571063
]);
10581064
}

0 commit comments

Comments
 (0)