https://github.com/hjson/hjson-rust
Hjson for Rust
https://github.com/hjson/hjson-rust
hjson rust serde
Last synced: 5 months ago
JSON representation
Hjson for Rust
- Host: GitHub
- URL: https://github.com/hjson/hjson-rust
- Owner: hjson
- License: mit
- Created: 2016-06-14T21:54:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-25T13:22:37.000Z (about 1 year ago)
- Last Synced: 2025-05-01T17:54:38.448Z (5 months ago)
- Topics: hjson, rust, serde
- Language: Rust
- Homepage: https://hjson.github.io/
- Size: 798 KB
- Stars: 111
- Watchers: 7
- Forks: 33
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: history.md
- License: LICENSE
Awesome Lists containing this project
README
# hjson-rust for serde
[](https://github.com/hjson/hjson-rust/actions)
[](https://crates.io/crates/serde-hjson)
```
{
# 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](https://github.com/serde-rs/json). For other platforms see [hjson.github.io](https://hjson.github.io).
This crate is a Rust library for parsing and generating Human JSON [Hjson](https://hjson.github.io). It is built upon [Serde](https://github.com/serde-rs/serde), a high performance generic serialization framework.
# Install
This crate works with Cargo and can be found on [crates.io](https://crates.io/crates/serde-hjson) with a `Cargo.toml` like:
```toml
[dependencies]
serde = "*"
serde-hjson = "*"
```## From the Commandline
Install with `cargo install hjson`
```
Hjson, the Human JSON.Usage:
hjson [options]
hjson [options]
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
```rust
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 = 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 = 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](http://hjson.github.io/hjson-rust/serde_hjson/)
# History
[see history.md](history.md)