Releases: jonasbb/serde_with
serde_with_macros v2.0.0-rc.0
Changed
-
Make
#[serde_as]
behave more intuitive onOption<T>
fields.The
#[serde_as]
macro now detects if a#[serde_as(as = "Option<S>")]
is used on a field of typeOption<T>
and applies#[serde(default)]
to the field.
This restores the ability to deserialize with missing fields and fixes a common annoyance (#183, #185, #311, #417).
This is a breaking change, since now deserialization will pass where it did not before and this might be undesired.The
Option
field and transformation are detected by directly matching on the type name.
These variants are detected asOption
.Option
std::option::Option
, with or without leading::
core::option::Option
, with or without leading::
If an existing
default
attribute is detected, the attribute is not applied again.
This behavior can be suppressed by using#[serde_as(no_default)]
or#[serde_as(as = "Option<S>", no_default)]
.
serde_with v1.14.0
Added
-
Add support for
time
crate v0.3 #450time::Duration
can now be serialized with theDurationSeconds
and related converters.// Rust #[serde_as(as = "serde_with::DurationSeconds<u64>")] value: Duration, // JSON "value": 86400,
time::OffsetDateTime
andtime::PrimitiveDateTime
can now be serialized with theTimestampSeconds
and related converters.// Rust #[serde_as(as = "serde_with::TimestampMicroSecondsWithFrac<String>")] value: time::PrimitiveDateTime, // JSON "value": "1000000",
time::OffsetDateTime
can be serialized in string format in different well-known formats.
Two formats are supported,time::format_description::well_known::Rfc2822
andtime::format_description::well_known::Rfc3339
.// Rust #[serde_as(as = "time::format_description::well_known::Rfc2822")] rfc_2822: OffsetDateTime, #[serde_as(as = "Vec<time::format_description::well_known::Rfc3339>")] rfc_3339: Vec<OffsetDateTime>, // JSON "rfc_2822": "Fri, 21 Nov 1997 09:55:06 -0600", "rfc_3339": ["1997-11-21T09:55:06-06:00"],
-
Deserialize
bool
from integers #456 462Deserialize an integer and convert it into a
bool
.
BoolFromInt<Strict>
(default) deserializes 0 tofalse
and1
totrue
, other numbers are errors.
BoolFromInt<Flexible>
deserializes any non-zero astrue
.
Serialization only emits 0/1.// Rust #[serde_as(as = "BoolFromInt")] // BoolFromInt<Strict> b: bool, // JSON "b": 1,
Changed
- Bump MSRV to 1.53, since the new dependency
time
requires that version.
Fixed
- Make the documentation clearer by stating that the
#[serde_as]
and#[skip_serializing_none]
attributes must always be places before#[derive]
.
serde_with v1.13.0
Added
-
Added support for
indexmap::IndexMap
andindexmap::IndexSet
types. #431, #436Both types are now compatible with these functions:
maps_duplicate_key_is_error
,maps_first_key_wins
,sets_duplicate_value_is_error
,sets_last_value_wins
.
serde_as
integration is provided by implementing bothSerializeAs
andDeserializeAs
for both types.
IndexMap
s can also be serialized as a list of types via theserde_as(as = "Vec<(_, _)>")
annotation.All implementations are gated behind the
indexmap
feature.Thanks to @jgrund for providing parts of the implementation.
serde_with v1.12.1
serde_with_macros v1.5.2
serde_with v1.12.0
Added
-
Deserialize a
Vec
and skip all elements failing to deserialize #383VecSkipError
acts like aVec
, but elements which fail to deserialize, like the"Yellow"
are ignored.#[derive(serde::Deserialize)] enum Color { Red, Green, Blue, } // JSON "colors": ["Blue", "Yellow", "Green"], // Rust #[serde_as(as = "VecSkipError<_>")] colors: Vec<Color>, // => vec![Blue, Green]
Thanks to @hdhoang for creating the PR.
-
Transform between maps and
Vec<Enum>
#375The new
EnumMap
type convertsVec
of enums into a single map.
The key is the enum variant name, and the value is the variant value.// Rust VecEnumValues(vec![ EnumValue::Int(123), EnumValue::String("Foo".to_string()), EnumValue::Unit, EnumValue::Tuple(1, "Bar".to_string()), EnumValue::Struct { a: 666, b: "Baz".to_string(), }, ] // JSON { "Int": 123, "String": "Foo", "Unit": null, "Tuple": [ 1, "Bar", ], "Struct": { "a": 666, "b": "Baz", } }
Changed
- The
Timestamp*Seconds
andTimestamp*SecondsWithFrac
types can now be used withchrono::NaiveDateTime
. #389
serde_with v1.11.0
Added
-
Serialize bytes as base64 encoded strings.
The character set and padding behavior can be configured.// Rust #[serde_as(as = "serde_with::base64::Base64")] value: Vec<u8>, #[serde_as(as = "Base64<Bcrypt, Unpadded>")] bcrypt_unpadded: Vec<u8>, // JSON "value": "SGVsbG8gV29ybGQ=", "bcrypt_unpadded": "QETqZE6eT07wZEO",
-
The minimal supported Rust version (MSRV) is now specified in the
Cargo.toml
via therust-version
field. The field is supported in Rust 1.56 and has no effect on versions before.More details: https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-rust-version-field
Fixed
- Fixed RUSTSEC-2020-0071 in the
time
v0.1 dependency, but changing the feature flags of thechrono
dependency. This should not change anything. Crates requiring theoldtime
feature ofchrono
can enable it separately.
serde_with_macros v1.5.1
Added
-
The minimal supported Rust version (MSRV) is now specified in the
Cargo.toml
via therust-version
field. The field is supported in Rust 1.56 and has no effect on versions before.More details: https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-rust-version-field
serde_with v1.10.0
Added
-
Add
BorrowCow
which instructs serde to borrow data during deserialization ofCow<'_, str>
,Cow<'_, [u8]>
, orCow<'_, [u8; N]>
. (#347)
The implementation is for serde#2072 and serde#2016, about#[serde(borrow)]
not working forOption<Cow<'a, str>>
.#[serde_as] #[derive(Deserialize, Serialize)] struct Data<'a> { #[serde_as(as = "Option<[BorrowCow; 1]>")] nested: Option<[Cow<'a, str>; 1]>, }
The
#[serde(borrow)]
annotation is automatically added by the#[serde_as]
attribute.
Changed
- Bump MSRV to 1.46, since the dev-dependency bitflags requires that version now.
flattened_maybe!
no longer requires theserde_with
crate to be available with a specific name.
This allows renaming the crate or usingflattened_maybe!
through a re-export without any complications.
serde_with_macros v1.5.0
Added
- Add the attribute
#[serde(borrow)]
on a field ifserde_as
is used in combination with theBorrowCow
type.