Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add deserializer for i128 and u128
  • Loading branch information
loloicci committed Nov 16, 2021
commit 152b3a6a2953bfe48483dc8e204ef6c3bf032f30
107 changes: 105 additions & 2 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod unescape;
pub use errors::{Error, Result};

use serde::de::{self, Visitor};
use serde::serde_if_integer128;

use self::enum_::{StructVariantAccess, UnitVariantAccess};
use self::map::MapAccess;
Expand Down Expand Up @@ -194,9 +195,10 @@ macro_rules! deserialize_unsigned {
.checked_add((c - b'0') as $uxx)
.ok_or(Error::InvalidNumber)?;
}
_ => return $visitor.$visit_uxx(number),
_ => break,
}
}
$visitor.$visit_uxx(number)
}
_ => Err(Error::InvalidType),
}
Expand Down Expand Up @@ -235,9 +237,10 @@ macro_rules! deserialize_signed {
.checked_add((c - b'0') as $ixx * if signed { -1 } else { 1 })
.ok_or(Error::InvalidNumber)?;
}
_ => return $visitor.$visit_ixx(number),
_ => break,
}
}
$visitor.$visit_ixx(number)
}
_ => return Err(Error::InvalidType),
}
Expand Down Expand Up @@ -308,6 +311,34 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
deserialize_signed!(self, visitor, i64, visit_i64)
}

serde_if_integer128! {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this macro? We only support 1.53+ so we can assume it is available, right?

Copy link
Author

@loloicci loloicci Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I missed that version assumption. I will remove it. Thank you!

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
match self
.parse_whitespace()
.ok_or(Error::EofWhileParsingValue)? {
b'"' => {
self.eat_char()
}
_ => return Err(Error::InvalidType)
};

let result = match self.peek() {
Some(b'0'..=b'9' | b'-') => deserialize_signed!(self, visitor, i128, visit_i128),
_ => return Err(Error::InvalidType)
};
match self.peek() {
Some(b'"') => {
self.eat_char();
result
}
_ => Err(Error::InvalidType)
}
}
}

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -336,6 +367,35 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
deserialize_unsigned!(self, visitor, u64, visit_u64)
}

serde_if_integer128! {
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
match self
.parse_whitespace()
.ok_or(Error::EofWhileParsingValue)? {
b'"' => {
self.eat_char();
}
_ => return Err(Error::InvalidType)
};

let result = match self.peek() {
Some(b'-') => return Err(Error::InvalidNumber),
Some(b'0'..=b'9') => deserialize_unsigned!(self, visitor, u128, visit_u128),
_ => return Err(Error::InvalidType)
};
match self.peek() {
Some(b'"') => {
self.eat_char();
result
}
_ => Err(Error::InvalidType)
}
}
}

fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
Expand Down Expand Up @@ -660,6 +720,49 @@ mod tests {
assert_eq!(from_str("[4,5]\r\n"), Ok([4, 5]));
}

#[test]
fn integer128() {
assert_eq!(from_str::<i128>(r#"0"#), Err(crate::de::Error::InvalidType));
assert_eq!(from_str::<i128>(r#""0""#), Ok(0));
assert_eq!(from_str::<i128>(r#""1""#), Ok(1));
assert_eq!(from_str::<i128>(r#""-1""#), Ok(-1));
// max i128
assert_eq!(
from_str::<i128>(r#""170141183460469231731687303715884105727""#),
Ok(170141183460469231731687303715884105727)
);
assert_eq!(
from_str::<i128>(r#""170141183460469231731687303715884105728""#),
Err(crate::de::Error::InvalidNumber)
);
// min i128
assert_eq!(
from_str::<i128>(r#""-170141183460469231731687303715884105728""#),
Ok(-170141183460469231731687303715884105728)
);
assert_eq!(
from_str::<i128>(r#""-170141183460469231731687303715884105729""#),
Err(crate::de::Error::InvalidNumber)
);

assert_eq!(from_str::<u128>(r#"0"#), Err(crate::de::Error::InvalidType));
assert_eq!(from_str::<u128>(r#""0""#), Ok(0));
assert_eq!(from_str::<u128>(r#""1""#), Ok(1));
assert_eq!(
from_str::<u128>(r#""-1""#),
Err(crate::de::Error::InvalidNumber)
);
// max u128
assert_eq!(
from_str::<u128>(r#""340282366920938463463374607431768211455""#),
Ok(340282366920938463463374607431768211455)
);
assert_eq!(
from_str::<u128>(r#""340282366920938463463374607431768211456""#),
Err(crate::de::Error::InvalidNumber)
)
}

#[test]
fn array() {
assert_eq!(from_str::<[i32; 0]>("[]"), Ok([]));
Expand Down
44 changes: 22 additions & 22 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,61 +622,61 @@ mod tests {
);

serde_if_integer128! {
assert_eq!(to_string::<u128>(&0).unwrap(), "\"0\"");
assert_eq!(to_string::<u128>(&1).unwrap(), "\"1\"");
assert_eq!(to_string::<u128>(&456789).unwrap(), "\"456789\"");
assert_eq!(to_string::<u128>(&4294967295).unwrap(), "\"4294967295\"");
assert_eq!(to_string::<u128>(&4294967296).unwrap(), "\"4294967296\"");
assert_eq!(to_string::<u128>(&0).unwrap(), r#""0""#);
assert_eq!(to_string::<u128>(&1).unwrap(), r#""1""#);
assert_eq!(to_string::<u128>(&456789).unwrap(), r#""456789""#);
assert_eq!(to_string::<u128>(&4294967295).unwrap(), r#""4294967295""#);
assert_eq!(to_string::<u128>(&4294967296).unwrap(), r#""4294967296""#);
assert_eq!(
to_string::<u128>(&9007199254740991).unwrap(),
"\"9007199254740991\""
r#""9007199254740991""#
); // Number.MAX_SAFE_INTEGER
assert_eq!(
to_string::<u128>(&9007199254740992).unwrap(),
"\"9007199254740992\""
r#""9007199254740992""#
); // Number.MAX_SAFE_INTEGER+1
assert_eq!(
to_string::<u128>(&9223372036854775807).unwrap(),
"\"9223372036854775807\""
r#""9223372036854775807""#
);
assert_eq!(
to_string::<u128>(&9223372036854775808).unwrap(),
"\"9223372036854775808\""
r#""9223372036854775808""#
);
assert_eq!(
to_string::<u128>(&std::u128::MAX).unwrap(),
"\"340282366920938463463374607431768211455\""
r#""340282366920938463463374607431768211455""#
);

assert_eq!(to_string::<i128>(&0).unwrap(), "\"0\"");
assert_eq!(to_string::<i128>(&1).unwrap(), "\"1\"");
assert_eq!(to_string::<i128>(&456789).unwrap(), "\"456789\"");
assert_eq!(to_string::<i128>(&4294967295).unwrap(), "\"4294967295\"");
assert_eq!(to_string::<i128>(&4294967296).unwrap(), "\"4294967296\"");
assert_eq!(to_string::<i128>(&0).unwrap(), r#""0""#);
assert_eq!(to_string::<i128>(&1).unwrap(), r#""1""#);
assert_eq!(to_string::<i128>(&456789).unwrap(), r#""456789""#);
assert_eq!(to_string::<i128>(&4294967295).unwrap(), r#""4294967295""#);
assert_eq!(to_string::<i128>(&4294967296).unwrap(), r#""4294967296""#);
assert_eq!(
to_string::<i128>(&9007199254740991).unwrap(),
"\"9007199254740991\""
r#""9007199254740991""#
); // Number.MAX_SAFE_INTEGER
assert_eq!(
to_string::<i128>(&9007199254740992).unwrap(),
"\"9007199254740992\""
r#""9007199254740992""#
); // Number.MAX_SAFE_INTEGER+1
assert_eq!(
to_string::<i128>(&9223372036854775807).unwrap(),
"\"9223372036854775807\""
r#""9223372036854775807""#
);
assert_eq!(
to_string::<i128>(&9223372036854775808).unwrap(),
"\"9223372036854775808\""
r#""9223372036854775808""#
);
assert_eq!(
to_string::<i128>(&std::i128::MAX).unwrap(),
"\"170141183460469231731687303715884105727\""
r#""170141183460469231731687303715884105727""#
);
assert_eq!(to_string::<i128>(&-1).unwrap(), "\"-1\"");
assert_eq!(to_string::<i128>(&-1).unwrap(), r#""-1""#);
assert_eq!(
to_string::<i128>(&std::i128::MIN).unwrap(),
"\"-170141183460469231731687303715884105728\""
r#""-170141183460469231731687303715884105728""#
);
}
}
Expand Down