9 DBusSerializable

Some people may want to be able to pass their own objects over D-Bus. Obviously only raw D-Bus types can be sent on the message bus itself, but the Java implementation allows the creation of serializable objects which can be passed to D-Bus functions and will be converted to/from D-Bus types by the library.

To create such a class you must implement the DBusSerializable18 class and provide two methods and a zero-argument constructor. The first method has the signature public Object

serialize() throws DBusException and the second must be called deserialize, return null and take as it’s arguments exactly all the dbus types that are being serialized to in order and with parameterization. The serialize method should return the class properties you wish to serialize, correctly formatted for the wire (DBusConnection.convertParameters() can help with this), in order in an Object array.

An example of a serializable class can be seen in figure 16.


import java.lang.reflect.Type;  
 
import java.util.List;  
import java.util.Vector;  
 
import org.freedesktop.dbus.DBusConnection;  
import org.freedesktop.dbus.DBusSerializable;  
 
public class TestSerializable implements DBusSerializable  
{  
   private int a;  
   private String b;  
   private Vector<Integer> c;  
   public TestSerializable(int a, String b, Vector<Integer> c)  
   {  
      this.a = a;  
      this.b = b.toString();  
      this.c = c;  
   }  
   public TestSerializable() {}  
   public void deserialize(int a, String b, List<Integer> c)  
   {  
      this.a = a;  
      this.b = b;  
      this.c = new Vector<Integer>(c);  
   }  
   public Object[] serialize()  
   {  
      return new Object[] { a, b, c };  
   }  
   public int getInt() { return a; }  
   public String getString() { return b; }  
   public Vector<Integer> getVector() { return c; }  
   public String toString()  
   {  
      return "TestSerializable{"+a+","+b+","+c+"}";  
   }  
}


Figure 16: A serializable class