serde-flattened
A csv and serde_json extension for flattening nested structures into flat representations. This enables for example serialization/deserialization of nested data to/from CSV.
Features
- Nested CSV Support: Serialize/deserialize nested Rust structs to/from flat CSV files where nested fields are encoded using
__-separated paths. - Flatten/Unflatten JSON Values: Convert nested
serde_json::Valueto/from flat maps with__-separated paths.
Quick Start
Add to your Cargo.toml:
[]
= { = "0.1.0" }
= "1.0"
= { = "1.0", = ["derive"] }
= "1.0"
Flattening JSON Values
use ;
use ;
let nested = json!;
let flat_map = flattened;
assert_eq!;
assert_eq!;
let restored = unflattened.unwrap;
assert_eq!;
Nested CSV Serialization/Deserialization
Define your nested struct:
Writing to CSV:
use CsvWriterEnableNestedExt;
use Cursor;
let mut wtr = new
.from_writer
.enable_nested;
for parent in
let csv_bytes = wtr.into_inner?.into_inner;
Reading from CSV:
use ;
use Cursor;
let mut rdr = new
.has_headers
.from_reader
.?;
for result in rdr.deserialize
How it Works
- Serialization: Nested structs are flattened into dot-separated field paths (e.g.,
user.address.city), with primitive/nested values JSON-serialized into CSV cells. - Deserialization: CSV cells are parsed as JSON
Values, assembled into a flatObject, unflattened using path keys, then deserialized into the target struct.
License
MIT