From ea22591e365a55d40b00b0ccee24f029f4ef732d Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Fri, 24 Jul 2020 19:08:59 +0200 Subject: [PATCH] Support u128/i128 types during serialization --- src/serde/de.rs | 24 ++++++++++++++++++++++++ src/serde/ser.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/serde/de.rs b/src/serde/de.rs index 88533718..4ab9919a 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -162,10 +162,12 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { Union::Variant(value) if value.is::() => self.deserialize_i16(visitor), Union::Variant(value) if value.is::() => self.deserialize_i32(visitor), Union::Variant(value) if value.is::() => self.deserialize_i64(visitor), + Union::Variant(value) if value.is::() => self.deserialize_i128(visitor), Union::Variant(value) if value.is::() => self.deserialize_u8(visitor), Union::Variant(value) if value.is::() => self.deserialize_u16(visitor), Union::Variant(value) if value.is::() => self.deserialize_u32(visitor), Union::Variant(value) if value.is::() => self.deserialize_u64(visitor), + Union::Variant(value) if value.is::() => self.deserialize_u128(visitor), Union::Variant(_) => self.type_error(), } @@ -219,6 +221,18 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { } } + fn deserialize_i128>(self, visitor: V) -> Result> { + if let Ok(v) = self.value.as_int() { + self.deserialize_int(v, visitor) + } else if cfg!(not(feature = "only_i32")) { + self.type_error() + } else { + self.value + .downcast_ref::() + .map_or_else(|| self.type_error(), |&x| visitor.visit_i128(x)) + } + } + fn deserialize_u8>(self, visitor: V) -> Result> { if let Ok(v) = self.value.as_int() { self.deserialize_int(v, visitor) @@ -259,6 +273,16 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> { } } + fn deserialize_u128>(self, visitor: V) -> Result> { + if let Ok(v) = self.value.as_int() { + self.deserialize_int(v, visitor) + } else { + self.value + .downcast_ref::() + .map_or_else(|| self.type_error(), |&x| visitor.visit_u128(x)) + } + } + fn deserialize_f32>(self, visitor: V) -> Result> { #[cfg(not(feature = "no_float"))] return self diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 181573b0..7c3f9321 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -150,6 +150,21 @@ impl Serializer for &mut DynamicSerializer { } } + fn serialize_i128(self, v: i128) -> Result> { + #[cfg(not(feature = "only_i32"))] + if v > i64::MAX as i128 { + return Ok(Dynamic::from(v)); + } else { + return self.serialize_i64(v as i64); + } + #[cfg(feature = "only_i32")] + if v > i32::MAX as i128 { + return Ok(Dynamic::from(v)); + } else { + return self.serialize_i32(v as i32); + } + } + fn serialize_u8(self, v: u8) -> Result> { #[cfg(not(feature = "only_i32"))] return self.serialize_i64(i64::from(v)); @@ -190,6 +205,21 @@ impl Serializer for &mut DynamicSerializer { } } + fn serialize_u128(self, v: u128) -> Result> { + #[cfg(not(feature = "only_i32"))] + if v > i64::MAX as u128 { + return Ok(Dynamic::from(v)); + } else { + return self.serialize_i64(v as i64); + } + #[cfg(feature = "only_i32")] + if v > i32::MAX as u128 { + return Ok(Dynamic::from(v)); + } else { + return self.serialize_i32(v as i32); + } + } + fn serialize_f32(self, v: f32) -> Result> { Ok(Dynamic::from(v)) }