Is there a way to serialise JSON value using avro-rs in Rust?

Viewed 367

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:

  1. Create a new project by running cargo new example
  2. Update the Cargo.toml with the following dependencies:
[package]
name = "rust-example"
version = "0.1.0"
edition = "2018"

[dependencies]
serde_json = "1.0.52"
avro-rs = "0.10.0"
  1. Update the scr/main.rs with:
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")
}
  1. Finally run cargo run to see the output.
1 Answers
Related