Skip to content
Fabio Ticconi edited this page Jul 30, 2016 · 16 revisions

When?

The module is the recommended serializer when you do not (want to) use LibGDX and you do not want to port your game to browsers. It has no extra benefit over the LibGDX serializer, while lacking some default serializers for libgdx classes.

How to

Configure project for (de)serialization

  1. Include artemis-odb-serializer-json module in your gradle/maven configuration.
  2. Add the required manager and configure a backend:
     final WorldSerializationManager manager = WorldSerializationManager();
     World world = new World(new WorldConfiguration().setSystem(manager));
     manager.setSerializer(new JsonArtemisSerializer(world))

Load from file

     final InputStream is = AnyClass.class.getResourceAsStream("level.json");
     manager.load(is, SaveFileFormat.class);

Save to string

     final StringWriter writer = new StringWriter();
     manager.save(writer, new SaveFileFormat(entities));
     String json = writer.toString();

entities is an IntBag with entity IDs defining the scope of the save operation.

Save to file

     final PrintWriter writer = new PrintWriter("level.json", "UTF-8");
     manager.save(writer, new SaveFileFormat(entities));
     writer.close();
Clone this wiki locally