Enumシリアライザ
jabsorbはデフォルトではEnumをシリアライズ/デシリアライズできないみたいなので、ドキュメントの「7.2. Implementing a Serializer」と添付のシリアライザクラスのソースを参考にカスタムシリアライザを作成。以下の実装でよいはず。
import org.jabsorb.serializer.AbstractSerializer; import org.jabsorb.serializer.MarshallException; import org.jabsorb.serializer.ObjectMatch; import org.jabsorb.serializer.SerializerState; import org.jabsorb.serializer.UnmarshallException; import org.json.JSONException; import org.json.JSONObject; /** * Enumのシリアライザ */ public class EnumSerializer extends AbstractSerializer { /**UID*/ private static final long serialVersionUID = -7925358912890518075L; @Override @SuppressWarnings("unchecked") public boolean canSerialize(Class clazz, Class jsonClazz) { return Enum.class.isAssignableFrom(clazz) && (jsonClazz == null || jsonClazz == JSONObject.class); } @Override @SuppressWarnings("unchecked") public Class[] getJSONClasses() { return new Class[] {}; } @Override @SuppressWarnings("unchecked") public Class[] getSerializableClasses() { return new Class[] {}; } @Override public Object marshall(SerializerState state, Object p, Object o) throws MarshallException { JSONObject obj = new JSONObject(); if (o instanceof Enum) { String name = ((Enum<?>) o).name(); try { obj.put("javaClass", o.getClass().getName()); obj.put("name", name); } catch (JSONException e) { throw new MarshallException(e.getMessage(), e); } } else { throw new MarshallException("cannot marshall enum using class " + o.getClass()); } return obj; } @Override @SuppressWarnings("unchecked") public ObjectMatch tryUnmarshall( SerializerState state, Class clazz, Object o) throws UnmarshallException { JSONObject jso = (JSONObject) o; try { Class<?> cl = Class.forName(jso.getString("javaClass")); if ( !Enum.class.isAssignableFrom(cl) ) { throw new UnmarshallException("not enum"); } state.setSerialized(o, ObjectMatch.OKAY); return ObjectMatch.OKAY; } catch (Exception e) { throw new UnmarshallException("not enum", e); } } @Override @SuppressWarnings("unchecked") public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException { JSONObject jso = (JSONObject) o; try { Class<?> cl = Class.forName(jso.getString("javaClass")); if ( !Enum.class.isAssignableFrom(cl) ) { throw new UnmarshallException("not enum"); } String name = jso.getString("name"); Object returnValue = Enum.valueOf( (Class) cl, name);; state.setSerialized(o, returnValue); return returnValue; } catch (Exception e) { throw new UnmarshallException("not enum", e); } } }
有効化するには、JSONRPCBridgeにインスタンスを設定してやればOKです。
JSONRPCBridge bridge = JSONRPCBridge.getGlobalBridge();
bridge.registerSerializer( new EnumSerializer() );