Skip to main content

Module json

Module json 

Source
Expand description

Implement Haystack JSON (Hayson) Encoding

The implementation is based on the excellent serde_json de/serialization framework.

§Examples

Encoding

use libhaystack::val::*;
let json = serde_json::to_string(&Value::make_true());

assert_eq!(json.unwrap(), "true");

let json = serde_json::to_string(&Value::make_coord_from(29.9792, 31.1342));

assert_eq!(json.unwrap(), r#"{"_kind":"coord","lat":29.9792,"lng":31.1342}"#);

Decoding

use libhaystack::val::*;
let list: Vec<Value> = serde_json::from_str(r#"[100, "foo", {"_kind": "marker"}]"#).expect("Valid JSON");

assert_eq!(list.len(), 3);

assert_eq!(list, vec![Value::make_number(100.0), Value::make_str("foo"), Value::make_marker()]);

Modules§

decode
Hayson decoder
encode
Hayson encoder