#[derive(JsonxConstructor)]
{
// Attributes available to this derive:
#[jsonx]
}
Expand description
Derives JsonxConstructor plus the serde impls that wire it in, for a type
that implements Display and FromStr.
The constructor name defaults to the type name lowercased; override it with
#[jsonx(name = "...")]. See the constructor module for details.
use std::fmt;
use std::str::FromStr;
#[derive(jsonx::JsonxConstructor, Debug, PartialEq)]
#[jsonx(name = "semver")]
struct Version(u16, u16);
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.0, self.1)
}
}
impl FromStr for Version {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
let (a, b) = s.split_once('.').ok_or("expected MAJOR.MINOR")?;
Ok(Version(a.parse().map_err(|_| "bad major")?, b.parse().map_err(|_| "bad minor")?))
}
}
assert_eq!(jsonx::to_string(&Version(1, 4)).unwrap(), r#"semver("1.4")"#);
assert_eq!(jsonx::from_str::<Version>(r#"semver("1.4")"#).unwrap(), Version(1, 4));Derives JsonxConstructor — and the serde
Serialize/Deserialize impls that wire it in — for a type that implements
Display and FromStr.
The constructor name defaults to the type name, lowercased. Override it with
#[jsonx(name = "...")]:
ⓘ
#[derive(jsonx::JsonxConstructor)]
#[jsonx(name = "semver")]
struct Version { major: u16, minor: u16 }
// (Version must also implement Display + FromStr)