merde_json_types
Logo by MisiasArt
merde_json_types is a companion crate to merde_json,
providing wrapper types that solve two problems at once.
Problem 1: Most crates have types that do not implement the merde_json traits
I'm thinking about the time crate, the chrono crate, camino, etc.
If you have, say, a time::OffsetDateTime in one of your structs,
then merde_json's derive macro will not work. You are going to need
a wrapper of some sort, and that's the kind of type this crate provides.
If you enable the time-serialize, time-deserialize, and merde_json
features, you can do this:
use ;
use Rfc3339;
let dt = Rfc3339;
let serialized = dt.to_json_string;
let deserialized: =
from_str.unwrap.to_rust_value.unwrap;
assert_eq!;
Problem 2: Keeping merde_json optional
The [time::Rfc3339] type is exported by this crate as soon as the time-types
feature is enabled. But merde_json_types doesn't even depend on merde_json
(or provide serialization/deserialization implementations) unless you activate
its merde_json feature!
That means, you can have your crate unconditionally depend on merde_json_types,
and use Rfc3339 in your public structs:
use ;
use Rfc3339;
derive!
And still only depend on merde_json when your own feature gets activated:
[]
= "2"
= { = "2", = true }
[]
= ["dep:merde_json", "merde_json_types/merde_json"]
Of course, for that to work, we need to get rid of any unconditional mention of
merde_json in our code, which would become something like:
use PhantomData;
use Rfc3339;
derive!