jim-rs 0.1.1

Immediate mode JSON serialization library
Documentation
  • Coverage
  • 0%
    0 out of 22 items documented0 out of 21 items with examples
  • Size
  • Source code size: 11.3 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 190.6 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • thexeondev/jim-rs
    7 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • thexeondev

JIM-RS

Immediate Mode JSON Serialization Library in Rust, inspired by Jim library in C.

Why

It's quite handy to construct a JSON object by series of actions rather than defining an actual structure (thus dealing with lifetime management, unnecessary allocations and whatnot). The library aims to be small and portable, so it doesn't depend on any external crates. The entirety of the "codebase" is around 100 lines of code.

Compile time guarantees

This library utilizes typestate pattern to ensure type safety. This means that the usage of object/array scopes is checked at compile time (e.g. an attempt to start serializing a field inside of an array scope will lead to compilation error)

Example

fn main() -> std::io::Result<()> {
    let mut buffer: Vec<u8> = Vec::new();

    jim_rs::new_root(&mut buffer)?
        .field("retcode")?
        .number(1337.0)?
        .field("message")?
        .string("OK")?
        .field("data")?
        .null()?
        .field("numbers")?
        .array()?
        .number(1234.0)?
        .number(1337.0)?
        .end()?
        .end()?;

    println!("{}", String::from_utf8(buffer).unwrap());
    Ok(())
}

Output

{"retcode": 1337, "message": "OK", "data": null, "numbers": [1234, 1337]}

Compile time scope checking example

An attempt to serialize field inside of an array scope

jim_rs::new_root(&mut buffer)?
    .field("numbers")?
    .array()?
    .field("number")
    .number(1234.0)?
    .end()?
    .end()?;

Compilation error

error[E0599]: no method named `field` found for struct `JsonWriter<&mut Vec<u8>, Array<Root>>` in the current scope
 --> src/main.rs:7:10
  |
4 | /     jim_rs::new_root(&mut buffer)?
5 | |         .field("numbers")?
6 | |         .array()?
7 | |         .field("number")
  | |         -^^^^^ method not found in `JsonWriter<&mut Vec<u8>, Array<Root>>`
  | |_________|
  |
  |
  = note: the method was found for
          - `JsonWriter<Sink, Object<Prev>>`
          - `JsonWriter<Sink, Root>`