// This registers objects that are going to be sent over the network. public static void register(EndPoint endPoint) { Kryo kryo = endPoint.getKryo(); kryo.register(RegisterName.class); kryo.register(String[].class); kryo.register(UpdateNames.class); kryo.register(ChatMessage.class); FieldSerializer<Data> dataClassSerializer = new FieldSerializer<Data>(kryo, Data.class); dataClassSerializer.getField("bytes").setClass(byte[].class, new ByteArraySerializer()); dataClassSerializer.getField("string").setClass(String.class, new StringSerializer()); dataClassSerializer.getField("string").setCanBeNull(true); kryo.register(Data.class, dataClassSerializer); }
/** * Registers the classes needed to use ObjectSpaces. This should be called before any connections * are opened. * * @see Kryo#register(Class, Serializer) */ public static void registerClasses(final Kryo kryo) { kryo.register(Object[].class); kryo.register(InvokeMethod.class); FieldSerializer serializer = (FieldSerializer) kryo.register(InvokeMethodResult.class).getSerializer(); serializer .getField("objectID") .setClass( int.class, new Serializer<Integer>() { @Override public void write(Kryo kryo, Output output, Integer object) { output.writeInt(object, true); } @Override public Integer read(Kryo kryo, Input input, Class<Integer> type) { return input.readInt(true); } }); kryo.register( InvocationHandler.class, new Serializer() { @Override public void write(Kryo kryo, Output output, Object object) { RemoteInvocationHandler handler = (RemoteInvocationHandler) Proxy.getInvocationHandler(object); output.writeInt(handler.objectID, true); } @Override public Object read(Kryo kryo, Input input, Class type) { int objectID = input.readInt(true); Connection connection = (Connection) kryo.getContext().get("connection"); Object object = getRegisteredObject(connection, objectID); if (WARN && (object == null)) { warn("kryonet", "Unknown object ID " + objectID + " for connection: " + connection); } return object; } }); }
private static void removeField(Kryo kryo, Class type, String fieldName) { FieldSerializer fld = new FieldSerializer(kryo, type); fld.removeField(fieldName); kryo.register(type, fld); }