From 1d28426d8ca03366e367dbf1939b3284b51f842c Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Sat, 28 May 2022 21:56:46 +0000 Subject: [PATCH] Add Changelog and guide entry --- serde_with/CHANGELOG.md | 17 +++++- .../src/guide/serde_as_transformations.md | 59 ++++++++++++------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/serde_with/CHANGELOG.md b/serde_with/CHANGELOG.md index ec98e2a7..b8d2347a 100644 --- a/serde_with/CHANGELOG.md +++ b/serde_with/CHANGELOG.md @@ -24,7 +24,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. `time::OffsetDateTime` and `time::PrimitiveDateTime` can now be serialized with the `TimestampSeconds` and related converters. - ```rust // Rust #[serde_as(as = "serde_with::TimestampMicroSecondsWithFrac")] @@ -49,6 +48,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. "rfc_3339": ["1997-11-21T09:55:06-06:00"], ``` +* Deserialize `bool` from integers #456 462 + + Deserialize an integer and convert it into a `bool`. + `BoolFromInt` (default) deserializes 0 to `false` and `1` to `true`, other numbers are errors. + `BoolFromInt` deserializes any non-zero as `true`. + Serialization only emits 0/1. + + ```rust + // Rust + #[serde_as(as = "BoolFromInt")] // BoolFromInt + b: bool, + + // JSON + "b": 1, + ``` + ### Changed * Bump MSRV to 1.53, since the new dependency `time` requires that version. diff --git a/serde_with/src/guide/serde_as_transformations.md b/serde_with/src/guide/serde_as_transformations.md index d553920a..9cc30082 100644 --- a/serde_with/src/guide/serde_as_transformations.md +++ b/serde_with/src/guide/serde_as_transformations.md @@ -4,26 +4,27 @@ This page lists the transformations implemented in this crate and supported by ` 1. [Base64 encode bytes](#base64-encode-bytes) 2. [Big Array support](#big-array-support) -3. [Borrow from the input for `Cow` type](#borrow-from-the-input-for-cow-type) -4. [`Bytes` with more efficiency](#bytes-with-more-efficiency) -5. [Convert to an intermediate type using `Into`](#convert-to-an-intermediate-type-using-into) -6. [Convert to an intermediate type using `TryInto`](#convert-to-an-intermediate-type-using-tryinto) -7. [`Default` from `null`](#default-from-null) -8. [De/Serialize into `Vec`, ignoring errors](#deserialize-into-vec-ignoring-errors) -9. [De/Serialize with `FromStr` and `Display`](#deserialize-with-fromstr-and-display) -10. [`Duration` as seconds](#duration-as-seconds) -11. [Hex encode bytes](#hex-encode-bytes) -12. [Ignore deserialization errors](#ignore-deserialization-errors) -13. [`Maps` to `Vec` of enums](#maps-to-vec-of-enums) -14. [`Maps` to `Vec` of tuples](#maps-to-vec-of-tuples) -15. [`NaiveDateTime` like UTC timestamp](#naivedatetime-like-utc-timestamp) -16. [`None` as empty `String`](#none-as-empty-string) -17. [One or many elements into `Vec`](#one-or-many-elements-into-vec) -18. [Pick first successful deserialization](#pick-first-successful-deserialization) -19. [Timestamps as seconds since UNIX epoch](#timestamps-as-seconds-since-unix-epoch) -20. [Value into JSON String](#value-into-json-string) -21. [`Vec` of tuples to `Maps`](#vec-of-tuples-to-maps) -22. [Well-known time formats for `OffsetDateTime`](#well-known-time-formats-for-offsetdatetime) +3. [`bool` from integer](#bool-from-integer) +4. [Borrow from the input for `Cow` type](#borrow-from-the-input-for-cow-type) +5. [`Bytes` with more efficiency](#bytes-with-more-efficiency) +6. [Convert to an intermediate type using `Into`](#convert-to-an-intermediate-type-using-into) +7. [Convert to an intermediate type using `TryInto`](#convert-to-an-intermediate-type-using-tryinto) +8. [`Default` from `null`](#default-from-null) +9. [De/Serialize into `Vec`, ignoring errors](#deserialize-into-vec-ignoring-errors) +10. [De/Serialize with `FromStr` and `Display`](#deserialize-with-fromstr-and-display) +11. [`Duration` as seconds](#duration-as-seconds) +12. [Hex encode bytes](#hex-encode-bytes) +13. [Ignore deserialization errors](#ignore-deserialization-errors) +14. [`Maps` to `Vec` of enums](#maps-to-vec-of-enums) +15. [`Maps` to `Vec` of tuples](#maps-to-vec-of-tuples) +16. [`NaiveDateTime` like UTC timestamp](#naivedatetime-like-utc-timestamp) +17. [`None` as empty `String`](#none-as-empty-string) +18. [One or many elements into `Vec`](#one-or-many-elements-into-vec) +19. [Pick first successful deserialization](#pick-first-successful-deserialization) +20. [Timestamps as seconds since UNIX epoch](#timestamps-as-seconds-since-unix-epoch) +21. [Value into JSON String](#value-into-json-string) +22. [`Vec` of tuples to `Maps`](#vec-of-tuples-to-maps) +23. [Well-known time formats for `OffsetDateTime`](#well-known-time-formats-for-offsetdatetime) ## Base64 encode bytes @@ -57,6 +58,22 @@ value: [[u8; 64]; 33], "value": [[0,0,0,0,0,...], [0,0,0,...], ...], ``` +## `bool` from integer + +Deserialize an integer and convert it into a `bool`. +[`BoolFromInt`] (default) deserializes 0 to `false` and `1` to `true`, other numbers are errors. +[`BoolFromInt`] deserializes any non-zero as `true`. +Serialization only emits 0/1. + +```rust +// Rust +#[serde_as(as = "BoolFromInt")] // BoolFromInt +b: bool, + +// JSON +"b": 1, +``` + ## Borrow from the input for `Cow` type The types `Cow<'_, str>`, `Cow<'_, [u8]>`, or `Cow<'_, [u8; N]>` can borrow from the input, avoiding extra copies. @@ -471,6 +488,8 @@ rfc_3339: OffsetDateTime, These conversions are availble with the `time_0_3` feature flag. [`Base64`]: crate::base64::Base64 +[`BoolFromInt`]: crate::BoolFromInt +[`BoolFromInt`]: crate::BoolFromInt [`Bytes`]: crate::Bytes [`chrono::DateTime`]: chrono_crate::DateTime [`chrono::DateTime`]: chrono_crate::DateTime