Module serialize

Module serialize 

Source
Expand description

Serde (de)serialization of worlds.

As component types are not known at compile time, the world must be provided with the means to serialize each component. This is provided by the WorldSerializer implementation. This implementation also describes how ComponentTypeIds (which are not stable between compiles) are mapped to stable type identifiers. Components that are not known to the serializer will be omitted from the serialized output.

The Registry provides a WorldSerializer implementation suitable for most situations.

Serializing all entities with a Position component to JSON.

// create a registry which uses strings as the external type ID
let mut registry = Registry::<String>::default();
registry.register::<Position>("position".to_string());
registry.register::<f32>("f32".to_string());
registry.register::<bool>("bool".to_string());

// serialize entities with the `Position` component
let entity_serializer = Canon::default();
let json = serde_json::to_value(&world.as_serializable(
    component::<Position>(),
    &registry,
    &entity_serializer,
))
.unwrap();
println!("{:#}", json);

// registries are also serde deserializers
use serde::de::DeserializeSeed;
let world: World = registry
    .as_deserialize(&entity_serializer)
    .deserialize(json)
    .unwrap();

Structs§

Canon
Contains the canon names of entities.
DeserializeIntoWorld
Wraps a WorldDeserializer and a world and implements serde::DeserializeSeed for deserializing into the world.
DeserializeNewWorld
Wraps a WorldDeserializer and a world and implements serde::DeserializeSeed for deserializing into a new world.
Registry
A world (de)serializer which describes how to (de)serialize the component types in a world.
SerializableWorld
A serializable representation of a world.

Enums§

UnknownType
An error type describing what to do when a component type is unrecognized.

Traits§

AutoTypeKey
A TypeKey which can construct itself for a given type T.
CustomEntitySerializer
Describes a mapping between a runtime Entity ID and a serialized equivalent.
EntitySerializer
Describes how to serialize and deserialize a runtime Entity ID.
TypeKey
A (de)serializable type which can represent a component type in a serialized world.
WorldDeserializer
Describes a type which knows how to deserialize the components in a world.
WorldSerializer
Describes a type which knows how to deserialize the components in a world.

Functions§

set_entity_serializer
Sets the EntitySerializer currently being used to serialize or deserialize Entity IDs.

Type Aliases§

EntityName
A 16 byte UUID which uniquely identifies an entity.