mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-24 01:48:00 +00:00
Move json into it's own crate
Not everyone needs json, so no reason to force people to build it.
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use serde::json;
|
||||
|
||||
// Creating serializable types with serde is quite simple with `serde_macros`. It implements a
|
||||
// syntax extension that automatically generates the necessary serde trait implementations.
|
||||
@@ -18,7 +18,7 @@ fn main() {
|
||||
let point = Point { x: 5, y: 6 };
|
||||
|
||||
// Serializing to JSON is pretty simple by using the `to_string` method:
|
||||
let serialized_point = json::to_string(&point).unwrap();
|
||||
let serialized_point = serde_json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
@@ -26,7 +26,7 @@ fn main() {
|
||||
// {"x":5,"y":6}
|
||||
|
||||
// There is also support for pretty printing using `to_string_pretty`:
|
||||
let serialized_point = json::to_string_pretty(&point).unwrap();
|
||||
let serialized_point = serde_json::to_string_pretty(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
@@ -37,7 +37,7 @@ fn main() {
|
||||
// }
|
||||
|
||||
// Values can also be deserialized with the same style using `from_str`:
|
||||
let deserialized_point: Point = json::from_str(&serialized_point).unwrap();
|
||||
let deserialized_point: Point = serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_point);
|
||||
// prints:
|
||||
@@ -46,16 +46,18 @@ fn main() {
|
||||
|
||||
// `Point`s aren't the only type that can be serialized to. Because `Point` members have the
|
||||
// same type, they can be also serialized into a map. Also,
|
||||
let deserialized_map: BTreeMap<String, i64> = json::from_str(&serialized_point).unwrap();
|
||||
let deserialized_map: BTreeMap<String, i64> =
|
||||
serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_map);
|
||||
// prints:
|
||||
//
|
||||
// {"x": 5, "y": 6}
|
||||
|
||||
// If you need to accept arbitrary data, you can also deserialize into `json::Value`, which
|
||||
// can represent all JSON values.
|
||||
let deserialized_value: json::Value = json::from_str(&serialized_point).unwrap();
|
||||
// If you need to accept arbitrary data, you can also deserialize into `serde_json::Value`,
|
||||
// which can represent all JSON values.
|
||||
let deserialized_value: serde_json::Value =
|
||||
serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_value);
|
||||
// prints:
|
||||
|
||||
Reference in New Issue
Block a user