Skip to content

Commit 4b7d2c0

Browse files
committed
Deserialize u128 and i128 as numbers
1 parent 765f7ea commit 4b7d2c0

File tree

2 files changed

+24
-55
lines changed

2 files changed

+24
-55
lines changed

src/de/map.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ impl<'de, 'a> de::Deserializer<'de> for MapKey<'a, 'de> {
160160
where
161161
V: Visitor<'de>,
162162
{
163-
// default implementation includes string unparsing
164-
self.de.deserialize_i128(visitor)
163+
deserialize_signed_key!(self, visitor, i128, visit_i128)
165164
}
166165

167166
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@@ -196,7 +195,7 @@ impl<'de, 'a> de::Deserializer<'de> for MapKey<'a, 'de> {
196195
where
197196
V: Visitor<'de>,
198197
{
199-
self.de.deserialize_u128(visitor)
198+
deserialize_unsigned_key!(self, visitor, u128, visit_u128)
200199
}
201200

202201
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>

src/de/mod.rs

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
358358
where
359359
V: Visitor<'de>,
360360
{
361-
match self.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
362-
b'"' => self.eat_char(),
363-
_ => return Err(Error::InvalidType),
364-
};
365-
366-
let result = match self.peek() {
367-
// after rust merged or-patterns feature, these two clause can be merged.
368-
// error[E0658]: or-patterns syntax is experimental
369-
Some(b'0'..=b'9') => deserialize_signed!(self, visitor, i128, visit_i128),
370-
Some(b'-') => deserialize_signed!(self, visitor, i128, visit_i128),
371-
_ => return Err(Error::InvalidType),
372-
};
373-
match self.peek() {
374-
Some(b'"') => {
375-
self.eat_char();
376-
result
377-
}
378-
_ => Err(Error::InvalidType),
379-
}
361+
deserialize_signed!(self, visitor, i128, visit_i128)
380362
}
381363

382364
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
@@ -411,25 +393,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
411393
where
412394
V: Visitor<'de>,
413395
{
414-
match self.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
415-
b'"' => {
416-
self.eat_char();
417-
}
418-
_ => return Err(Error::InvalidType),
419-
};
420-
421-
let result = match self.peek() {
422-
Some(b'-') => return Err(Error::InvalidNumber),
423-
Some(b'0'..=b'9') => deserialize_unsigned!(self, visitor, u128, visit_u128),
424-
_ => return Err(Error::InvalidType),
425-
};
426-
match self.peek() {
427-
Some(b'"') => {
428-
self.eat_char();
429-
result
430-
}
431-
_ => Err(Error::InvalidType),
432-
}
396+
deserialize_unsigned!(self, visitor, u128, visit_u128)
433397
}
434398

435399
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
@@ -755,43 +719,49 @@ mod tests {
755719

756720
#[test]
757721
fn integer128() {
758-
assert_eq!(from_str::<i128>(r#"0"#), Err(crate::de::Error::InvalidType));
759-
assert_eq!(from_str::<i128>(r#""0""#), Ok(0));
760-
assert_eq!(from_str::<i128>(r#""1""#), Ok(1));
761-
assert_eq!(from_str::<i128>(r#""-1""#), Ok(-1));
722+
assert_eq!(
723+
from_str::<i128>(r#""0""#),
724+
Err(crate::de::Error::InvalidType)
725+
);
726+
assert_eq!(from_str::<i128>(r#"0"#), Ok(0));
727+
assert_eq!(from_str::<i128>(r#"1"#), Ok(1));
728+
assert_eq!(from_str::<i128>(r#"-1"#), Ok(-1));
762729
// max i128
763730
assert_eq!(
764-
from_str::<i128>(r#""170141183460469231731687303715884105727""#),
731+
from_str::<i128>(r#"170141183460469231731687303715884105727"#),
765732
Ok(170141183460469231731687303715884105727)
766733
);
767734
assert_eq!(
768-
from_str::<i128>(r#""170141183460469231731687303715884105728""#),
735+
from_str::<i128>(r#"170141183460469231731687303715884105728"#),
769736
Err(crate::de::Error::InvalidNumber)
770737
);
771738
// min i128
772739
assert_eq!(
773-
from_str::<i128>(r#""-170141183460469231731687303715884105728""#),
740+
from_str::<i128>(r#"-170141183460469231731687303715884105728"#),
774741
Ok(-170141183460469231731687303715884105728)
775742
);
776743
assert_eq!(
777-
from_str::<i128>(r#""-170141183460469231731687303715884105729""#),
744+
from_str::<i128>(r#"-170141183460469231731687303715884105729"#),
778745
Err(crate::de::Error::InvalidNumber)
779746
);
780747

781-
assert_eq!(from_str::<u128>(r#"0"#), Err(crate::de::Error::InvalidType));
782-
assert_eq!(from_str::<u128>(r#""0""#), Ok(0));
783-
assert_eq!(from_str::<u128>(r#""1""#), Ok(1));
784748
assert_eq!(
785-
from_str::<u128>(r#""-1""#),
749+
from_str::<u128>(r#""0""#),
750+
Err(crate::de::Error::InvalidType)
751+
);
752+
assert_eq!(from_str::<u128>(r#"0"#), Ok(0));
753+
assert_eq!(from_str::<u128>(r#"1"#), Ok(1));
754+
assert_eq!(
755+
from_str::<u128>(r#"-1"#),
786756
Err(crate::de::Error::InvalidNumber)
787757
);
788758
// max u128
789759
assert_eq!(
790-
from_str::<u128>(r#""340282366920938463463374607431768211455""#),
760+
from_str::<u128>(r#"340282366920938463463374607431768211455"#),
791761
Ok(340282366920938463463374607431768211455)
792762
);
793763
assert_eq!(
794-
from_str::<u128>(r#""340282366920938463463374607431768211456""#),
764+
from_str::<u128>(r#"340282366920938463463374607431768211456"#),
795765
Err(crate::de::Error::InvalidNumber)
796766
)
797767
}

0 commit comments

Comments
 (0)