Immediate Mode JSON Serialization Library in Rust
Find a file
2025-10-13 03:49:54 +03:00
src fix: add self.comma() call for nested object fields 2025-10-13 03:49:54 +03:00
.gitignore Hi 2025-10-13 03:12:09 +03:00
Cargo.lock fix: add self.comma() call for nested object fields 2025-10-13 03:49:54 +03:00
Cargo.toml fix: add self.comma() call for nested object fields 2025-10-13 03:49:54 +03:00
flake.lock Hi 2025-10-13 03:12:09 +03:00
flake.nix Hi 2025-10-13 03:12:09 +03:00
LICENSE Hi 2025-10-13 03:12:09 +03:00
README.md Hi 2025-10-13 03:12:09 +03:00

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>`