Crate nu_json

Source
Expand description

§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.

Re-exports§

pub use self::de::from_iter;
pub use self::de::from_reader;
pub use self::de::from_slice;
pub use self::de::from_str;
pub use self::de::Deserializer;
pub use self::de::StreamDeserializer;
pub use self::error::Error;
pub use self::error::ErrorCode;
pub use self::error::Result;
pub use self::ser::to_string;
pub use self::ser::to_string_raw;
pub use self::ser::to_string_with_indent;
pub use self::ser::to_string_with_tab_indentation;
pub use self::ser::to_vec;
pub use self::ser::to_writer;
pub use self::ser::Serializer;
pub use self::value::from_value;
pub use self::value::to_value;
pub use self::value::Map;
pub use self::value::Value;

Modules§

builder
de
Hjson Deserialization
error
JSON Errors
ser
Hjson Serialization
value