Skip to content

BigDecimal deserialization error #40

Closed

Description

@ollis

I am unable to deserialze an avro record with a Bigdecimal field. Given this minimal test:

    #[test]
    fn minimal() {
        #[derive(Debug, serde::Deserialize)]
        struct Test {
            decimal: apache_avro::Decimal,
        }
        let decimal_value = Value::Decimal(Decimal::from(vec![1, 24]));
        let record = Value::Record(vec![("decimal".to_string(), decimal_value)]);
        let deserialize_result = from_value::<Test>(&record);
        dbg!(deserialize_result);
    }

The output is deserialize_result = Err( Failed to deserialize Avro value into value: Expected a Record or Union, got: Decimal(Decimal { value: 280, len: 2 }), )

Activity

  1. martin-g commented on Nov 5, 2024

    @martin-g
    Member

    Help is welcome!
    I have no experience with this part of the code and I won't have time to look deeper into this issue in the next few weeks.

  2. ollis commented on Nov 6, 2024

    @ollis
    ContributorAuthor

    I can get it to work as expected by changing the implementation of Decimal to use a custom implementation of Serialize and Deserialize instead of relying on derive:

    #[derive(Debug, Clone, Eq)]
    pub struct Decimal {
        value: BigInt,
        len: usize,
    }
    
     impl Serialize for Decimal {
         fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
         where
             S: Serializer
         {
             let bytes = &*self.to_vec().unwrap();
             serializer.serialize_bytes(bytes)
         }
     }
    impl<'de> Deserialize<'de> for Decimal {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            struct DecimalVisitor;
            impl<'de> serde::de::Visitor<'de> for DecimalVisitor {
                type Value = Decimal;
    
                fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                    formatter.write_str("a byte slice or seq of bytes")
                }
    
                fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
                where
                    E: serde::de::Error,
                {
                    Ok(Decimal::from(v))
                }
                fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                where
                    A: SeqAccess<'de>,
                {
                    let mut bytes = Vec::new();
                    while let Some(value) = seq.next_element::<u8>()? {
                        bytes.push(value);
                    }
    
                    Ok(Decimal::from(bytes))
                }
            }
            deserializer.deserialize_bytes(DecimalVisitor)
        }
    }
  3. martin-g commented on Nov 6, 2024

    @martin-g
    Member

    Awesome!
    Please send a Pull Request!

  4. ollis commented on Nov 7, 2024

    @ollis
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions