serde-split
serde-split is a helpful wrapper around serde's derive macros for
serialization/deserialization.
Using serde-split's versions of the Deserialize and Serialize derive macros will allow the
deriver to derive two separate implementations of the trait for use with (de)serializers
that properly support skipping fields (such as serde_json) and those that don't (such as bincode).
Examples
Let's say you are creating a game with bevy and have an animation format.
In development, you might want this animation format to be easily modifyable JSON data but for
official releases you want to package it as binary data.
For the JSON development asset, you'll load each of the keyframes from a path relative to the JSON file, and for the release asset you'll deserialize a spritesheet as PNG data from a binary file.
In this very real use case that inspired this crate, you could achieve it this way:
use ;
use RgbaImage;
bincode also doesn't properly support adjacent enum tagging, which would help simplify JSON data
for human maintenance but would break binary formats.
You could imagine, also for the same game, that you have some collision object that can have a static position or a path-based position:
use Vec2;
use ;
// This is the only way to declare this while allowing it to work with bincode
Unfortunately, the above would make our JSON data look like this:
Using serde-split's macros, you could instead declare your struct like this:
use Vec2;
use ;
And now your JSON representation gets simplified:
While the binary representation maintains well-defined for bincode's (de)serializer!
What does it expand to?
If we use the sprite animation example from above, the following struct:
will roughly expand to:
const _: = ;
With a similar looking expansion for Serialize.