mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-15 05:01:02 +00:00
docs(readme): Improve the readme
This commit is contained in:
@@ -17,8 +17,8 @@ Documentation is available at:
|
|||||||
* [serde\_json](https://serde-rs.github.io/serde/serde_json/serde_json/index.html)
|
* [serde\_json](https://serde-rs.github.io/serde/serde_json/serde_json/index.html)
|
||||||
* [serde\_codegen](https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html)
|
* [serde\_codegen](https://serde-rs.github.io/serde/serde_codegen/serde_codegen/index.html)
|
||||||
|
|
||||||
Using Serde
|
Using Serde with Nightly Rust and serde\_macros
|
||||||
===========
|
===============================================
|
||||||
|
|
||||||
Here is a simple example that demonstrates how to use Serde by serializing and
|
Here is a simple example that demonstrates how to use Serde by serializing and
|
||||||
deserializing to JSON. Serde comes with some powerful code generation libraries
|
deserializing to JSON. Serde comes with some powerful code generation libraries
|
||||||
@@ -76,6 +76,9 @@ When run, it produces:
|
|||||||
Point { x: 1, y: 2 }
|
Point { x: 1, y: 2 }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Using Serde with Stable Rust, syntex, and serde\_codegen
|
||||||
|
========================================================
|
||||||
|
|
||||||
Stable Rust is a little more complicated because it does not yet support
|
Stable Rust is a little more complicated because it does not yet support
|
||||||
compiler plugins. Instead we need to use the code generation library
|
compiler plugins. Instead we need to use the code generation library
|
||||||
[syntex](https://github.com/erickt/rust-syntex) for this:
|
[syntex](https://github.com/erickt/rust-syntex) for this:
|
||||||
@@ -215,6 +218,19 @@ include!(concat!(env!("OUT_DIR"), "/main.rs"));
|
|||||||
|
|
||||||
The `src/main.rs.in` is the same as before.
|
The `src/main.rs.in` is the same as before.
|
||||||
|
|
||||||
|
Then to run with stable:
|
||||||
|
|
||||||
|
```
|
||||||
|
% cargo build
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
Or with nightly:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
% cargo build --features nightly --no-default-features
|
||||||
|
...
|
||||||
|
|
||||||
Serialization without Macros
|
Serialization without Macros
|
||||||
============================
|
============================
|
||||||
|
|
||||||
@@ -311,6 +327,8 @@ as a named map. Its visitor uses a simple state machine to iterate through all
|
|||||||
the fields:
|
the fields:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
extern crate serde;
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
@@ -479,6 +497,13 @@ deserializes an enum variant from a string. So for our `Point` example from
|
|||||||
before, we need to generate:
|
before, we need to generate:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
extern crate serde;
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
enum PointField {
|
enum PointField {
|
||||||
X,
|
X,
|
||||||
Y,
|
Y,
|
||||||
@@ -507,11 +532,7 @@ impl serde::Deserialize for PointField {
|
|||||||
deserializer.visit(PointFieldVisitor)
|
deserializer.visit(PointFieldVisitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
|
||||||
|
|
||||||
This is then used in our actual deserializer:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
impl serde::Deserialize for Point {
|
impl serde::Deserialize for Point {
|
||||||
fn deserialize<D>(deserializer: &mut D) -> Result<Point, D::Error>
|
fn deserialize<D>(deserializer: &mut D) -> Result<Point, D::Error>
|
||||||
where D: serde::de::Deserializer
|
where D: serde::de::Deserializer
|
||||||
|
|||||||
Reference in New Issue
Block a user