mirror of
https://github.com/serde-rs/json.git
synced 2026-02-19 06:29:08 +00:00
Align arbitrary_precision number strings with zmij’s formatting
This commit is contained in:
parent
8b291c4c56
commit
eeb2bcd3f2
18
src/de.rs
18
src/de.rs
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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()),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user