home

Serialization and Marshalling in UtilME

There are 5 ways to Serialize/Marshall data in UtilME;

Any of these can be used for sending data over a network using http/https or Socket connection such as SocketClient or for saving data into ByteArrayOutputStream for saving to the RMS.
Object that are supported out of the box: NOT supported:

All 5 methods encode straight into a OutputStream and decode straight from a InputStream, and at NO POINT create byte arrays or ByteArrayOutputStream. This means that memory usage and memory fragmentation is kept to a minimum.
There are also 4 'Code Generators' for generating code to support none standard java objects, They can be found in the ToolsME repository (https://swingme.svn.sourceforge.net/svnroot/swingme/ToolsME).

comparison
Small Fast Forward Compatible Backwards Compatible Human-readable
XML No No Yes Yes Yes
JSON No No Yes Yes Yes
Java Binary Yes Yes No No No
ProtoBuf Yes No Yes (only this implementation) Yes No

XML Util

XMLUtil.java
Example socket client using XMLUtil
01 public class MySocketClient extends SocketClient {
02     private XMLUtil access;
03     protected void connected(InputStream in, OutputStream out) {
04         access = new XMLUtil();
05     }
06     protected void disconnected() {
07         access = null;
08     }
09     protected void write(OutputStream out, Object objectthrows IOException {
10         access.save(out, object);
11     }
12     protected Object read(InputStream inthrows IOException {
13         return access.loadnew UTF8InputStreamReader(in) );
14     }
15     protected void handleObject(Object task) {
16         // TODO
17     }
18     protected void updateState(int c) {
19         // TODO
20     }
21 }
ANT Task for generating Marshalling code
1   <taskdef classname="net.yura.tools.mobilegen.MobileXmlGen" classpath="./dist/${ant.project.name}.jar" name="gen-xml"/>
2   <gen-xml
3     generatedFile="${basedir}/src/net/yura/mobile/gen/XMLAccess.java"
4     classNamesFile="${basedir}/generate.properties"/>

Bin Util

BinUtil.java

BinUtil uses DataOutputStream and DataInputStream to write data to the stream.

Example socket client using BinUtil
01 public class MySocketClient extends SocketClient {
02     private BinUtil access;
03     protected void connected(InputStream in, OutputStream out) {
04         access = new BinUtil();
05     }
06     protected void disconnected() {
07         access = null;
08     }
09     protected void write(OutputStream out, Object objectthrows IOException {
10         access.save(out, object);
11     }
12     protected Object read(InputStream inthrows IOException {
13         return access.load(in);
14     }
15     protected void handleObject(Object task) {
16         // TODO
17     }
18     protected void updateState(int c) {
19         // TODO
20     }
21 }
ANT Task for generating Serialization code
1   <taskdef classname="net.yura.tools.mobilegen.MobileBinGen" classpath="./dist/${ant.project.name}.jar" name="gen-bin"/>
2   <gen-bin
3     generatedFile="${basedir}/src/net/yura/mobile/gen/BinAccess.java"
4     classNamesFile="${basedir}/generate.properties"/>

JSON Util

JSONUtil.java
Example socket client using JSONUtil
01 public class MySocketClient extends SocketClient {
02     private JSONUtil access;
03     protected void connected(InputStream in, OutputStream out) {
04         access = new JSONUtil();
05     }
06     protected void disconnected() {
07         access = null;
08     }
09     protected void write(OutputStream out, Object objectthrows IOException {
10         access.save(out, object);
11     }
12     protected Object read(InputStream inthrows IOException {
13         return access.load(in);
14     }
15     protected void handleObject(Object task) {
16         // TODO
17     }
18     protected void updateState(int c) {
19         // TODO
20     }
21 }
ANT Task for generating Marshalling code
1   <taskdef classname="net.yura.tools.mobilegen.MobileJsonGen" classpath="./dist/${ant.project.name}.jar" name="gen-json"/>
2   <gen-json
3     generatedFile="${basedir}/src/net/yura/mobile/gen/JSONAccess.java"
4     classNamesFile="${basedir}/generate.properties"/>

Proto Util

ProtoUtil.java

This is a 'bastardized' version of Google Protocol Buffers that allows for Anonymous Objects. No .proto file is needed to read and write basic objects using ProtoUtil, but if you would like to allow other objects, and also wants to be able to read and write these using the other implementations you will need a proto file that describes the basic types as well as your own objects. The Code Generator will only generate ONE class with no inner classes, it will map the 'messages' to Java Objects only if it can find them on the class path during the code generation, and if they have get and set methods already that correspond to the members of that messeage. If the code generator can not find a java object that correspond to a particular message it will map it using a hashtable with keys being the strings that are the names of the members. Currently does not support packed arrays, but may do some day.

base .proto file
message Object {
    optional int32 int = 1;
    optional double double = 2;
    optional float float = 3;
    optional bool boolean = 4;
    optional string string = 5;
    optional int32 short = 6;
    optional int64 long = 7;
    optional int32 char = 8;
    optional int32 byte = 9;
    optional Vector vector = 10;
    optional Vector array = 11;
    optional Hashtable hashtable = 12;
    optional bytes bytes = 13;

    optional YourObject your_object = 21;
}
message Vector {
    repeated Object elements = 1;
}
message Hashtable {
    repeated KeyValue mappings = 1;
}
message KeyValue {
    required Object key = 1;
    required Object value = 2;
}

message YourObject {
    required string name = 1;
    required int32 age = 2;
}
...
Example socket client using ProtoUtil
01 public class MySocketClient extends SocketClient {
02     private ProtoUtil access;
03     protected void connected(InputStream in, OutputStream out) {
04         access = new ProtoUtil();
05     }
06     protected void disconnected() {
07         access = null;
08     }
09     protected void write(OutputStream out, Object objectthrows IOException {
10         DataOutputStream dout = new DataOutputStream(out);
11         int size = access.computeAnonymousObjectSize(object);
12         dout.writeInt(size);
13         dout.flush();
14         access.save(out, object);
15     }
16     protected Object read(InputStream inthrows IOException {
17         DataInputStream din = new DataInputStream(in);
18         int size = din.readInt();
19         return access.loadin, size );
20     }
21     protected void handleObject(Object task) {
22         // TODO
23     }
24     protected void updateState(int c) {
25         // TODO
26     }
27 }
ANT Task for generating Serialization code
1   <taskdef classname="net.yura.tools.mobilegen.MobileProtoGen" classpath="./dist/${ant.project.name}.jar" name="gen-proto"/>
2   <gen-proto
3     protoSource="${basedir}/test.proto"
4     objectPackage="net.yura.tools.mobilegen.model"
5     outputPackage="net.yura.mobile.gen"
6     outputClass="ProtoAccess"
7     sourceRoot="${basedir}/src"/>
8   </target>