Crate yojson_rs[][src]

Yojson parser

crates.io docs.rs Build Status source badge license badge

This library parses JSON data (yojson format) into a nested Rust tree data structure.

Yojson values

A value in Yojson is represented with the Value enum in this crate:

pub enum Value {
  Null,
  Bool(bool),
  Integer(i64),
  Float(f64),
  String(String),
  Assoc(Assoc),
  Array(Array),
  Tuple(Vec<Value>),
  Variant(Variant),
}

The Yojson format is an extension of the JSON format. See “Yojson format document” for more information.

  • Tuples: like JSON arrays but within parentheses instead of square brackets, such as (1.23, 4.56).
  • Variants without argument: <"Foo">.
  • Variants with one argument: <"Bar": 123>.
  • Unquoted field names and variants are accepted if they match the pattern [A-Za-z][A-Za-z_0-9]*: { x: <Foo>, "#y": <Bar2> }.
  • Comments: /* multiline comment */ and // end-of-line comment.
  • Special numeric entities: [ Infinity, -Infinity, NaN ].

Parsing JSON

Parse JSON data.

use yojson_rs;

let json = r#"
  {
    x : 123,
    y : {
           "y1" : "abc\ndef\u0021",
           "y2" : [null, 123.45, (12, "y3")]
        },
    z : NaN
  }
  "#;
assert!(yojson_rs::parser::parse(json).is_ok());

Convert to a JSON string.

A data structure can be converted to a JSON string by to_string.

use yojson_rs;

let json_str = r#"
  {
    x : 123,
    y : {
           "y1" : "abc\ndef\u0021",
           "y2" : [null, 123.45, (12, "y3")]
        },
    z : NaN
  }
  "#;
let json = yojson_rs::parser::parse(json_str).unwrap();
println!("{}", yojson_rs::to_string(json));

Modules

parser

Definition of a parse fucntion.

value

Definition of a Yojson value

Functions

to_string

Convert to a JSON string.