39 breaking releases

new 0.77.1 Mar 17, 2023
0.76.0 Feb 21, 2023
0.73.0 Dec 20, 2022
0.72.0 Nov 29, 2022
0.23.0 Nov 24, 2020

#68 in Encoding

Download history 1150/week @ 2022-11-28 735/week @ 2022-12-05 649/week @ 2022-12-12 803/week @ 2022-12-19 492/week @ 2022-12-26 555/week @ 2023-01-02 881/week @ 2023-01-09 525/week @ 2023-01-16 620/week @ 2023-01-23 1071/week @ 2023-01-30 680/week @ 2023-02-06 953/week @ 2023-02-13 2198/week @ 2023-02-20 1866/week @ 2023-02-27 1168/week @ 2023-03-06 1522/week @ 2023-03-13

6,854 downloads per month
Used in 23 crates (4 directly)

MIT license

110KB
3K SLoC

nu-json

crate

a fork of serde_hjson.

The changes made to this crate are kept in CHANGELOG.

The Rust implementation of Hjson is based on the Serde JSON Serialization Library.

This crate is a Rust library for parsing and generating Human JSON Hjson. It is built upon Serde, a high performance generic serialization framework.

Install

This crate works with Cargo and can be found on crates.io with a Cargo.toml like:

[dependencies]
serde = "1"
nu-json = "0.76"

From the Commandline

Add with:

 cargo add serde
 cargo add nu-json

Usage

extern crate serde;
extern crate nu_json;

use nu_json::{Map,Value};

fn main() {

    // Now let's look at decoding Hjson data

    let sample_text=r#"
    {
        # specify rate in requests/second
        rate: 1000
        array:
        [
            foo
            bar
        ]
    }"#;

    // Decode and unwrap.
    let mut sample: Map<String, Value> = nu_json::from_str(&sample_text).unwrap();

    // scope to control lifetime of borrow
    {
        // Extract the rate
        let rate = sample.get("rate").unwrap().as_f64().unwrap();
        println!("rate: {}", rate);

        // Extract the array
        let array : &mut Vec<Value> = sample.get_mut("array").unwrap().as_array_mut().unwrap();
        println!("first: {}", array.get(0).unwrap());

        // Add a value
        array.push(Value::String("tak".to_string()));
    }

    // Encode to Hjson
    let sample2 = nu_json::to_string(&sample).unwrap();
    println!("Hjson:\n{}", sample2);
}

DOCS

At the moment, the documentation on serde_hjson / serde_json is also relevant for nu-json.

Dependencies

~220–410KB