Align arbitrary_precision number strings with zmij’s formatting

This commit is contained in:
baishen 2026-01-06 16:16:52 +08:00
parent 8b291c4c56
commit eeb2bcd3f2
3 changed files with 32 additions and 17 deletions

View File

@ -993,7 +993,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
fn scan_number(&mut self, buf: &mut String) -> Result<()> {
match tri!(self.peek_or_null()) {
b'.' => self.scan_decimal(buf),
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
b'e' | b'E' => self.scan_exponent(buf),
_ => Ok(()),
}
}
@ -1018,26 +1018,32 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
match tri!(self.peek_or_null()) {
e @ (b'e' | b'E') => self.scan_exponent(e as char, buf),
b'e' | b'E' => self.scan_exponent(buf),
_ => Ok(()),
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_exponent(&mut self, e: char, buf: &mut String) -> Result<()> {
fn scan_exponent(&mut self, buf: &mut String) -> Result<()> {
self.eat_char();
buf.push(e);
buf.push('e');
match tri!(self.peek_or_null()) {
let has_sign = match tri!(self.peek_or_null()) {
b'+' => {
self.eat_char();
buf.push('+');
true
}
b'-' => {
self.eat_char();
buf.push('-');
true
}
_ => {}
_ => false,
};
if !has_sign {
buf.push('+');
}
// Make sure a digit follows the exponent place.

View File

@ -279,8 +279,11 @@ impl Number {
Some(Number { n })
}
/// Returns the exact original JSON representation that this Number was
/// parsed from.
/// Returns the JSON representation that this Number was parsed from.
///
/// When parsing with serde_json's `arbitrary_precision` feature enabled,
/// positive exponents are normalized to include an explicit `+` sign so
/// that `1e140` becomes `1e+140`.
///
/// For numbers constructed not via parsing, such as by `From<i32>`, returns
/// the JSON representation that serde\_json would serialize for this

View File

@ -1037,22 +1037,28 @@ fn test_parse_number() {
#[cfg(feature = "arbitrary_precision")]
test_parse_ok(vec![
("1e999", Number::from_string_unchecked("1e999".to_owned())),
("1e999", Number::from_string_unchecked("1e+999".to_owned())),
("1e+999", Number::from_string_unchecked("1e+999".to_owned())),
("-1e999", Number::from_string_unchecked("-1e999".to_owned())),
(
"-1e999",
Number::from_string_unchecked("-1e+999".to_owned()),
),
("1e-999", Number::from_string_unchecked("1e-999".to_owned())),
("1E999", Number::from_string_unchecked("1E999".to_owned())),
("1E+999", Number::from_string_unchecked("1E+999".to_owned())),
("-1E999", Number::from_string_unchecked("-1E999".to_owned())),
("1E-999", Number::from_string_unchecked("1E-999".to_owned())),
("1E+000", Number::from_string_unchecked("1E+000".to_owned())),
("1E999", Number::from_string_unchecked("1e+999".to_owned())),
("1E+999", Number::from_string_unchecked("1e+999".to_owned())),
(
"-1E999",
Number::from_string_unchecked("-1e+999".to_owned()),
),
("1E-999", Number::from_string_unchecked("1e-999".to_owned())),
("1E+000", Number::from_string_unchecked("1e+000".to_owned())),
(
"2.3e999",
Number::from_string_unchecked("2.3e999".to_owned()),
Number::from_string_unchecked("2.3e+999".to_owned()),
),
(
"-2.3e999",
Number::from_string_unchecked("-2.3e999".to_owned()),
Number::from_string_unchecked("-2.3e+999".to_owned()),
),
]);
}