VariantCodec
Generic Data Structure Serialization Library for Java
About
The VariantCodec library provides a generic data structure; the variant, which can be a map, collection, string or primitive (i.e. integer, double), and provides several options for serialization; binary, a C-style syntax and XML.



Why is this useful?
  • It separates the data structure from the representation so you are not locked into one representation (e.g. XML).
  • It is an extremely easy way to serialize data structures, from a simple property map to a complex object tree.
  • It provides different serializations for different uses:
    • Binary for efficient storage,
    • Java/C-style syntax for a compact representation that is human readable,
    • XML for compatibility.
  • It provides a validation/schema mechanism defined as code (ie. it separates the schema definition from the format).
  • It provides an object->variant->object mapper for simple serialization.
The following snippet shows how to use the library with the C/Java style serialization format:
package org.boris.variantcodec;

import org.boris.variant.VTCollection;
import org.boris.variant.VTMap;
import org.boris.variant.codec.SourceCodec;

public class VariantCodecExample
{
    public static void main(String[] args) throws Exception {
        VTMap m = new VTMap();
        m.add("var1", "Hello World!");
        m.add("num1", 12);
        m.add("float1", 23.5f);
        m.add("binary1", new byte[] { 3, 4, 4, 5, 5 });
        VTCollection c = new VTCollection();
        c.add(1);
        c.add(2);
        c.add(3);
        m.add("coll1", c);
        System.out.println(SourceCodec.encodeFormatted(m));
    }
}
This would output the following:
{
    var1="Hello World!";
    num1=12;
    float1=23.5f;
    binary1=<0304040505>;
    coll1=[1,2,3];
}
If the above output was stored in a String, you would decode it using the following code:
VTMap m = (VTMap) SourceCodec.decode(s);
System.out.println(m.getString("var1"));
This would output the following:
Hello World!
Download
The latest download is available from the Project Page.
Documentation
The javadoc for the library can be found here.
Object Serialization
The following shows the simple variantcodec object serializer:
import org.boris.variant.util.VariantObjectSerializer;

public class Test1
{
    public String a = "something";
    public double d = 1.8;
    public int[] c1 = { 3, 2, 1 };
    public boolean b2 = false;
    public short s1 = 234;
    public byte b3 = 12;
    public float f1 = 3.4f;

    public String toString() {
        return VariantObjectSerializer.encode(this).toString();
    }

    public static void main(String[] args) {
        System.out.println(new Test1());
    }
}
This would output the following:
{
    a="something";
    d=1.8;
    c1=[3,2,1];
    b2=false;
    s1=234s;
    b3=12b;
    f1=3.4f;
}
Change History
V0.0.3
  • Added charset support to binary codec
  • More consist use of reader/writer on source codec
  • Built as an OSGI bundle
  • Removed unused utilities
V0.0.2
  • Bug fixes with source codec.
  • Additional utilities.
V0.0.1
  • Initial version.