With Open Source you get ability to to review and possibly copy some bits and pieces of code. In this case I suggest you look at well designed MessagePack and copy Templates.
Example of custom Template for Joda DateTime using MessagePack. Following Template serializes DateTime to Long (Millis from 1970) and deserializes it back in UTC (DateTimeZone.UTC). If you wish to maintain correct time zone it may be easily extended:
public class DateTimeSerializerTemplate extends AbstractTemplate<DateTime> {
private DateTimeSerializerTemplate() {
}
public void write(Packer pk, DateTime target, boolean required) throws IOException {
if (target == null) {
if (required) {
throw new MessageTypeException("Attempted to write null");
}
pk.writeNil();
return;
}
pk.write(target.getMillis());
}
public DateTime read(Unpacker u, DateTime to, boolean required) throws IOException {
if (!required && u.trySkipNil()) {
return null;
}
return new DateTime(u.readLong(), DateTimeZone.UTC);
}
static public DateTimeSerializerTemplate getInstance() {
return instance;
}
static final DateTimeSerializerTemplate instance = new DateTimeSerializerTemplate();
}
In your Class just register the template above:
msgpack = new MessagePack();
msgpack.register(DateTime.class, DateTimeSerializerTemplate.getInstance());