JSON Serialization for Rust
Rust
Latest commit f0e7626 Dec 7, 2016 @dtolnay dtolnay Release 0.8.4
Permalink
Failed to load latest commit information.
json Release 0.8.4 Dec 7, 2016
json_tests Release 0.8.4 Dec 7, 2016
.gitignore Initial import of serde_json Aug 25, 2015
.travis.yml Drop testing on rust 1.10 Nov 19, 2016
LICENSE Add licenses Aug 30, 2015
LICENSE-APACHE Add licenses Aug 30, 2015
LICENSE-MIT Add licenses Aug 30, 2015
README.md Update to proc_macro Oct 9, 2016
rustfmt.toml Run rustfmt Jul 15, 2016

README.md

Serde JSON Serialization Library

Build status Coverage Status Latest Version

Documentation

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.

Installation

This crate works with Cargo and can be found on crates.io with a Cargo.toml like:

[dependencies]
serde = "0.8"
serde_json = "0.8"

Using Serde JSON

serde_json is very simple to use out of the box:

extern crate serde;
extern crate serde_json;

use serde_json::Map;

fn main() {
    let mut map = Map::new();
    map.insert("x".to_string(), 1.0);
    map.insert("y".to_string(), 2.0);

    let s = serde_json::to_string(&map).unwrap();
    assert_eq!(s, "{\"x\":1.0,\"y\":2.0}");

    let deserialized_map: Map<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_derive. First add this to Cargo.toml:

[dependencies]
...
serde = "0.8"
serde_derive = "0.8"
...

Then run:

#![feature(proc_macro)]

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
    x: f64,
    y: f64,
}

fn main() {
    let point = Point { x: 1.0, y: 2.0 };

    let s = serde_json::to_string(&point).unwrap();
    assert_eq!(s, "{\"x\":1.0,\"y\":2.0}");

    let deserialized_point: Point = serde_json::from_str(&s).unwrap();
    assert_eq!(point, deserialized_point);
}