diff --git a/crates/nu-protocol/src/value/serde_bigdecimal.rs b/crates/nu-protocol/src/value/serde_bigdecimal.rs index 0bc407f232..38c4e69b93 100644 --- a/crates/nu-protocol/src/value/serde_bigdecimal.rs +++ b/crates/nu-protocol/src/value/serde_bigdecimal.rs @@ -1,18 +1,11 @@ use bigdecimal::BigDecimal; -use num_traits::cast::FromPrimitive; -use num_traits::cast::ToPrimitive; /// Enable big decimal serialization by providing a `serialize` function pub fn serialize(big_decimal: &BigDecimal, serializer: S) -> Result where S: serde::Serializer, { - serde::Serialize::serialize( - &big_decimal - .to_f64() - .ok_or_else(|| serde::ser::Error::custom("expected a f64-sized bignum"))?, - serializer, - ) + serde::Serialize::serialize(&big_decimal.to_string(), serializer) } /// Enable big decimal deserialization by providing a `deserialize` function @@ -20,7 +13,7 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, { - let x: f64 = serde::Deserialize::deserialize(deserializer)?; - Ok(BigDecimal::from_f64(x) - .ok_or_else(|| serde::de::Error::custom("expected a f64-sized bigdecimal"))?) + let x: String = serde::Deserialize::deserialize(deserializer)?; + Ok(BigDecimal::parse_bytes(x.as_bytes(), 10) + .ok_or_else(|| serde::de::Error::custom("expected a bigdecimal"))?) } diff --git a/crates/nu-protocol/src/value/serde_bigint.rs b/crates/nu-protocol/src/value/serde_bigint.rs index c9c05bd57b..dabe8c1623 100644 --- a/crates/nu-protocol/src/value/serde_bigint.rs +++ b/crates/nu-protocol/src/value/serde_bigint.rs @@ -1,18 +1,11 @@ use num_bigint::BigInt; -use num_traits::cast::FromPrimitive; -use num_traits::cast::ToPrimitive; /// Enable big int serialization by providing a `serialize` function pub fn serialize(big_int: &BigInt, serializer: S) -> Result where S: serde::Serializer, { - serde::Serialize::serialize( - &big_int - .to_i64() - .ok_or_else(|| serde::ser::Error::custom("expected a i64-sized bignum"))?, - serializer, - ) + serde::Serialize::serialize(&big_int.to_string(), serializer) } /// Enable big int deserialization by providing a `deserialize` function @@ -20,7 +13,8 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result where D: serde::Deserializer<'de>, { - let x: i64 = serde::Deserialize::deserialize(deserializer)?; - Ok(BigInt::from_i64(x) - .ok_or_else(|| serde::de::Error::custom("expected a i64-sized bignum"))?) + let x: String = serde::Deserialize::deserialize(deserializer)?; + + Ok(BigInt::parse_bytes(x.as_bytes(), 10) + .ok_or_else(|| serde::de::Error::custom("expected a bignum"))?) } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 46c43e4ce4..3847b61cee 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -332,6 +332,30 @@ fn string_interpolation_with_it_column_path() { assert_eq!(actual.out, "sammie"); } +#[test] +fn bignum_large_integer() { + let actual = nu!( + cwd: ".", + r#" + echo 91231720741731287123917 + "# + ); + + assert_eq!(actual.out, "91231720741731287123917"); +} + +#[test] +fn bignum_large_decimal() { + let actual = nu!( + cwd: ".", + r#" + echo 91231720741731287123917.1 + "# + ); + + assert_eq!(actual.out, "91231720741731287123917.1"); +} + #[test] fn run_custom_command() { let actual = nu!(