mirror of
https://github.com/serde-rs/json.git
synced 2026-01-25 10:06:08 +00:00
feat(value): implement RFC6901 JSON Pointer Add a method `pointer(&str)` to `Value`. Deprecate `lookup(&str)`. Bump version to 0.7.1. Closes #9 Notes: * It should be possible to add a `pointer_mut(&str)` method to Value. This would allow to add and modify values. (Maybe even a delete method) I failed to add such a method because of borrow checker. * Should [RFC6902 JSON Patch](https://tools.ietf.org/html/rfc6902) be implemented or is this something for a separate crate? (Patch is based on Pointer)
Serde JSON Serialization Library
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 = "*"
serde_json = "*"
Using Serde JSON
serde_json is very simple to use out of the box:
extern crate serde;
extern crate serde_json;
use std::collections::BTreeMap;
fn main() {
let mut map = BTreeMap::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,\"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(custom_derive, plugin)]
#![plugin(serde_macros)]
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,\"y\":2}");
let deserialized_point: Point = serde_json::from_str(&s).unwrap();
assert_eq!(point, deserialized_point);
}
Languages
Rust
100%
