Skip to content

Commit

Permalink
refactor key checks while accessing fields in presence event data par…
Browse files Browse the repository at this point in the history
…sing implementation
  • Loading branch information
Mohit Tejani authored and Mohit Tejani committed Jan 13, 2025
1 parent d34bce0 commit 81becc2
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/Api/PubnubApi/JsonDataParse/EventDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,29 @@ public T Deserialize<T>(IDictionary<string, object> json)

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

PNPresenceEventResult presenceEvent = null;

if (presenceDicObj != null)
if (presenceDataFields != null)
{
presenceEvent = new PNPresenceEventResult();
presenceEvent.Event = presenceDicObj["action"].ToString();
long presenceTimeStamp;
if (Int64.TryParse(presenceDicObj["timestamp"].ToString(), out presenceTimeStamp))
presenceEvent = new PNPresenceEventResult
{
Event = presenceDataFields["action"]?.ToString()
};
if (Int64.TryParse(presenceDataFields["timestamp"].ToString(), out var presenceTimeStamp))
{
presenceEvent.Timestamp = presenceTimeStamp;
}

if (presenceDicObj.TryGetValue("uuid", out var value))
{
presenceEvent.Uuid = value.ToString();
}
presenceEvent.Uuid = presenceDataFields["uuid"]?.ToString();

if (Int32.TryParse(presenceDicObj["occupancy"].ToString(), out var presenceOccupany))
if (Int32.TryParse(presenceDataFields["occupancy"]?.ToString(), out var presenceOccupany))
{
presenceEvent.Occupancy = presenceOccupany;
}

if (presenceDicObj.TryGetValue("data", out var presenceEventDataField))
if (presenceDataFields.TryGetValue("data", out var presenceEventDataField))
{
if (presenceEventDataField is Dictionary<string, object> presenceData)
{
Expand All @@ -86,8 +84,7 @@ private PNPresenceEventResult DeserializePresenceEvent(IDictionary<string, objec

presenceEvent.Channel = jsonFields["channel"]?.ToString();
presenceEvent.Channel = presenceEvent.Channel?.Replace("-pnpres", "");



presenceEvent.Subscription = jsonFields["channelGroup"]?.ToString();
presenceEvent.Subscription = presenceEvent.Subscription?.Replace("-pnpres", "");

Expand All @@ -99,7 +96,7 @@ private PNPresenceEventResult DeserializePresenceEvent(IDictionary<string, objec

if (presenceEvent.Event != null && presenceEvent.Event.ToLowerInvariant() == "interval")
{
if (presenceDicObj.TryGetValue("join", out var joinUserList))
if (presenceDataFields.TryGetValue("join", out var joinUserList))
{
List<object> joinDeltaList = joinUserList as List<object>;
if (joinDeltaList is { Count: > 0 })
Expand All @@ -108,27 +105,27 @@ private PNPresenceEventResult DeserializePresenceEvent(IDictionary<string, objec
}
}

if (presenceDicObj.ContainsKey("timeout"))
if (presenceDataFields.ContainsKey("timeout"))
{
List<object> timeoutDeltaList = presenceDicObj["timeout"] as List<object>;
List<object> timeoutDeltaList = presenceDataFields["timeout"] as List<object>;
if (timeoutDeltaList != null && timeoutDeltaList.Count > 0)
{
presenceEvent.Timeout = timeoutDeltaList.Select(x => x.ToString()).ToArray();
}
}

if (presenceDicObj.ContainsKey("leave"))
if (presenceDataFields.ContainsKey("leave"))
{
List<object> leaveDeltaList = presenceDicObj["leave"] as List<object>;
List<object> leaveDeltaList = presenceDataFields["leave"] as List<object>;
if (leaveDeltaList != null && leaveDeltaList.Count > 0)
{
presenceEvent.Leave = leaveDeltaList.Select(x => x.ToString()).ToArray();
}
}

if (presenceDicObj.ContainsKey("here_now_refresh"))
if (presenceDataFields.ContainsKey("here_now_refresh"))
{
string hereNowRefreshStr = presenceDicObj["here_now_refresh"].ToString();
string hereNowRefreshStr = presenceDataFields["here_now_refresh"].ToString();
if (!string.IsNullOrEmpty(hereNowRefreshStr))
{
bool boolHereNowRefresh = false;
Expand Down

0 comments on commit 81becc2

Please sign in to comment.