This crate is a Rust library for parsing and generating the JSON (JavaScript Object Notation) file format. It is built upon Serde, a high performance generic serialization framework.
This crate works with Cargo and can be found on
crates.io with a Cargo.toml like:
[dependencies]
serde = "*"serde_json = "*"serde_json is very simple to use out of the box:
externcrate serde;externcrate serde_json;use std::collections::BTreeMap;fnmain(){letmut map = BTreeMap::new();
map.insert("x".to_string(),1.0);
map.insert("y".to_string(),2.0);let s = serde_json::to_string(&map);assert_eq!(s,"{\"x\":1,\"y\":2}");let deserialized_map:BTreeMap<String,f64> = serde_json::from_str(s).unwrap();assert_eq!(map, deserialized_map);}It also can be used with Serde's automatic serialization library,
serde_macros. First add this to Cargo.toml:
[dependencies]
...serde = "*"serde_macros = "*"...Then run:
#![feature(plugin)]#![plugin(serde_macros)]externcrate serde;externcrate serde_json;#[derive(Debug,PartialEq,Serialize,Deserialize)]structPoint{x:f64,y:f64,}fnmain(){let point = Point{x:1.0,y:2.0};let s = serde_json::to_string(&point);assert_eq!(s,"{\"x\":1,\"y\":2}");let deserialized_point:Point = serde_json::from_str(s).unwrap();assert_eq!(point, deserialized_point);}