-
Notifications
You must be signed in to change notification settings - Fork 41
[ECO-5386] Liveobjects serialization using msgpack-jackson #1101
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d388c15
[ECO-5386] Defined LiveObjectSerializer interface
sacOO7 7a4873e
[ECO-5386] Implement JSON and MessagePack serialization for ObjectMes…
sacOO7 b846865
[ECO-5386] Implemented custom serializers for ObjectData type
sacOO7 7e4f5d6
[ECO-5386] Implemented unit tests for ObjectMesssage json/msgpack ser…
sacOO7 792ffc9
[ECO-5386] Updated unit tests for ObjectMesssage serialization
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
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
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); | ||
| } |
40 changes: 40 additions & 0 deletions
40
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,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; | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix thread safety issue in singleton pattern.
The singleton implementation for
liveObjectSerializeris not thread-safe. Multiple threads could simultaneously check the null condition and create multiple instances, violating the singleton pattern.public static LiveObjectSerializer getLiveObjectSerializer() { - if (liveObjectSerializer == null) { + if (liveObjectSerializer == null) { + synchronized (LiveObjectsHelper.class) { + 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; }📝 Committable suggestion
🤖 Prompt for AI Agents