doc(readme): Update readme to compile with latest serde

This commit is contained in:
Erick Tryzelaar 2016-02-26 11:03:06 -08:00
parent 4117b1c054
commit 6ea632e98b

View File

@ -100,7 +100,7 @@ serde_json = "*"
`src/main.rs`:
```rust
```rust,ignore
extern crate serde;
extern crate serde_json;
@ -109,7 +109,7 @@ include!(concat!(env!("OUT_DIR"), "/main.rs"));
`src/main.rs.in`:
```rust
```rust,ignore
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
@ -130,7 +130,7 @@ fn main() {
`build.rs`
```rust
```rust,ignore
extern crate syntex;
extern crate serde_codegen;
@ -189,7 +189,7 @@ serde_macros = { version = "*", optional = true }
`build.rs`:
```rust
```rust,ignore
#[cfg(not(feature = "serde_macros"))]
mod inner {
extern crate syntex;
@ -223,7 +223,7 @@ fn main() {
`src/main.rs`:
```rust
```rust,ignore
#![cfg_attr(feature = "serde_macros", feature(custom_derive, plugin))]
#![cfg_attr(feature = "serde_macros", plugin(serde_macros))]
@ -248,7 +248,7 @@ Then to run with stable:
Or with nightly:
```rust
```
% cargo build --features nightly --no-default-features
...
```
@ -272,7 +272,7 @@ The
[Serializer](http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html)
is threaded through the type:
```rust
```rust,ignore
impl serde::Serialize for i32 {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: serde::Serializer,
@ -289,7 +289,7 @@ to the
[Serializer](http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html)
in order to walk through the type:
```rust
```rust,ignore
impl<K, V> Serialize for BTreeMap<K, V>
where K: Serialize + Ord,
V: Serialize,
@ -350,6 +350,7 @@ the fields:
```rust
extern crate serde;
extern crate serde_json;
struct Point {
x: i32,
@ -391,6 +392,13 @@ impl<'a> serde::ser::MapVisitor for PointMapVisitor<'a> {
}
}
}
fn main() {
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
println!("{}", serialized);
}
```
Deserialization without Macros
@ -405,7 +413,7 @@ implementation. It passes a
The [Visitor](http://serde-rs.github.io/serde/serde/serde/de/trait.Visitor.html)
can create the `i32` from a variety of different types:
```rust
```rust,ignore
impl Deserialize for i32 {
fn deserialize<D>(deserializer: &mut D) -> Result<i32, D::Error>
where D: serde::Deserializer,
@ -442,13 +450,13 @@ which allows a
[Deserialize](http://serde-rs.github.io/serde/serde/serde/de/trait.Deserialize.html)
to generate an error for a few common error conditions. Here's how it could be used:
```rust
```rust,ignore
...
fn visit_string<E>(&mut self, _: String) -> Result<i32, E>
where E: Error,
{
Err(serde::de::Error::syntax("expect a string"))
Err(serde::de::Error::custom("expect a string"))
}
...
@ -460,7 +468,7 @@ Maps follow a similar pattern as before, and use a
to walk through the values generated by the
[Deserializer](http://serde-rs.github.io/serde/serde/serde/de/trait.Deserializer.html).
```rust
```rust,ignore
impl<K, V> serde::Deserialize for BTreeMap<K, V>
where K: serde::Deserialize + Eq + Ord,
V: serde::Deserialize,
@ -510,7 +518,6 @@ impl<K, V> serde::de::Visitor for BTreeMapVisitor<K, V>
Ok(values)
}
}
```
Deserializing structs goes a step further in order to support not allocating a
@ -520,7 +527,9 @@ before, we need to generate:
```rust
extern crate serde;
extern crate serde_json;
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
@ -546,7 +555,7 @@ impl serde::Deserialize for PointField {
match value {
"x" => Ok(PointField::X),
"y" => Ok(PointField::Y),
_ => Err(serde::de::Error::syntax("expected x or y")),
_ => Err(serde::de::Error::custom("expected x or y")),
}
}
}
@ -598,6 +607,15 @@ impl serde::de::Visitor for PointVisitor {
Ok(Point{ x: x, y: y })
}
}
fn main() {
let serialized = "{\"x\":1,\"y\":2}";
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
println!("{:?}", deserialized);
}
```
Design Considerations and tradeoffs for Serializers and Deserializers
@ -616,7 +634,7 @@ as either a `null`, or some other value. Serde `Serializer`s and
`Deserializer`s can opt-in support for this. For serialization, this is pretty
easy. Simply implement these methods:
```rust
```rust,ignore
...
fn visit_none(&mut self) -> Result<(), Self::Error> {
@ -636,7 +654,7 @@ next value in the serialized token stream. This following example is from
where it checks to see if the next value is an `Option`, a `()`, or some other
value:
```rust
```rust,ignore
...
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>