doc(annotations): Add docs for the latest documentation

This commit is contained in:
Erick Tryzelaar 2016-02-18 19:32:10 -08:00
parent c268eb2258
commit dbd05ae9cc

View File

@ -670,22 +670,36 @@ Annotations
`serde_codegen` and `serde_macros` support annotations that help to customize
how types are serialized. Here are the supported annotations:
Container Annotations:
| Annotation | Function |
| ---------- | -------- |
| `#[serde(rename="name")` | Serialize and deserialize this container with the given name |
| `#[serde(rename(serialize="name1"))` | Serialize this container with the given name |
| `#[serde(rename(deserialize="name1"))` | Deserialize this container with the given name |
| `#[serde(deny_unknown_fields)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
Variant Annotations:
| Annotation | Function |
| ---------- | -------- |
| `#[serde(rename="name")` | Serialize and deserialize this variant with the given name |
| `#[serde(rename(serialize="name1"))` | Serialize this variant with the given name |
| `#[serde(rename(deserialize="name1"))` | Deserialize this variant with the given name |
Field Annotations:
| Annotation | Function |
| ---------- | -------- |
| `#[serde(rename(json="name1", xml="name2"))` | Serialize this field with the given name for the given formats |
| `#[serde(rename="name")` | Serialize and deserialize this field with the given name |
| `#[serde(rename(serialize="name1"))` | Serialize this field with the given name |
| `#[serde(rename(deserialize="name1"))` | Deserialize this field with the given name |
| `#[serde(default)` | If the value is not specified, use the `Default::default()` |
| `#[serde(rename="name")` | Serialize this field with the given name |
| `#[serde(default="$expr")` | If the value is not specified, use the `$expr` expression |
| `#[serde(skip_serializing)` | Do not serialize this value |
| `#[serde(skip_serializing_if_empty)` | Do not serialize this value if `$value.is_empty()` is `true` |
| `#[serde(skip_serializing_if_none)` | Do not serialize this value if `$value.is_none()` is `true` |
Structure Annotations:
| Annotation | Function |
| ---------- | -------- |
| `#[serde(deny_unknown_fields)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. |
| `#[serde(skip_serializing_if="$expr")` | Do not serialize this value if the `$expr` expression returns true |
| `#[serde(serialize_with="$expr")` | Use the `$expr` expression to serialize this field |
| `#[serde(deserialize_with="$expr")` | Use the `$expr` expression to deserialize this field |
Serialization Formats Using Serde