Java has built in methods to convert a class into data for storage, it
works similar to the JSON that the robots receive.  However, the java
serialization classes I know of deal with binary data.  You can
probably use the json classes to serialize as a string (as they are
already in your project for the robot to use), but the binary data
will take up less space than textual, so it's better for the appengine
data store.

Here is a helper class I created to handle serialize/unserialize
operations for storing in the app engine db.


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public final class SerializeHelper {
        public static byte[] Serialize(Serializable obj) {
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                ObjectOutputStream objOut;
                try {
                         objOut = new ObjectOutputStream(output);
                         objOut.writeObject(obj);
                } catch (IOException e) {
                        return null;
                }
                return output.toByteArray();
        }

        public static Object Unserialize(byte[] data) {
                ByteArrayInputStream input = new ByteArrayInputStream(data);
                try {
                        ObjectInputStream inpObj = new ObjectInputStream(input);
                        try {
                                return inpObj.readObject();
                        } catch (ClassNotFoundException e) {
                                return null;
                        }
                } catch (IOException e) {
                        return null;
                }
        }
}


Any class you want to serialize must implement Serializable.  Member
variables and arrays will also be serialized as long as their types
implement Serializable (I'm not sure what it does if they don't.  I
think it throws a run time error).

For saving serialized classes to the appengine data store, use the
Blob data type.

Remember to be careful about changing the classes you want to
serialize.  The serializable interface does request you have public
static final long serialVersionUID, but I have not looked into
"updating" serialized classes if they change, nor have I had the case
come up.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Wave API" group.
To post to this group, send email to google-wave-api@googlegroups.com
To unsubscribe from this group, send email to 
google-wave-api+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-wave-api?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to