Skip to content

serde_with v1.7.0

Compare
Choose a tag to compare
@github-actions github-actions released this 24 Mar 21:52
adf9026

Added

  • Add support for arrays of arbitrary size. (#272)
    This feature requires Rust 1.51+.

    // Rust
    #[serde_as(as = "[[_; 64]; 33]")]
    value: [[u8; 64]; 33],
    
    // JSON
    "value": [[0,0,0,0,0,...], [0,0,0,...], ...],

    Mapping of arrays was available before, but limited to arrays of length 32.
    All conversion methods are available for the array elements.

    This is similar to the existing serde-big-array crate with three important improvements:

    1. Support for the serde_as annotation.
    2. Supports non-copy elements (see serde-big-array#6).
    3. Supports arbitrary nestings of arrays (see serde-big-array#7).
  • Arrays with tuple elements can now be deserialized from a map. (#272)
    This feature requires Rust 1.51+.

    // Rust
    #[serde_as(as = "BTreeMap<_, _>")]
    value: [(String, u16); 3],
    
    // JSON
    "value": {
        "a": 1,
        "b": 2,
        "c": 3
    },
  • The Bytes type is heavily inspired by serde_bytes and ports it to the serde_as system. (#277)

    #[serde_as(as = "Bytes")]
    value: Vec<u8>,

    Compared to serde_bytes these improvements are available

    1. Integration with the serde_as annotation (see serde-bytes#14).
    2. Implementation for arrays of arbitrary size (Rust 1.51+) (see serde-bytes#26).
  • The OneOrMany allows to deserialize a Vec from either a single element or a sequence. (#281)

    #[serde_as(as = "OneOrMany<_>")]
    cities: Vec<String>,

    This allows to deserialize from either cities: "Berlin" or cities: ["Berlin", "Paris"].
    The serialization can be configured to always emit a list with PreferMany or emit a single element with PreferOne.