pub struct IfIsHumanReadable<H, F = Same>(/* private fields */);
Expand description
Use the first format if De/Serializer::is_human_readable
, otherwise use the second
If the second format is not specified, the normal
Deserialize
/Serialize
traits are used.
§Examples
use std::time::Duration;
#[serde_as]
#[derive(Deserialize, Serialize)]
struct A {
#[serde_as(as = "IfIsHumanReadable<DisplayFromStr>")]
number: u32,
}
let x = A {
number: 777,
};
assert_eq!(json!({ "number": "777" }), serde_json::to_value(&x).unwrap());
assert_eq!(vec![145, 205, 3, 9], rmp_serde::to_vec(&x).unwrap());
#[serde_as]
#[derive(Deserialize, Serialize)]
struct B {
#[serde_as(as = "IfIsHumanReadable<DurationMilliSeconds, DurationSeconds>")]
duration: Duration,
}
let x = B {
duration: Duration::from_millis(1500),
};
assert_eq!(json!({ "duration": 1500 }), serde_json::to_value(&x).unwrap());
assert_eq!(vec![145, 2], rmp_serde::to_vec(&x).unwrap());
Trait Implementations§
source§impl<'de, T, H, F> DeserializeAs<'de, T> for IfIsHumanReadable<H, F>where
H: DeserializeAs<'de, T>,
F: DeserializeAs<'de, T>,
impl<'de, T, H, F> DeserializeAs<'de, T> for IfIsHumanReadable<H, F>where
H: DeserializeAs<'de, T>,
F: DeserializeAs<'de, T>,
source§fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
source§impl<T, TA, FA> JsonSchemaAs<T> for IfIsHumanReadable<TA, FA>where
TA: JsonSchemaAs<T>,
Available on crate feature schemars_0_8
only.
impl<T, TA, FA> JsonSchemaAs<T> for IfIsHumanReadable<TA, FA>where
TA: JsonSchemaAs<T>,
Available on crate feature
schemars_0_8
only.source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Returns a string that uniquely identifies the schema produced by this type. Read more
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moresource§impl<T, H, F> SerializeAs<T> for IfIsHumanReadable<H, F>
impl<T, H, F> SerializeAs<T> for IfIsHumanReadable<H, F>
source§fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
Auto Trait Implementations§
impl<H, F> Freeze for IfIsHumanReadable<H, F>
impl<H, F> RefUnwindSafe for IfIsHumanReadable<H, F>where
H: RefUnwindSafe,
F: RefUnwindSafe,
impl<H, F> Send for IfIsHumanReadable<H, F>
impl<H, F> Sync for IfIsHumanReadable<H, F>
impl<H, F> Unpin for IfIsHumanReadable<H, F>
impl<H, F> UnwindSafe for IfIsHumanReadable<H, F>where
H: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more