-
Notifications
You must be signed in to change notification settings - Fork 44
[ECO-5386] Liveobjects serialization without jackson deps #1106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
d388c15
7a4873e
b846865
7e4f5d6
ef65124
8d23a1f
36f9844
6fa439d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package io.ably.lib.objects; | ||
|
|
||
| import com.google.gson.JsonArray; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.msgpack.core.MessagePacker; | ||
| import org.msgpack.core.MessageUnpacker; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Serializer interface for converting between LiveObject arrays and their | ||
| * MessagePack or JSON representations. | ||
| */ | ||
| public interface LiveObjectSerializer { | ||
| /** | ||
| * Reads a MessagePack array from the given unpacker and deserializes it into an Object array. | ||
| * | ||
| * @param unpacker the MessageUnpacker to read from | ||
| * @return the deserialized Object array | ||
| * @throws IOException if an I/O error occurs during unpacking | ||
| */ | ||
| @NotNull | ||
| Object[] readMsgpackArray(@NotNull MessageUnpacker unpacker) throws IOException; | ||
|
|
||
| /** | ||
| * Serializes the given Object array as a MessagePack array using the provided packer. | ||
| * | ||
| * @param objects the Object array to serialize | ||
| * @param packer the MessagePacker to write to | ||
| * @throws IOException if an I/O error occurs during packing | ||
| */ | ||
| void writeMsgpackArray(@NotNull Object[] objects, @NotNull MessagePacker packer) throws IOException; | ||
|
|
||
| /** | ||
| * Reads a JSON array from the given {@link JsonArray} and deserializes it into an Object array. | ||
| * | ||
| * @param json the {@link JsonArray} representing the array to deserialize | ||
| * @return the deserialized Object array | ||
| */ | ||
| @NotNull | ||
| Object[] readFromJsonArray(@NotNull JsonArray json); | ||
|
|
||
| /** | ||
| * Serializes the given Object array as a JSON array. | ||
| * | ||
| * @param objects the Object array to serialize | ||
| * @return the resulting JsonArray | ||
| */ | ||
| @NotNull | ||
| JsonArray asJsonArray(@NotNull Object[] objects); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package io.ably.lib.objects; | ||
|
|
||
| import io.ably.lib.realtime.AblyRealtime; | ||
| import io.ably.lib.util.Log; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
|
|
||
| public class LiveObjectsHelper { | ||
|
|
||
| private static final String TAG = LiveObjectsHelper.class.getName(); | ||
| private static LiveObjectSerializer liveObjectSerializer; | ||
|
|
||
| public static LiveObjectsPlugin tryInitializeLiveObjectsPlugin(AblyRealtime ablyRealtime) { | ||
| try { | ||
| Class<?> liveObjectsImplementation = Class.forName("io.ably.lib.objects.DefaultLiveObjectsPlugin"); | ||
| LiveObjectsAdapter adapter = new Adapter(ablyRealtime); | ||
| return (LiveObjectsPlugin) liveObjectsImplementation | ||
| .getDeclaredConstructor(LiveObjectsAdapter.class) | ||
| .newInstance(adapter); | ||
| } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | | ||
| InvocationTargetException e) { | ||
| Log.i(TAG, "LiveObjects plugin not found in classpath. LiveObjects functionality will not be available.", e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static LiveObjectSerializer getLiveObjectSerializer() { | ||
| if (liveObjectSerializer == null) { | ||
| try { | ||
| Class<?> serializerClass = Class.forName("io.ably.lib.objects.serialization.DefaultLiveObjectSerializer"); | ||
| liveObjectSerializer = (LiveObjectSerializer) serializerClass.getDeclaredConstructor().newInstance(); | ||
| } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | | ||
| InvocationTargetException e) { | ||
| Log.e(TAG, "Failed to init LiveObjectSerializer, LiveObjects plugin not included in the classpath", e); | ||
| return null; | ||
| } | ||
| } | ||
| return liveObjectSerializer; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.ably.lib.objects; | ||
|
|
||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonNull; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonSerializationContext; | ||
| import com.google.gson.JsonSerializer; | ||
| import io.ably.lib.util.Log; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| public class LiveObjectsJsonSerializer implements JsonSerializer<Object[]>, JsonDeserializer<Object[]> { | ||
| private static final String TAG = LiveObjectsJsonSerializer.class.getName(); | ||
| private final LiveObjectSerializer serializer = LiveObjectsHelper.getLiveObjectSerializer(); | ||
|
|
||
| @Override | ||
| public Object[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
| if (serializer == null) { | ||
| Log.w(TAG, "Skipping 'state' field json deserialization because LiveObjectsSerializer not found."); | ||
| return null; | ||
| } | ||
| if (!json.isJsonArray()) { | ||
| throw new JsonParseException("Expected a JSON array for 'state' field, but got: " + json); | ||
| } | ||
| return serializer.readFromJsonArray(json.getAsJsonArray()); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement serialize(Object[] src, Type typeOfSrc, JsonSerializationContext context) { | ||
| if (serializer == null) { | ||
| Log.w(TAG, "Skipping 'state' field json serialization because LiveObjectsSerializer not found."); | ||
| return JsonNull.INSTANCE; | ||
| } | ||
| return serializer.asJsonArray(src); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,7 @@ public class ProtocolSerializer { | |||||||||||||||||
| /**************************************** | ||||||||||||||||||
| * Msgpack decode | ||||||||||||||||||
| ****************************************/ | ||||||||||||||||||
|
|
||||||||||||||||||
| public static ProtocolMessage readMsgpack(byte[] packed) throws AblyException { | ||||||||||||||||||
| try { | ||||||||||||||||||
| MessageUnpacker unpacker = Serialisation.msgpackUnpackerConfig.newUnpacker(packed); | ||||||||||||||||||
|
|
@@ -27,30 +27,31 @@ public static ProtocolMessage readMsgpack(byte[] packed) throws AblyException { | |||||||||||||||||
| /**************************************** | ||||||||||||||||||
| * Msgpack encode | ||||||||||||||||||
| ****************************************/ | ||||||||||||||||||
| public static byte[] writeMsgpack(ProtocolMessage message) { | ||||||||||||||||||
|
|
||||||||||||||||||
| public static byte[] writeMsgpack(ProtocolMessage message) throws AblyException { | ||||||||||||||||||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||||||||||||||||||
| MessagePacker packer = Serialisation.msgpackPackerConfig.newPacker(out); | ||||||||||||||||||
| try { | ||||||||||||||||||
| message.writeMsgpack(packer); | ||||||||||||||||||
|
|
||||||||||||||||||
| packer.flush(); | ||||||||||||||||||
| return out.toByteArray(); | ||||||||||||||||||
| } catch(IOException e) { return null; } | ||||||||||||||||||
| } catch (IOException ioe) { | ||||||||||||||||||
| throw AblyException.fromThrowable(ioe); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you double-check that we won't introduce regression here
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, there's no point in returning
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, we already throw error for similar readMsgpack method => ably-java/lib/src/main/java/io/ably/lib/types/ProtocolSerializer.java Lines 18 to 25 in 80a655e
Seems, the relevant code for the writeMsgpack method wasn't updated.
|
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /**************************************** | ||||||||||||||||||
| * JSON decode | ||||||||||||||||||
| ****************************************/ | ||||||||||||||||||
|
|
||||||||||||||||||
| public static ProtocolMessage fromJSON(String packed) throws AblyException { | ||||||||||||||||||
| return Serialisation.gson.fromJson(packed, ProtocolMessage.class); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /**************************************** | ||||||||||||||||||
| * JSON encode | ||||||||||||||||||
| ****************************************/ | ||||||||||||||||||
|
|
||||||||||||||||||
| public static byte[] writeJSON(ProtocolMessage message) throws AblyException { | ||||||||||||||||||
| return Serialisation.gson.toJson(message).getBytes(Charset.forName("UTF-8")); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.