Skip to content
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

ref: refactored events data parsing implementation #231

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

mohitpubnub
Copy link
Contributor

@mohitpubnub mohitpubnub commented Jan 13, 2025

ref(subscribe): events data parsing/deserialisation implementation enhancement

Scope:

Refactor Events data parsing implementation for subscribe responses to avoid major changes in future while updating any event data fields. [e.g Adding custom messageType in message event]
Serialisation library apis changes are out of scope for this change.

@mohitpubnub mohitpubnub self-assigned this Jan 13, 2025
@mohitpubnub mohitpubnub added the priority: medium This PR should be reviewed after all high priority PRs. label Jan 13, 2025
Copy link
Contributor

@jakub-grzesiowski jakub-grzesiowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some serialization concerns, mostly due to Unity ion the case of NewtonsoftJsonDotNet

@@ -7,12 +7,16 @@ internal class ResponseBuilder
private readonly PNConfiguration config;
private readonly IJsonPluggableLibrary jsonLib;
private readonly IPubnubLog pubnubLog;
private readonly NewtonsoftJsonDotNet newtonsoftJsonDotNet;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a IJsonPluggable reference here, we shouldn't use the concrete implementation because NewtonsoftJsonDotNet breaks in certain Unity build targets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe with that aspect, Since this code is not changing configuration related to serialisation/deserialisation.

NewtonsoftJsonDotNet is existing custom serialiser library Which is already being used in other parts of the sdk (e.g. PubnubCore class) and got tested already with unity since old versions.

The reason for not using pluggable one, There are some default settings in serialiser(with netwonsoft) and type declarations to make sdk's NewtonsoftJsonDotNet class compatible with unity and other supported targets.

Using pluggable one might leave a chance to break existing behaviour while deserialising the custom user messages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have NewtonsoftJsonUnity in Unity SDK, which is very similar but uses the Unity version of Newtonsoft library to avoid crashing in IL2CPP build mode (it was a client reported issue a while back), and from what I see we use the DotNet version directly as the default value if no other pluggabel was provided - we definitely can't use it instead of pluggable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the implementation, not EventSerializer is using only json pluggable.

for unity compatibility, I saw updates at v6.20.1

also this will make this implementation a bit more unified.

good catch!

public class EventDeserializer
{
private readonly IJsonPluggableLibrary jsonLibrary;
private readonly NewtonsoftJsonDotNet newtonSoftJsonLibrary; // the default serializer of sdk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


private PNPresenceEventResult DeserializePresenceEvent(IDictionary<string, object> jsonFields)
{
Dictionary<string, object> presenceDicObj = jsonLibrary.ConvertToDictionaryObject(jsonFields["payload"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance the "payload" key won't be there? That goes for the other keys too, do we need TryGetValue-s here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. I think it's just coding style I followed.
But true, We can avoid those checks.

Updating....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated for mandatory fields

@@ -23,7 +20,7 @@ public T Deserialize<T>(IDictionary<string, object> json)
var typeInfo = typeof(T);
if (typeInfo.GetTypeInfo().IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(PNMessageResult<>))
{
response = newtonSoftJsonLibrary.DeserializeMessageResultEvent<T>(json);
response = jsonLibrary.DeserializeToObject<T>(json);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakub-grzesiowski updated to json pluggable library

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead jsonDotNet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: medium This PR should be reviewed after all high priority PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants