json_object 0.1.1

A Simple JsonObject library for Rust
Documentation
  • Coverage
  • 32.81%
    21 out of 64 items documented3 out of 56 items with examples
  • Size
  • Source code size: 33.14 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 643.47 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Rebon402/json_object
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Rebon402

json_object

A simple, ergonomic, zero-dependency JSON library for Rust — no macros crate needed.

Crates.io Docs.rs License: MIT


Features

  • 🏗️ json! macro — build JSON with familiar literal syntax
  • 🔍 Dot-path accessobj.get_path("user.address.city")
  • ✏️ Fluent buildersObjectBuilder and ArrayBuilder
  • 📦 Parse & Serialize — compact and pretty-print output
  • 🦀 Zero dependencies — pure std only
  • No unsafe code

Installation

[dependencies]

json_object = "0.1.0"


Quick Start

use json_object::{Json, ObjectBuilder, ArrayBuilder, json};

fn main() {
    // Build with macro
    let obj = json!({
        "name": "Rebon402",
        "age": 30i64,
        "active": true,
        "score": 9.5f64,
        "tags": ["rust", "json"],
        "address": {
            "city": "Bangkok"
        },
        "nothing": null
    });

    // Access values
    println!("{}", obj["name"].as_str().unwrap());      // Rebon402
    println!("{}", obj["age"].as_i64().unwrap());       // 30
    println!("{}", obj["active"].as_bool().unwrap());   // true

    // Dot-path access
    let city = obj.get_path("address.city").unwrap();
    println!("{}", city.as_str().unwrap());             // Bangkok

    // Serialize
    println!("{}", obj.to_json());                      // compact
    println!("{}", obj.to_pretty("  "));                // pretty
}

Usage

json! Macro

let v = json!({
    "key": "value",
    "num": 42i64,
    "flag": true,
    "list": [1i64, 2i64, 3i64],
    "nested": { "x": 0i64 },
    "empty": null
});

Parse from string

let v = Json::parse(r#"{"name":"Bob","age":25}"#).unwrap();
assert_eq!(v["name"].as_str(), Some("Bob"));

Reading values

v["key"].as_str()    // Option<&str>
v["num"].as_i64()    // Option<i64>
v["num"].as_f64()    // Option<f64>
v["flag"].as_bool()  // Option<bool>
v["list"].as_array() // Option<&Vec<Json>>
v["obj"].as_object() // Option<&HashMap<String, Json>>

// Index operator — returns Json::Null instead of panicking
let name = &v["missing"]; // Json::Null

// Array index
let first = &v["list"][0];

// Deep path
v.get_path("user.address.city") // Option<&Json>

Mutating values

let mut obj = Json::new_object();
obj.set("name", "Carol");
obj.set("age", 28i64);
obj.remove("age");
obj.merge(&other_obj);
obj.contains_key("name"); // true

let mut arr = Json::new_array();
arr.push("hello");
arr.push(42i64);

Fluent Builders

// Object
let user = ObjectBuilder::new()
    .set("name", "Dave")
    .set("age", 35i64)
    .set_if(is_admin, "role", "admin")   // conditional
    .build();

// Array
let scores = ArrayBuilder::new()
    .push(10i64)
    .push(20i64)
    .push_if(bonus, 50i64)               // conditional
    .extend(vec![1i64, 2i64, 3i64])
    .build();

Serialization

let compact = obj.to_json();          // {"name":"Rebon402","age":30}
let pretty  = obj.to_pretty("  ");   // 2-space indent
let tabbed  = obj.to_pretty("\t");   // tab indent

Type checks

v.is_null()    // bool
v.is_bool()    // bool
v.is_int()     // bool
v.is_float()   // bool
v.is_number()  // int or float
v.is_str()     // bool
v.is_array()   // bool
v.is_object()  // bool

Iteration

// Array
for item in obj["tags"].iter_array() {
    println!("{}", item);
}

// Object
for (key, val) in obj.iter_object() {
    println!("{}: {}", key, val);
}

obj.keys()   // Vec<&String>
obj.values() // Vec<&Json>
obj.len()    // usize

From<T> conversions

All common types convert automatically into Json:

Rust type Json variant
bool Json::Bool
i32, i64, u64, usize Json::Int
f32, f64 Json::Float
&str, String Json::Str
Vec<T> Json::Array
Option<T> Json::Null or inner value

License

MIT — see LICENSE