Releases: jonasbb/serde_with
serde_with_macros v2.0.1
Changed
- Warn if
serde_as
is used on an enum variant.
Attributes on enum variants were never supported.
But#[serde(with = "...")]
can be added on variants, such that some confusion can occur when migration (#499).
serde_with v2.0.0
Added
-
Make
JsonString<T>
smarter by allowing nestingserde_as
definitions.
This allows applying custom serialization logic, before the value gets converted into a JSON string.// Rust #[serde_as(as = "JsonString<Vec<(JsonString, _)>>")] value: BTreeMap<[u8; 2], u32>, // JSON {"value":"[[\"[1,2]\",3],[\"[4,5]\",6]]"}
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)]
. -
NoneAsEmptyString
andstring_empty_as_none
use a different serialization bound (#388).Both types used
AsRef<str>
as the serialization bound.
This is limiting for non-string types likeOption<i32>
.
The deserialization often was already more flexible, due to theFromStr
bound.For most std types this should have little impact, as the types implementing
AsRef<str>
mostly implementDisplay
, too, such asString
,Cow<str>
, orRc<str>
. -
Bump MSRV to 1.60. This is required for the optional dependency feature syntax in cargo.
Removed
-
Remove old module based conversions.
The newer
serde_as
based conversions are preferred.-
seq_display_fromstr
: UseDisplayFromStr
in combination with your container type:#[serde_as(as = "BTreeSet<DisplayFromStr>")] addresses: BTreeSet<Ipv4Addr>, #[serde_as(as = "Vec<DisplayFromStr>")] bools: Vec<bool>,
-
tuple_list_as_map
: UseBTreeMap
on aVec
of tuples:#[serde_as(as = "BTreeMap<_, _>")] // HashMap will also work s: Vec<(i32, String)>,
-
map_as_tuple_list
can be replaced with#[serde_as(as = "Vec<(_, _)>")]
. -
display_fromstr
can be replaced with#[serde_as(as = "DisplayFromStr")]
. -
bytes_or_string
can be replaced with#[serde_as(as = "BytesOrString")]
. -
default_on_error
can be replaced with#[serde_as(as = "DefaultOnError")]
. -
default_on_null
can be replaced with#[serde_as(as = "DefaultOnNull")]
. -
string_empty_as_none
can be replaced with#[serde_as(as = "NoneAsEmptyString")]
. -
StringWithSeparator
can now only be used inserde_as
.
The definition of theSeparator
trait and its implementations have been moved to theformats
module. -
json::nested
can be replaced with#[serde_as(as = "json::JsonString")]
.
-
-
Remove previously deprecated modules.
sets_first_value_wins
btreemap_as_tuple_list
andhashmap_as_tuple_list
can be replaced with#[serde_as(as = "Vec<(_, _)>")]
.
serde_with_macros v2.0.0
No changes compared to 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 supressed by using#[serde_as(no_default)]
or#[serde_as(as = "Option<S>", no_default)]
.
Fixed
- Make the documentation clearer by stating that the
#[serde_as]
and#[skip_serializing_none]
attributes must always be placed before#[derive]
.
serde_with 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)]
. -
NoneAsEmptyString
andstring_empty_as_none
use a different serialization bound (#388).Both types used
AsRef<str>
as the serialization bound.
This is limiting for non-string types likeOption<i32>
.
The deserialization often was already more flexible, due to theFromStr
bound.For most std types this should have little impact, as the types implementing
AsRef<str>
mostly implementDisplay
, too, such asString
,Cow<str>
, orRc<str>
. -
Bump MSRV to 1.60. This is required for the optional dependency feature syntax in cargo.
Removed
-
Remove old module based conversions.
The newer
serde_as
based conversions are preferred.-
seq_display_fromstr
: UseDisplayFromStr
in combination with your container type:#[serde_as(as = "BTreeSet<DisplayFromStr>")] addresses: BTreeSet<Ipv4Addr>, #[serde_as(as = "Vec<DisplayFromStr>")] bools: Vec<bool>,
-
tuple_list_as_map
: UseBTreeMap
on aVec
of tuples:#[serde_as(as = "BTreeMap<_, _>")] // HashMap will also work s: Vec<(i32, String)>,
-
map_as_tuple_list
can be replaced with#[serde_as(as = "Vec<(_, _)>")]
. -
display_fromstr
can be replaced with#[serde_as(as = "DisplayFromStr")]
. -
bytes_or_string
can be replaced with#[serde_as(as = "BytesOrString")]
. -
default_on_error
can be replaced with#[serde_as(as = "DefaultOnError")]
. -
default_on_null
can be replaced with#[serde_as(as = "DefaultOnNull")]
. -
string_empty_as_none
can be replaced with#[serde_as(as = "NoneAsEmptyString")]
. -
StringWithSeparator
can now only be used inserde_as
.
The definition of theSeparator
trait and its implementations have been moved to theformats
module. -
json::nested
can be replaced with#[serde_as(as = "json::JsonString")]
.
-
-
Remove previously deprecated modules.
sets_first_value_wins
btreemap_as_tuple_list
andhashmap_as_tuple_list
can be replaced with#[serde_as(as = "Vec<(_, _)>")]
.
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