Arch-ECS
💬 Join the discord!☕ Buy us a coffee!
  • 🌄Why Arch?
  • 📖Documentation
    • Concepts
    • World
    • Entity
    • Query
    • Archetypes & Chunks
    • Optimizations
      • Query-Techniques
      • Pass on data
      • Batch and Bulk
      • PURE_ECS
      • Multithreading
      • EntityData
    • Utilities
      • Component Registration
      • Non-generic API
      • CommandBuffer
      • Events
      • Dangerous Extensions
  • 🧩Extensions
    • Arch.Extended
      • Arch.System
      • Arch.System.SourceGenerator
      • Arch.EventBus
      • Arch.AOT.SourceGenerator
      • Arch.LowLevel
      • Arch.Persistence
      • Arch.Relationships
  • 💡Examples & Guidelines
    • Arch.Samples
    • Entities in Query
    • Structural changes
  • Unity
  • 🎮Projects using Arch
    • Skylandkingdoms
    • Cubetory
    • SS14
    • EquilibriumEngine-CSharp
    • Rougelite-Survivor
  • ✏️Misc
    • Roadmap
    • FAQ
Powered by GitBook
On this page
  • JSON
  • Binary

Was this helpful?

Edit on GitHub
  1. Extensions
  2. Arch.Extended

Arch.Persistence

Arch.Persistence, a tiny persistence framework that supports JSON & Binary.

PreviousArch.LowLevelNextArch.Relationships

Last updated 1 month ago

Was this helpful?

With this package you can easily convert your worlds and entities to JSON and binary and back. This is useful for save files, databases or networking, for example.

Both serializers support (de)serialization with bytes and streams. The binary one also supports the newly introduced IBufferWriter<T> for streaming purposes.

Limitations:

  • Single Entity serialization does not support cyclic references and requires custom handling.

  • World serialization can handle cyclic entity references.

  • Can not handle cross-world references since those are literally unresolvable.

  • The order in which components are must be the same for deserialization as for serialization.

JSON

JSON is the ultimate data exchange format. If you don't know it, you should really catch up. The usage is very very simple and flexible. Take a look at the or check out the example!

// Initialize the Serializer, and inject custom ones to handle classes or structs in a specific way.
var serializer = new ArchJsonSerializer(new SpriteSerializer{GraphicsDevice = GraphicsDevice}, ...);

var worldJson = serializer.ToJson(_world);   // Serialize your world to a json-string
_world = serializer.FromJson(worldJson);     // Deserialize it from a json-string.

var entityJson = serializer.ToJson(_world, someEntity);  // Serialize single Entity to json.
serializer.FromJson(_world, entityJson);         // Entity is being deserialized to the given world. 

// Example Custom Serializer for Sprites
public class SpriteSerializer : IJsonFormatter<Sprite>
{
    public void Serialize(ref JsonWriter writer, Sprite value, IJsonFormatterResolver formatterResolver)
    {
        // Your logic
    }

    public Sprite Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
    {
        // Your logic
    }
}

Binary

Binary is fast and efficient. However, not human readable. For certain purposes, it is nevertheless perfect.

// Initialize the Serializer, and inject custom ones to handle classes or structs in a specific way.
var serializer = new ArchBinarySerializer(new SpriteSerializer{GraphicsDevice = GraphicsDevice}, ...);

var worldBytes = serializer.Serialize(_world);   // Serialize your world to a byte[]
_world = serializer.Deserialize(worldBytes );    // Deserialize it from a byte[].

var entityBytes = serializer.Serialize(_world, someEntity);  // Serialize single Entity to byte[].
serializer.Deserialize(_world, entityBytes);                 // Entity is being deserialized to the given world. 

// Example Custom Serializer for Sprites
public class SpriteSerializer : IMessagePackFormatter<Sprite>
{
    public void Serialize(ref MessagePackWriter writer, Sprite value, MessagePackSerializerOptions options)
    {
        // Your logic
    }

    public Sprite Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
    {
        // Your logic
    }
}

Since the package uses Messagepack under the hood, you can easily extend it at any time. For an insight into how to create custom serializers, check out the !

🧩
registered
git-example project
project