imacake
imacake

Reputation: 1713

Java serialisation of complicated and HUGE data structures

I have a Model, which needs to be written to disk somehow and read afterwards (making a simple Java 2D game). Now, there's a reference to Player, which has an insaneload of References to Blocks and Entities. In my project there are a HUGE amount of Entities and Blocks, and I really dont want to write a toString()/encode() then fromString()/decode()... The project is overdue anyway and over here its 4:05 AM :p.

So, what does a human in that situation do? Crawl here and ask about what Serialization does, and if it will work right away. I'm in the drama that it must work on the first go, no much space to experimenter, sadly.

The situation below needs to be entirely written to disk. Doable with a single serialize() function :o ?

Now, imagine this fictive classes(in reality I have 5800 lines of java code and a ton of intermixed data, that hurts)(no functions)(off the top of my head):

class InventorySlot { Object[] list; Class type; int quantity; }
class Inventory { InventorySlot[] slots; }

class Player { int hp; Inventory inv,palette; double x,y; }

enum Block { air, stone, cobble, dirt, etc; }
class Entity { double x,y,dir,speed,h,w; /*AND CUSTOMS! (derivers)*/ }   
class EntitySpriteAnimationPuf { Particle[] parts; final double friction, dir, dirnoise; Color color; } // final vars!
class World
{
    List<Chunk> chunks; // have an infinite map. each chunk is 64x64 Blocks.
    Entity[65535] entities; // I have a lot of entites, both types and later their instances.
}
class Model // target class to be entirely written down
{
    Player p;
    WindowManager wm; // Yes, i have my own GUI. Yes, it looks wonderful. Yes its got a lot of data... Yes, ouch.
    World w; // got this manually written down.
    Vector<String> terminal; // Dont want this to be serialized...
    boolean guiactive, pause;
    // And a few more...
}
// AND A LOT MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE. MORE.

Upvotes: 0

Views: 1010

Answers (2)

Dapeng
Dapeng

Reputation: 1726

here is what u need to do with to load/save java object (exception handling is ignored)

make sure all the classes implement the Serializable interface

one thing to know is that, if you changed any of the serialized classes, then

most likely your "deserialization" will fail

public void load() {
    FileInputStream filein = new FileInputStream("data");
    ObjectInputStream in = new ObjectInputStream(filein);
    Model model = (Model) in.readObject();
    in.close();
}

public void save() {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data"));
    out.writeObject(model);
    out.close();
}

Upvotes: 2

Dmitri
Dmitri

Reputation: 9157

If you don't want to write your own serialization code, then kryo is a better alternative to Java Serializable. Especially if performance and serialized size are a consideration (sounds like they are for you).

And yes, all you have to do is register each of your model class with kryo, then just hand it an object graph to serialize.

You can also then add individual serializers for specific classes, if you need to be more efficient than the automatic serializer (which is still far more efficient that Java's).

It's also (somewhat) more resistant to class and JVM version changes; Serializable is quite brittle in that regard.

Upvotes: 0

Related Questions