mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-26 00:27:56 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a5d0703e44 | |||
| 0a32cea26e | |||
| da4e37d3f5 |
@@ -579,6 +579,69 @@ impl serde::de::Visitor for PointVisitor {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Design Considerations and tradeoffs for Serializers and Deserializers
|
||||||
|
=====================================================================
|
||||||
|
|
||||||
|
Serde serialization and deserialization implementations are written in such a
|
||||||
|
way that they err on being able to represent more values, and also provide
|
||||||
|
better error messages when they are passed an incorrect type to deserialize
|
||||||
|
from. For example, by default, it is a syntax error to deserialize a `String`
|
||||||
|
into an `Option<String>`. This is implemented such that it is possible to
|
||||||
|
distinguish between the values `None` and `Some(())`, if the serialization
|
||||||
|
format supports option types.
|
||||||
|
|
||||||
|
However, many formats do not have option types, and represents optional values
|
||||||
|
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
|
||||||
|
...
|
||||||
|
|
||||||
|
fn visit_none(&mut self) -> Result<(), Self::Error> {
|
||||||
|
self.visit_unit()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_some<T>(&mut self, value: T) -> Result<(), Self::Error> {
|
||||||
|
value.serialize(self)
|
||||||
|
}
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
For deserialization, this can be implemented by way of the
|
||||||
|
`Deserializer::visit_option` hook, which presumes that there is some ability to peek at what is the
|
||||||
|
next value in the serialized token stream. This following example is from
|
||||||
|
[serde_tests::TokenDeserializer](https://github.com/serde-rs/serde/blob/master/serde_tests/tests/token.rs#L435-L454),
|
||||||
|
where it checks to see if the next value is an `Option`, a `()`, or some other
|
||||||
|
value:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
...
|
||||||
|
|
||||||
|
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
match self.tokens.peek() {
|
||||||
|
Some(&Token::Option(false)) => {
|
||||||
|
self.tokens.next();
|
||||||
|
visitor.visit_none()
|
||||||
|
}
|
||||||
|
Some(&Token::Option(true)) => {
|
||||||
|
self.tokens.next();
|
||||||
|
visitor.visit_some(self)
|
||||||
|
}
|
||||||
|
Some(&Token::Unit) => {
|
||||||
|
self.tokens.next();
|
||||||
|
visitor.visit_none()
|
||||||
|
}
|
||||||
|
Some(_) => visitor.visit_some(self),
|
||||||
|
None => Err(Error::EndOfStreamError),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
Annotations
|
Annotations
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "0.6.10"
|
version = "0.6.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
@@ -17,5 +17,5 @@ num-impls = ["num-bigint", "num-complex", "num-rational"]
|
|||||||
num-rational = ["num/rational"]
|
num-rational = ["num/rational"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.0.36", optional = true }
|
clippy = { version = "^0.0.37", optional = true }
|
||||||
num = { version = "^0.1.27", default-features = false }
|
num = { version = "^0.1.27", default-features = false }
|
||||||
|
|||||||
@@ -283,6 +283,13 @@ impl<
|
|||||||
> Visitor for OptionVisitor<T> {
|
> Visitor for OptionVisitor<T> {
|
||||||
type Value = Option<T>;
|
type Value = Option<T>;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_unit<E>(&mut self) -> Result<Option<T>, E>
|
||||||
|
where E: Error,
|
||||||
|
{
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
|
fn visit_none<E>(&mut self) -> Result<Option<T>, E>
|
||||||
where E: Error,
|
where E: Error,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_codegen"
|
name = "serde_codegen"
|
||||||
version = "0.6.10"
|
version = "0.6.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Macros to auto-generate implementations for the serde framework"
|
description = "Macros to auto-generate implementations for the serde framework"
|
||||||
@@ -20,7 +20,7 @@ syntex = { version = "^0.26.0", optional = true }
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
aster = { version = "^0.10.0", default-features = false }
|
aster = { version = "^0.10.0", default-features = false }
|
||||||
clippy = { version = "^0.0.36", optional = true }
|
clippy = { version = "^0.0.37", optional = true }
|
||||||
quasi = { version = "^0.4.0", default-features = false }
|
quasi = { version = "^0.4.0", default-features = false }
|
||||||
quasi_macros = { version = "^0.4.0", optional = true }
|
quasi_macros = { version = "^0.4.0", optional = true }
|
||||||
syntex = { version = "^0.26.0", optional = true }
|
syntex = { version = "^0.26.0", optional = true }
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_macros"
|
name = "serde_macros"
|
||||||
version = "0.6.10"
|
version = "0.6.11"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "Macros to auto-generate implementations for the serde framework"
|
description = "Macros to auto-generate implementations for the serde framework"
|
||||||
@@ -13,7 +13,7 @@ name = "serde_macros"
|
|||||||
plugin = true
|
plugin = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = "^0.0.36"
|
clippy = "^0.0.37"
|
||||||
serde_codegen = { version = "^0.6.10", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
serde_codegen = { version = "^0.6.10", path = "../serde_codegen", default-features = false, features = ["nightly"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_tests"
|
name = "serde_tests"
|
||||||
version = "0.6.2"
|
version = "0.6.3"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
@@ -25,7 +25,7 @@ serde = { version = "*", path = "../serde", features = ["num-impls"] }
|
|||||||
syntex = "^0.26.0"
|
syntex = "^0.26.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clippy = { version = "^0.0.36", optional = true }
|
clippy = { version = "^0.0.37", optional = true }
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "test"
|
name = "test"
|
||||||
|
|||||||
Reference in New Issue
Block a user