Skip to content
Kyle Bedell edited this page Apr 11, 2016 · 42 revisions

artemis-odb-serializer-json-libgdx

Provides serialization for all platforms.

When?

The module is the recommended serializer when using LibGDX. It is the only serializer with browser (GWT/HTML) support.

Warning

The GWT target typically requires some effort to get working and can be hard to debug. Not recommended for beginners. Still, use of this module is recommended if you plan to target browsers in the future.

This module depends on BOTH gdx and odb gwt reflection caches! Make absolutely certain you register all your components and related types with both reflection caches, or you will spend hours debugging! See step 4 below.

Setup

Assumes Gradle LibGDX project with working GWT configuration.

  1. Add compile "net.onedaybeard.artemis:artemis-odb-serializer-json-libgdx:1.3.1" to your core module.
  2. Add compile "net.onedaybeard.artemis:artemis-odb-serializer-json-libgdx:1.3.1:sources" to your html module.
  3. Add the gwt json module to GdxDefinition.gwt.xml <inherits name='com.artemis-json-libgdx' />
  4. Register all classes (or parent packages) in GdxDefinition.gwt.xml, example:
<extend-configuration-property name="gdx.reflect.include" value="net.mypackage.components"/>
<extend-configuration-property name="artemis.reflect.include" value="net.mypackage.components"/>

Register the Serializer

WorldSerializationManager requires a concrete subclass of ArtemisSerializer to be set before saving and loading world state. If you don't want to roll your own, there is already one provided for you named JsonArtemisSerializer. In addition, you'll need to register the WorldSerializationManager with the world.

WorldSerializationManager worldSerializationManager = new WorldSerializationManager();

WorldConfiguration worldConfig = new WorldConfigurationBuilder()
    .with(
        worldSerializationManager
        // ...your other systems/managers
    ).build();

    world = new World(worldConfig);

    // Set serializer
    worldSerializationManager.setSerializer(new JsonArtemisSerializer(world));

To Json String

private String subscriptionToJson(EntitySubscription subscription) {
	final StringWriter writer = new StringWriter();
	final SaveFileFormat save = new SaveFileFormat(subscription.getEntities());
	world.getSystem(WorldSerializationManager.class).save(writer, save);
	return writer.toString();
}

To Local Storage

final EntitySubscription allEntities = world.getManager(AspectSubscriptionManager.class).get(Aspect.all());

try {
	final StringWriter writer = new StringWriter();
	final SaveFileFormat save = new SaveFileFormat(allEntities.getEntities());
	world.getSystem(WorldSerializationManager.class).save(writer, save);

	final Preferences preferences = Gdx.app.getPreferences("MyGame");
	preferences.putString("MyStateId", writer.toString());
	preferences.flush();
} catch (Exception e) {
	Gdx.app.error("MyGame", "Save Failed", e);
}  

Local storage has a quota in most browsers! Article about local storage quota

From Local Storage

try {
	final Preferences preferences = Gdx.app.getPreferences("MyGame")
	final String json = preferences.getString("MyStateId");
	final ByteArrayInputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
	world.getSystem(WorldSerializationManager.class).load(is,SaveFileFormat.class);
} catch (Exception e) {
	Gdx.app.error("MyGame", "Load Failed", e);
}
Clone this wiki locally