I would like to serialise a JSON value using avro-rs. I can't seem to find a way to do this. To reproduce you can do the following:
- Create a new project by running
cargo new example - Update the
Cargo.tomlwith the following dependencies:
[package]
name = "rust-example"
version = "0.1.0"
edition = "2018"
[dependencies]
serde_json = "1.0.52"
avro-rs = "0.10.0"
- Update the
scr/main.rswith:
use avro_rs::{Schema, Writer};
use serde_json::json;
fn main() {
let raw_schema = r#"
{
"type": "record",
"name": "action",
"fields": [
{"name": "id", "type": "string"}
]
}
"#;
let schema = Schema::parse_str(raw_schema).unwrap();
let mut writer = Writer::new(&schema, Vec::new());
let action = json!({
"id": "some id"
});
writer.append(action).unwrap(); // panics: ValidationError("value does not match schema")
}
- Finally run
cargo runto see the output.