[jdev] Serialization in JSO--Using JAVA
Matt Tucker
matt at jivesoftware.com
Tue Apr 5 10:15:28 CDT 2005
Merwin,
This is a built-in feature of Smack, but you could definitely do it with JSO too. What you'd do:
1) Serialize the object to a byte stream and then encode is as base 64.
2) Add it as a packet extension to a message or presence.
3) Look for the packet extension on the other side and de-serialize the object.
We call these packet properties in Smack, which can be various primitive values or Java objects. Here's the code we use to write out the extension XML, which you could probably adapt for JSO:
Regards,
Matt
-------------------
if (properties != null && !properties.isEmpty()) {
buf.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
// Loop through all properties and write them out.
for (Iterator i=getPropertyNames(); i.hasNext(); ) {
String name = (String)i.next();
Object value = getProperty(name);
buf.append("<property>");
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
buf.append("<value type=\"");
if (value instanceof Integer) {
buf.append("integer\">").append(value).append("</value>");
}
else if (value instanceof Long) {
buf.append("long\">").append(value).append("</value>");
}
else if (value instanceof Float) {
buf.append("float\">").append(value).append("</value>");
}
else if (value instanceof Double) {
buf.append("double\">").append(value).append("</value>");
}
else if (value instanceof Boolean) {
buf.append("boolean\">").append(value).append("</value>");
}
else if (value instanceof String) {
buf.append("string\">");
buf.append(StringUtils.escapeForXML((String)value));
buf.append("</value>");
}
// Otherwise, it's a generic Serializable object. Serialized objects are in
// a binary format, which won't work well inside of XML. Therefore, we base-64
// encode the binary data before adding it.
else {
ByteArrayOutputStream byteStream = null;
ObjectOutputStream out = null;
try {
byteStream = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
buf.append("java-object\">");
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append(encodedVal).append("</value>");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (out != null) {
try { out.close(); } catch (Exception e) { }
}
if (byteStream != null) {
try { byteStream.close(); } catch (Exception e) { }
}
}
}
buf.append("</property>");
}
buf.append("</properties>");
}
> -----Original Message-----
> From: jdev-bounces at jabber.org
> [mailto:jdev-bounces at jabber.org] On Behalf Of Merwin Pinto
> Sent: Tuesday, April 05, 2005 6:39 AM
> To: Jabber software development list
> Subject: [jdev] Serialization in JSO--Using JAVA
>
>
> Hi All,
> Is it possible to serialize objects and send them
> accross using Presence or Message packets?
> the methods like setShow(),setStatus().... etc take only
> Strings as Arguments .... Is there a method to send Objects....?
> Can i subclass the presence packet and write a method to
> accept a object ?..
> But the question is will it be sent accross thru the network
> to another client?
>
> PLEASE HELP .....
> Thanks a lot ,
> regards
> Merwin
> _______________________________________________
> jdev mailing list
> jdev at jabber.org
> http://mail.jabber.org/mailman/listinfo/jdev
>
More information about the JDev
mailing list