public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     return(EnrichableField.FromJSON(JToken.Load(reader)));
 }
Exemple #2
0
        internal static EnrichedActivity FromJson(JObject obj)
        {
            EnrichedActivity             activity             = new EnrichedActivity();
            EnrichedAggregatedActivity   aggregateActivity    = null;
            EnrichedNotificationActivity notificationActivity = null;

            if (obj.Properties().Any(p => p.Name == Field_Activities))
            {
                if ((obj.Properties().Any(p => p.Name == Field_IsRead)) ||
                    (obj.Properties().Any(p => p.Name == Field_IsSeen)))
                {
                    activity = aggregateActivity = notificationActivity = new EnrichedNotificationActivity();
                }
                else
                {
                    activity = aggregateActivity = new EnrichedAggregatedActivity();
                }
            }

            obj.Properties().ForEach((prop) =>
            {
                switch (prop.Name)
                {
                case Field_Id: activity.Id = prop.Value.Value <string>(); break;

                case Field_Actor: activity.Actor = EnrichableField.FromJSON(prop.Value); break;

                case Field_Verb: activity.Verb = EnrichableField.FromJSON(prop.Value); break;

                case Field_Object: activity.Object = EnrichableField.FromJSON(prop.Value); break;

                case Field_Target: activity.Target = EnrichableField.FromJSON(prop.Value); break;

                case Field_ForeignId: activity.ForeignId = EnrichableField.FromJSON(prop.Value); break;

                case Field_Time: activity.Time = prop.Value.Value <DateTime>(); break;

                case Field_To:
                    {
                        JArray array = prop.Value as JArray;
                        if ((array != null) && (array.SafeCount() > 0))
                        {
                            if (array.First.Type == JTokenType.Array)
                            {
                                // need to take the first from each array
                                List <string> tos = new List <string>();

                                foreach (var child in array)
                                {
                                    var str = child.ToObject <string[]>();
                                    tos.Add(str[0]);
                                }

                                activity.To = tos;
                            }
                            else
                            {
                                activity.To = prop.Value.ToObject <string[]>().ToList();
                            }
                        }
                        else
                        {
                            activity.To = new List <string>();
                        }
                        break;
                    }

                case Field_Activities:
                    {
                        var activities = new List <EnrichedActivity>();

                        JArray array = prop.Value as JArray;
                        if ((array != null) && (array.SafeCount() > 0))
                        {
                            foreach (var child in array)
                            {
                                var childJO = child as JObject;
                                if (childJO != null)
                                {
                                    activities.Add(FromJson(childJO));
                                }
                            }
                        }

                        if (aggregateActivity != null)
                        {
                            aggregateActivity.Activities = activities;
                        }
                        break;
                    }

                case Field_ActorCount:
                    {
                        if (aggregateActivity != null)
                        {
                            aggregateActivity.ActorCount = prop.Value.Value <int>();
                        }
                        break;
                    }

                case Field_IsRead:
                    {
                        if (notificationActivity != null)
                        {
                            notificationActivity.IsRead = prop.Value.Value <bool>();
                        }
                        break;
                    }

                case Field_IsSeen:
                    {
                        if (notificationActivity != null)
                        {
                            notificationActivity.IsSeen = prop.Value.Value <bool>();
                        }
                        break;
                    }

                case Field_CreatedAt:
                    {
                        if (aggregateActivity != null)
                        {
                            aggregateActivity.CreatedAt = prop.Value.Value <DateTime>();
                        }
                        break;
                    }

                case Field_UpdatedAt:
                    {
                        if (aggregateActivity != null)
                        {
                            aggregateActivity.UpdatedAt = prop.Value.Value <DateTime>();
                        }
                        break;
                    }

                case Field_Group:
                    {
                        if (aggregateActivity != null)
                        {
                            aggregateActivity.Group = prop.Value.Value <string>();
                        }
                        break;
                    }

                case Field_OwnReactions:
                    {
                        activity.OwnReactions = prop.Value.ToObject <IDictionary <string, IEnumerable <Reaction> > >();
                        break;
                    }

                case Field_LatestReactions:
                    {
                        activity.LatestReactions = prop.Value.ToObject <IDictionary <string, IEnumerable <Reaction> > >();
                        break;
                    }

                case Field_ReactionCounts:
                    {
                        activity.ReactionCounts = prop.Value.ToObject <IDictionary <string, int> >();
                        break;
                    }

                default:
                    {
                        // stash everything else as custom
                        activity._data[prop.Name] = prop.Value;
                        break;
                    };
                }
            });
            return(activity);
        }