jsony

Crates.iojsony
lib.rsjsony
version0.1.7
created_at2024-10-01 11:53:49.052484+00
updated_at2025-06-25 13:13:56.968764+00
descriptionAn experimental fast compiling serialization and deserialization libary for JSON like formats.
homepage
repositoryhttps://github.com/exrok/jsony
max_upload_size
id1392815
size305,138
Thomas Dagenais (exrok)

documentation

README

Jsony

An experimental fast compiling serialization and deserialization rust library for JSON like formats.

Crates.io Crates.io License

WARNING: Jsony is currently in early development and makes extensive use of unsafe.

Features

  • Fast compile times
  • Competitive runtime performance
  • Featureful derive macros for implementing To/From for various data formats
  • Infallible serialization guaranteed to succeed via the type system
  • Data formats:
    • JSON (optional extension: trailing commas, comments, unquoted keys)
    • Compact Binary Encoding with zerocopy and versioning support.
  • Lazy JSON parser for efficiently extracting small fragments.
  • JSON templating macros
  • Encode directly to a file or stack allocated to buffer.
  • Custom field property validation

Example

use jsony::{Jsony, require};

#[derive(Jsony, Debug)]
#[jsony(Json, tag = "kind")]
enum Status<'a> {
    Online,
    Error {
        #[jsony(default = i64::MAX)]
        code: i64,
        #[jsony(validate = require!(|m| !m.is_empty(), "Message must be non-empty"))]
        message: Cow<'a, str>,
        #[jsony(flatten, via = Iterator)]
        properties: Vec<(String, Data)>,
    },
    Offline,
}

#[derive(Jsony, Debug)]
#[jsony(Json, untagged)]
enum Data {
    Text(String),
    Array(Vec<Data>),
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input: String = jsony::object! {
        kind: "Error",
        code: 300,
        message: "System Failure",
        value: ["alpha", ["beta", "bravo"]],
    };

    let data: String = jsony::drill(&input)["value"][1][0].parse()?;
    assert_eq!(data, "beta");

    let status: Status = jsony::from_json(&input)?;

    assert_eq!(input, jsony::to_json(&status));

    Ok(())
}

Acknowledgements

The derive feature set is largely based of serde and serde_with.
The json parser is heavily inspire by jiter and serde_json.

Commit count: 98

cargo fmt