81 releases (breaking)

0.100.0 Nov 13, 2024
0.98.0 Sep 18, 2024
0.96.1 Jul 29, 2024
0.91.0 Mar 5, 2024
0.23.0 Nov 24, 2020

#366 in Encoding

Download history 2010/week @ 2024-08-21 1491/week @ 2024-08-28 1233/week @ 2024-09-04 1394/week @ 2024-09-11 2226/week @ 2024-09-18 1526/week @ 2024-09-25 1509/week @ 2024-10-02 1499/week @ 2024-10-09 2318/week @ 2024-10-16 1592/week @ 2024-10-23 1810/week @ 2024-10-30 1348/week @ 2024-11-06 2047/week @ 2024-11-13 1883/week @ 2024-11-20 2324/week @ 2024-11-27 1708/week @ 2024-12-04

8,145 downloads per month
Used in 30 crates (6 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.first().unwrap());

        // Add a value
        array.push(Value::String("baz".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

~0.5–1.2MB
~23K SLoC