Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codegen expr magic for default, skip serializing, and alternative serializers #237

Closed
wants to merge 76 commits into from

Conversation

erickt
Copy link
Member

@erickt erickt commented Feb 16, 2016

This is an alternative implementation of #214, #198, #208 and implements #90 and #216. This allows one to write:

#[derive(Serialize, Deserialize)]
struct Struct {
    #[serde(default="123")]
    a: i32,
    #[serde(skip_serializing_if="self.b == 123")]
    b: i32,
    #[serde(serialize_with="(self.b == 123).serialize(serializer)"]
    c: i32,
    #[serde(deserialize_with="Ok(if try!(bool::deserialize(deserializer) { 123) } else { 0 })"]
    d: i32,
}

cc @oli-obk, @target-san, @arcnmx

skade and others added 30 commits September 24, 2015 07:03
When using a `ValueDeserializer` errors produced with the methods
`type_mismatch`, `length_mismatch` and `syntax` were not forwarded
correctly. These methods now store all information in the enum
`de::value::Error`.
This makes it possible to remove the `From<de::value::Error>` trait
bound from the `de::Error` trait.

An example for using a `ValueDeserializer` to forward deserializing to
another type's visitor:

```rust
fn visit_u8<E>(&mut self, value: u8) -> Result<Self::Value, E>
    where E: de::Error
{
    try!(Deserialize::deserialize(&mut value.into_deserializer()))
}
```

Please note that there is an regression in serde-rs/json which needs to
be fixed.
This deserializers can be used to delegate deserialization to another
type's visitor.

Example:
```json
fn visit_map<V>(&mut self, visitor: V) -> Result<Self::Value, V::Error>
    where V: de::MapVisitor
{
    let mut deserializer = de::value::MapVisitorDeserializer::new(visitor);
    try!(Deserialize::deserialize(&mut deserializer))
}
```
chore(git): Ignore swap files
fix(serde-rs#151): renaming Deserializer::visit_* to Deserializer::deserialize_*
# Conflicts:
#	serde_codegen/Cargo.toml
feat(cargo): Quasi version to 0.3.9
# Conflicts:
#	serde_codegen/Cargo.toml
Have serde::de::Error require std::error::Error
Use SVG version of Travis CI build status badge
# Conflicts:
#	serde_codegen/Cargo.toml
* Added codegen for disallow_unknown
* ... with new default to ignore unknown values during deserialization
* Added ContainerAttrs
jwilm and others added 27 commits January 28, 2016 11:49
IgnoredAny was calling `deserializer.deserialize` directly which is
guaranteed to Error for certain formats like redis and bincode. This
adds a `deserialize_ignored_any` method to hint to such implementations.
feat(de): Support hinting for IgnoredAny
feat(ser): Add ser::Error trait; avoid panic when serializing Paths
…pathbuf

Use deserializer.deserialize_string for PathBuf
This makes it more consistent with the naming style used throughout
the rest of serde.
This feature has never been used, and it's complicating the
implementation of serde-rs#207. We could restore this functionality if there is
ever interest in it.

Closes serde-rs#211.
…d deserializing

According to serde-rs#61, if one uses serde to serialize requests that pass
url-encoded parameters to a server, it might get responses back
with a different capitalization scheme. This patch restores the
behavior implemented in serde-rs#62.

# Conflicts:
#	serde_codegen/src/attr.rs
#	serde_codegen/src/de.rs
#	serde_tests/tests/test_annotations.rs
Remove support for format-specific renames, replace with ser or de specific renames
Was previously nightly. This resulted in compilation error when the
clippy feature was not enabled because the clippy crate could not be
found.
plugin(clippy) now relies on feature = "clippy"
# Conflicts:
#	serde/src/de/mod.rs
#	serde_codegen/src/attr.rs
#	serde_codegen/src/de.rs
#	serde_codegen/src/lib.rs
# Conflicts:
#	serde_codegen/src/de.rs
This feature adds support for the default to be specified to be
some expression (which unfortunately needs to be parsed from
a string) without needing this value to have an implementation
of `Default`, or for a new-type wrapper in order to provide an
alternative implementation. This expression is run in a function,
and therefore has no access to any of the internal state of
the deserializer.
This allows end users to use an arbitrary expression to decide whether
or not to serialize some field. This expression has access to all the
fields in the struct, but none of the internal state of the Serialize
implementation. For structs, serde implements this by creating a
temporary trait and implementing the struct for it. For struct variants,
the fields are copied by reference into a temporary struct first
before implementing the temporary trait.

This also fixes a bug where the serde_codegen wasn't making calls to
Serializer::serialize_{tuple,struct}_variant{,_elt}.
This allows a field to be serialized with an expression instead
of the default serializer. This simplifies serializing a struct
or enum that contains an external type that doesn't implement
`serde::Serialize`. This expression is passed a variable
`serializer` that needs to be used to serialize the expression.
This allows a field to be deserialized with an expression instead
of the default deserializer. This simplifies deserializing a struct
or enum that contains an external type that doesn't implement
`serde::Deserialize`. This expression is passed a variable
`deserializer` that needs to be used to deserialize the expression.
@erickt
Copy link
Member Author

erickt commented Feb 16, 2016

arg, wrong branch.

@erickt erickt closed this Feb 16, 2016
@erickt
Copy link
Member Author

erickt commented Feb 16, 2016

Pushed up on the right branch to #238.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.