#serialization #bevy #serde #save #entity-component #serializer-deserializer


Stateful, structural and human-readable serialization crate for the bevy engine

17 releases

0.3.2 Apr 21, 2024
0.3.1 Apr 18, 2024
0.2.3 Apr 15, 2024
0.1.11 Apr 2, 2024
0.1.6 Mar 18, 2024

#288 in Encoding

Download history 409/week @ 2024-02-27 85/week @ 2024-03-05 229/week @ 2024-03-12 153/week @ 2024-03-19 242/week @ 2024-03-26 483/week @ 2024-04-02 375/week @ 2024-04-09 487/week @ 2024-04-16 26/week @ 2024-04-23 7/week @ 2024-04-30

681 downloads per month
Used in bevy_stat_query




Crates.io Docs Bevy tracking

Stateful, structural and human-readable serialization crate for the bevy engine.


  • Stateful serialization and deserialization with world access.
  • Treat an Entity, its Components and children as a single serde object.
  • Serialize Handles and provide a generalized data interning interface.
  • Serialize stored Entitys like smart pointers.
  • Deserialize trait objects like Box<dyn T>, as an alternative to typetag.
  • Extremely lightweight and modular. No systems, no plugins.
  • Supports almost every serde format*

* Ergonomics may vary depend on Serializer, Deserializer and DeserializeSeed trait support

Getting Started

Serialize an Entity Character with some components and children, assuming all components are Serialize and DeserializeOwned:

bind_object!(Character as "Character" {
    character: Character,
    position: Position,
    #[serde(default, skip_serializing_if="Option::is_none")]
    weapon: Maybe<Weapon>,
    shield: Maybe<Shield>,
    #[serde(default, skip_serializing_if="Vec::is_empty")]
    potions: ChildVec<Potion>,

Then call save on World, where serializer is something like serde_json::Serializer.

// Save
// Load
// Delete

This saves a list of Characters as an array:

    { .. },
    { .. },

To save multiple types of objects in a batch, create a batch serialization type with the batch! macro.

type SaveFile = batch!(
    Character, Monster, Terrain,
    // Use `BindResource` to serialize a resource.

This saves each type in a map entry:

    "Character": [ 
        { .. },
        { .. },
    "Monster": [ .. ],
    "Terrain": [ .. ],
    "MyResource": ..


  • What if my types aren't Serialize and DeserializeOwned?

We can derive or implement SerdeProject to convert them into serde types.

  • I don't own the type

Use Convert and the SerdeProject macro to cast the type to an owned newtype.

  • I have an ID and I want to serialize its content

SerdeProject allows you to fetch a resource from the world during serialization.

  • I have a Box<dyn T>

If you are on a non-wasm platform you can try the typetag crate. If not, or if you want more control, checkout the typetagged module in this crate.

The traits and what they do

  • Serialize and DeserializeOwned

Any Serialize and DeserializeOwned type is automatically SerdeProject and any such Component is automatically a BevyObject.

This comes with the downside that we cannot implement SerdeProject on any foreign type due to the orphan rule. This is where Convert and the SerdeProject macro comes in handy.

  • SerdeProject

SerdeProject projects non-serde types into serde types with world access.

The SerdeProject macro implements SerdeProject on type where all fields either implements SerdeProject or converts to a SerdeProject newtype via the Convert trait.


Serialize a Handle as its path, stored in AssetServer.

struct MySprite {
    // implements serde, therefore is `SerdeProject`.
    pub name: String,
    // Calls `Convert` and `PathHandle<Image>` is `SerdeProject`.
    pub handle: Handle<Image>
  • Convert

Convert allows you to RefCast a non-serializable type to a newtype that implements SerdeProject.

For example PathHandle<Handle<T>> serializes Handle as a String, while UniqueHandle<Handle<T>> serializes Handle as a T. This zero-cost conversion can be done via the ref_cast crate.

  • BevyObject

A BevyObject allows an Entity to be serialized. This can either be just a component, or a combination of components, children, components on children, etc.

All SerdeProject components are BevyObjects.

  • BindBevyObject

BindBevyObject is a QueryFilter, usually a key component, that determines the entry point for serialization and deserialization.

Any entity that has the QueryFilter but does not satisfy the layout of the bound BevyObject will result in an error.

use the bind_object! macro to create a serialization entry.

  • Named

Provides a serialization name for resources.

  • SaveLoad

Represents a batch serialization type, or contents of a single save file.


The typetag crate allows you to serialize trait objects like Box<dyn T>, but using typetag will always pull in all implementations linked to your build and does not work on WASM. To address these limitations this crate allows you to register deserializers manually in the bevy World and use the TypeTagged newtype for serialization.

world.register_typetag::<Box<dyn Animal>, Cat>()


struct MyComponent {
    #[serde_project("TypeTagged<Box<dyn Weapon>>")]
    weapon: Box<dyn Weapon>

To have user friendly configuration files, you can use register_deserialize_any and AnyTagged to allow deserialize_any, i.e. deserialize 42 instead of {"int": 42} in self-describing formats. Keep in mind using AnyTagged in a non-self-describing format like postcard will always return an error as this is a limitation of the serde specification.

world.register_deserialize_any(|s: &str| 
        .map_err(|e| e.to_string())?
    ) as Box<dyn Animal>)


bevy bevy-serde-project
0.13 latest


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~882K SLoC