Skip to content

Commit

Permalink
Add Number u128 conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 18, 2024
1 parent 7e45e3d commit d86703f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
78 changes: 64 additions & 14 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,6 @@ impl Number {
self.n.parse().ok()
}

/// If the `Number` is an integer, represent it as i128 if possible. Returns
/// None otherwise.
pub fn as_i128(&self) -> Option<i128> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => Some(n as i128),
N::NegInt(n) => Some(n as i128),
N::Float(_) => None,
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}

/// If the `Number` is an integer, represent it as u64 if possible. Returns
/// None otherwise.
pub fn as_u64(&self) -> Option<u64> {
Expand Down Expand Up @@ -211,6 +198,31 @@ impl Number {
}
}

/// If the `Number` is an integer, represent it as i128 if possible. Returns
/// None otherwise.
pub fn as_i128(&self) -> Option<i128> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => Some(n as i128),
N::NegInt(n) => Some(n as i128),
N::Float(_) => None,
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}

/// If the `Number` is an integer, represent it as u128 if possible. Returns
/// None otherwise.
pub fn as_u128(&self) -> Option<u128> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => Some(n as u128),
N::NegInt(_) | N::Float(_) => None,
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}

/// Converts an `i128` to a `Number`. Numbers smaller than i64::MIN or
/// larger than u64::MAX can only be represented in `Number` if serde_json's
/// "arbitrary_precision" feature is enabled.
Expand Down Expand Up @@ -240,6 +252,33 @@ impl Number {
Some(Number { n })
}

/// Converts a `u128` to a `Number`. Numbers greater than u64::MAX can only
/// be represented in `Number` if serde_json's "arbitrary_precision" feature
/// is enabled.
///
/// ```
/// # use serde_json::Number;
/// #
/// assert!(Number::from_u128(256).is_some());
/// ```
pub fn from_u128(i: u128) -> Option<Number> {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
if let Ok(u) = u64::try_from(i) {
N::PosInt(u)
} else {
return None;
}
}
#[cfg(feature = "arbitrary_precision")]
{
i.to_string()
}
};
Some(Number { n })
}

/// Returns the exact original JSON representation that this Number was
/// parsed from.
///
Expand Down Expand Up @@ -376,13 +415,22 @@ impl<'de> Deserialize<'de> for Number {
where
E: de::Error,
{
Number::from_i128(value).ok_or_else(|| de::Error::custom("not a JSON number"))
Number::from_i128(value)
.ok_or_else(|| de::Error::custom("JSON number out of range"))
}

fn visit_u64<E>(self, value: u64) -> Result<Number, E> {
Ok(value.into())
}

fn visit_u128<E>(self, value: u128) -> Result<Number, E>
where
E: de::Error,
{
Number::from_u128(value)
.ok_or_else(|| de::Error::custom("JSON number out of range"))
}

fn visit_f64<E>(self, value: f64) -> Result<Number, E>
where
E: de::Error,
Expand Down Expand Up @@ -503,6 +551,8 @@ macro_rules! deserialize_any {
return visitor.visit_u64(u);
} else if let Some(i) = self.as_i64() {
return visitor.visit_i64(i);
} else if let Some(u) = self.as_u128() {
return visitor.visit_u128(u);
} else if let Some(i) = self.as_i128() {
return visitor.visit_i128(i);
} else if let Some(f) = self.as_f64() {
Expand Down
8 changes: 8 additions & 0 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl<'de> Deserialize<'de> for Value {
Ok(Value::Number(value.into()))
}

fn visit_u128<E>(self, value: u128) -> Result<Value, E>
where
E: serde::de::Error,
{
let de = serde::de::value::U128Deserializer::new(value);
Number::deserialize(de).map(Value::Number)
}

#[inline]
fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
Ok(Number::from_f64(value).map_or(Value::Null, Value::Number))
Expand Down

0 comments on commit d86703f

Please sign in to comment.