J3O format changes

Recently I made some changes to the J3O format used in jME3.

There’s now a signature “JME3” written into the header of the file, in addition to a 32-bit version number.

Also there’s now a version number per class, e.g:



[java]class A implements Savable {

public static final int SAVABLE_VERSION = 1;



public void read(…){

InputCapsule ic = …

System.out.println( ic.getSavableVersion(A.class) );

System.out.println( ic.getSavableVersion(B.class) );

}

}



class B extends A {

public static final int SAVABLE_VERSION = 2;

}[/java]

The above code will print 1, 2 when you save and load a B object. If you save an A object, it will crash because there’s no version information for a B class since A is not B. If you saved a class before the new J3O format was used or if you didn’t specify SAVABLE_VERSION, then 0 will be returned.



To get the “global” format version you can use BinaryImporter.getFormatVersion() inside the Savable.read() method, this will return 1 for the new J3O format and return 0 for the old J3O format. Its already used extensively in jME3 for certain backward compatibility hacks. The format version will change very rarely, maybe between major jME revisions like jME3->jME4.



The main advantages of taking this approach is that you can now determine if a file is a J3O file without checking the extension, by looking at the signature. Also now you can apply certain compatibility hacks only if you detect an old version, thus the latest J3O files will not use the compatibility hacks and load faster.

1 Like

ah, very nice, thx for the contribution and info