Skip to content

meh/json-rust

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-rust

Parse and serialize JSON with ease.

Complete Documentation - Cargo - Repository

Why?

JSON is a very loose format where anything goes - arrays can hold mixed types, object keys can change types between API calls or not include some keys under some conditions. Mapping that to idiomatic Rust structs introduces friction.

This crate intends to avoid that friction by extensively using static dispatch and hiding type information behind enums, while still giving you all the guarantees of safe Rust code.

let data = json::parse(r#"

{
    "code": 200,
    "success": true,
    "payload": {
        "features": [
            "awesome",
            "easyAPI",
            "lowLearningCurve"
        ]
    }
}

"#).unwrap();

assert!(data["code"] == 200);
assert!(data["success"] == true));
assert!(data["payload"]["features"].is_array());
assert!(data["payload"]["features"][0] == "awesome");
assert!(data["payload"]["features"].contains("easyAPI"));

// Error resilient: non-existent values default to null
assert!(data["this"]["does"]["not"]["exist"].is_null());

Create JSON data without defining structs

#[macro_use]
extern crate json;

fn main() {
    let data = object!{
        "a" => "bar",
        "b" => array![1, false, "foo"]
    };

    assert_eq!(data.dump(), r#"{"a":"bar","b":[1,false,"foo"]}"#);
}

Mutate simply by assigning new values

let mut data = json::parse(r#"

{
    "name": "Bob",
    "isAwesome": false
}

"#).unwrap();

data["isAwesome"] = true.into();
data["likes"] = "Rust".into();

assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);

// Pretty print the output
println!("{:#}", data);

Installation

Just add it to your Cargo.toml file:

[dependencies]
json = "*"

Then import it in your main.rs / lib.rs file:

#[macro_use]
extern crate json;

About

JSON implementation in Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%