From 06e54efc367c58f56027feed04df82a9c2072913 Mon Sep 17 00:00:00 2001 From: Martin Grigorov Date: Tue, 27 Feb 2024 12:06:38 +0200 Subject: [PATCH] AVRO-3949: [Rust]: Add support for serde to apache_avro::Decimal (#2772) Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/Cargo.lock | 1 + lang/rust/avro/Cargo.toml | 2 +- lang/rust/avro/src/decimal.rs | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lang/rust/Cargo.lock b/lang/rust/Cargo.lock index d35261e0734..b5d4f258c3d 100644 --- a/lang/rust/Cargo.lock +++ b/lang/rust/Cargo.lock @@ -821,6 +821,7 @@ dependencies = [ "autocfg", "num-integer", "num-traits", + "serde", ] [[package]] diff --git a/lang/rust/avro/Cargo.toml b/lang/rust/avro/Cargo.toml index f73e902b95c..bfb2c957399 100644 --- a/lang/rust/avro/Cargo.toml +++ b/lang/rust/avro/Cargo.toml @@ -61,7 +61,7 @@ crc32fast = { default-features = false, version = "1.4.0", optional = true } digest = { default-features = false, version = "0.10.7", features = ["core-api"] } libflate = { default-features = false, version = "2.0.0", features = ["std"] } log = { workspace = true } -num-bigint = { default-features = false, version = "0.4.4" } +num-bigint = { default-features = false, version = "0.4.4", features = ["std", "serde"] } regex-lite = { default-features = false, version = "0.1.5", features = ["std", "string"] } serde = { workspace = true } serde_json = { workspace = true } diff --git a/lang/rust/avro/src/decimal.rs b/lang/rust/avro/src/decimal.rs index c0646590e9f..6854f3424d9 100644 --- a/lang/rust/avro/src/decimal.rs +++ b/lang/rust/avro/src/decimal.rs @@ -18,7 +18,7 @@ use crate::{AvroResult, Error}; use num_bigint::{BigInt, Sign}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, serde::Serialize, serde::Deserialize)] pub struct Decimal { value: BigInt, len: usize, @@ -132,4 +132,15 @@ mod tests { Ok(()) } + + #[test] + fn avro_3949_decimal_serde() -> TestResult { + let decimal = Decimal::from(&[1, 2, 3]); + + let ser = serde_json::to_string(&decimal)?; + let de = serde_json::from_str(&ser)?; + std::assert_eq!(decimal, de); + + Ok(()) + } }