serdavro

Crates.ioserdavro
lib.rsserdavro
version1.0.0
created_at2025-10-14 22:11:27.516104+00
updated_at2025-10-14 22:11:27.516104+00
descriptionA temporary way to use serde flatten feature with apache-avro
homepage
repositoryhttps://gitlab.com/sufflope/serdavro
max_upload_size
id1883287
size155,850
Jean-Sébastien Bour (Sufflope)

documentation

README

serdavro

License GitLab Release Crates.io Version Source Code Repository GitLab Pipeline Status GitLab Code Coverage Crates.io MSRV

#[serde(flatten)]-aware extension for apache-avro

Disclaimer

THIS CRATE IS SUPPOSED TO BE TEMPORARY

Support of #[serde(flatten)] in apache-avro is under work, this crate fills the void in the meantime.

Usage

If you have a struct with a flattened field, instead of using apache-avro derive and Writer::append_ser writer methods which will fail, use this crate’s #[serdavro] macro and append_serdavro extension method:

use apache_avro::{from_value, Writer, AvroSchema, Reader, Schema};
use serdavro::{serdavro, SerdavroWriter};
use serde::{Deserialize, Serialize};

// This struct can use standard derive
#[derive(AvroSchema, Serialize)]
// Required for below test
#[derive(Debug, Deserialize, PartialEq)]
pub struct Foo {
    one: u32,
}

// This struct flattens one of its field, so it needs this crate's macro instead
#[serdavro]
#[derive(Serialize)]
// Required for below test
#[derive(Debug, Deserialize, PartialEq)]
struct Bar {
    #[serde(flatten)]
    foo: Foo,
    two: u32,
}

let bar = Bar {
    foo: Foo { one: 1 },
    two: 2
};

let schema = Bar::get_schema();

// `Foo`'s `one` field was flattened as another root record field
assert!(
    matches!(
        &schema,
        Schema::Record(record)
            if record.fields.iter().map(|field| {
                &field.name
            }).collect::<Vec<_>>() == ["one", "two"]
    )
);

// When appending a value, use this crate's special extension method
let mut writer = Writer::new(&schema, Vec::new());
writer.append_serdavro(&bar).unwrap();

// Check that it was correctly serialized and is deserializable
let encoded = writer.into_inner().unwrap();
let mut reader = Reader::new(&encoded[..]).unwrap();
let value = reader.next().unwrap().unwrap();
let result: Bar = from_value(&value).unwrap();
assert_eq!(result, bar)

Name

It is serde and avro but flattened

Oh si c’est rigolo !…

Commit count: 0

cargo fmt