| Crates.io | serdavro |
| lib.rs | serdavro |
| version | 1.0.0 |
| created_at | 2025-10-14 22:11:27.516104+00 |
| updated_at | 2025-10-14 22:11:27.516104+00 |
| description | A temporary way to use serde flatten feature with apache-avro |
| homepage | |
| repository | https://gitlab.com/sufflope/serdavro |
| max_upload_size | |
| id | 1883287 |
| size | 155,850 |
#[serde(flatten)]-aware extension for apache-avro
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.
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)
It is serde and avro but flattened…
