mirror of
https://github.com/serde-rs/json.git
synced 2026-01-20 07:35:50 +00:00
if f32 was requested use 32-bit minimal-lexical
instead of using the 64-bit version and then casting down to f32
This commit is contained in:
parent
e004665bab
commit
0d97e08568
20
src/de.rs
20
src/de.rs
@ -23,6 +23,7 @@ pub struct Deserializer<R> {
|
||||
read: R,
|
||||
scratch: Vec<u8>,
|
||||
remaining_depth: u8,
|
||||
requested_f32: bool,
|
||||
#[cfg(feature = "unbounded_depth")]
|
||||
disable_recursion_limit: bool,
|
||||
}
|
||||
@ -43,6 +44,7 @@ where
|
||||
#[cfg(not(feature = "unbounded_depth"))]
|
||||
{
|
||||
Deserializer {
|
||||
requested_f32: false,
|
||||
read: read,
|
||||
scratch: Vec::new(),
|
||||
remaining_depth: 128,
|
||||
@ -52,6 +54,7 @@ where
|
||||
#[cfg(feature = "unbounded_depth")]
|
||||
{
|
||||
Deserializer {
|
||||
requested_f32: false,
|
||||
read: read,
|
||||
scratch: Vec::new(),
|
||||
remaining_depth: 128,
|
||||
@ -743,7 +746,11 @@ impl<'de, R: Read<'de>> Deserializer<R> {
|
||||
let integer = &self.scratch[..integer_end];
|
||||
let fraction = &self.scratch[integer_end..];
|
||||
|
||||
let f: f64 = minimal_lexical::parse_float(integer.iter(), fraction.iter(), exponent);
|
||||
let f = if self.requested_f32 {
|
||||
minimal_lexical::parse_float::<f32, _, _>(integer.iter(), fraction.iter(), exponent) as f64
|
||||
} else {
|
||||
minimal_lexical::parse_float::<f64, _, _>(integer.iter(), fraction.iter(), exponent)
|
||||
};
|
||||
self.scratch.clear();
|
||||
|
||||
if f.is_infinite() {
|
||||
@ -1161,9 +1168,18 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
|
||||
deserialize_prim_number!(deserialize_u16);
|
||||
deserialize_prim_number!(deserialize_u32);
|
||||
deserialize_prim_number!(deserialize_u64);
|
||||
deserialize_prim_number!(deserialize_f32);
|
||||
deserialize_prim_number!(deserialize_f64);
|
||||
|
||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
self.requested_f32 = true;
|
||||
let val = self.deserialize_any(visitor);
|
||||
self.requested_f32 = false;
|
||||
val
|
||||
}
|
||||
|
||||
serde_if_integer128! {
|
||||
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user