7 unstable releases

Uses old Rust 2015

0.9.1 Aug 19, 2019
0.9.0 Dec 19, 2018
0.8.2 Oct 17, 2018
0.8.1 Aug 18, 2016
0.0.1 Jun 24, 2016

#387 in Encoding

Download history 14236/week @ 2024-01-03 16117/week @ 2024-01-10 18642/week @ 2024-01-17 17796/week @ 2024-01-24 26674/week @ 2024-01-31 19187/week @ 2024-02-07 21495/week @ 2024-02-14 23011/week @ 2024-02-21 23830/week @ 2024-02-28 22329/week @ 2024-03-06 29202/week @ 2024-03-13 29921/week @ 2024-03-20 20635/week @ 2024-03-27 22379/week @ 2024-04-03 19735/week @ 2024-04-10 17478/week @ 2024-04-17

84,488 downloads per month
Used in 11 crates (8 directly)

MIT license

130KB
3.5K SLoC

hjson-rust for serde

Build Status crate

Hjson Intro

{
  # specify rate in requests/second (because comments are helpful!)
  rate: 1000

  // prefer c-style comments?
  /* feeling old fashioned? */

  # did you notice that rate doesn't need quotes?
  hey: look ma, no quotes for strings either!

  # best of all
  notice: []
  anything: ?

  # yes, commas are optional!
}

The Rust implementation of Hjson is based on the Serde JSON Serialization Library. For other platforms see hjson.org.

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 = "*"
serde-hjson = "*"

From the Commandline

Install with cargo install hjson

Hjson, the Human JSON.

Usage:
  hjson [options]
  hjson [options] <input>
  hjson (-h | --help)
  hjson (-V | --version)

Options:
  -h --help     Show this screen.
  -j            Output as formatted JSON.
  -c            Output as JSON.
  -V --version  Show version.

Sample:

  • run hjson test.json > test.hjson to convert to Hjson
  • run hjson -j test.hjson > test.json to convert to JSON

Usage

extern crate serde;
extern crate serde_hjson;

use serde_hjson::{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> = serde_hjson::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 = serde_hjson::to_string(&sample).unwrap();
    println!("Hjson:\n{}", sample2);
}

API

see Rust doc

History

see history.md

Dependencies

~2.8–4MB
~68K SLoC