-
Notifications
You must be signed in to change notification settings - Fork 42
[ECO-5386] Liveobjects serialization #1109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c694c9
[ECO-5386] Added interfaces for liveobject serialization
sacOO7 53116e8
[ECO-5386] Added impl. for LiveObjectSerializer interface
sacOO7 1a3b35c
[ECO-5386] Added unit tests for Liveobject serialization
sacOO7 b5c1554
[ECO-5386] Fixed serialization and related helpers as per review comm…
sacOO7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
lib/src/main/java/io/ably/lib/objects/LiveObjectSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
45 changes: 45 additions & 0 deletions
45
lib/src/main/java/io/ably/lib/objects/LiveObjectsHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 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 volatile 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) { | ||
| synchronized (LiveObjectsHelper.class) { | ||
| if (liveObjectSerializer == null) { // Double-Checked Locking (DCL) | ||
| 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; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
lib/src/main/java/io/ably/lib/objects/LiveObjectsJsonSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.