#serialization #format #macro #key #run-time #performance #debugging #jsony

jsony

An experimental fast compiling serialization and deserialization libary for JSON like formats

5 releases

new 0.1.2 Apr 8, 2025
0.1.1 Apr 1, 2025
0.1.0 Mar 30, 2025
0.0.2 Nov 3, 2024
0.0.1 Oct 1, 2024

#436 in Encoding

Download history 1/week @ 2025-02-12 195/week @ 2025-03-26 202/week @ 2025-04-02

397 downloads per month

MIT license

240KB
6K SLoC

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
  • Encoding checked at compile time, infallible at runtime
  • Data formats
    • JSON (optional extension: trailing commas, comments, unquoted keys)
    • Custom Binary Encoding
  • Lazy JSON parser for efficiently extracting small fragments.
  • JSON templating macros
  • Encode directly to a file or stack allocated to buffer.

Example

use jsony::Jsony;

#[derive(Jsony, Debug)]
#[jsony(FromJson, ToJson, tag = "kind")]
enum Status<'a> {
    Online,
    Error {
        #[jsony(default = i64::MAX)]
        code: i64,
        message: Cow<'a, str>,
        #[jsony(flatten, via = Iterator)]
        properties: Vec<(String, JsonItem<'a>)>,
    },
    Offline,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input: String = jsony::object! {
        kind: "Error",
        code: 300,
        message: "System Failure",
        data: [1, 2, 3],
        value: {
          previous: {
            kind: "Offline",
          }
        }
    };

    let previous: Status = jsony::drill(&input)["value"]["previous"].parse()?;
    assert!(matches!(previous, Status::Offline));

    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.

Dependencies

~135KB