Example #1
0
        public static SwrveTrigger LoadFromJson(object json)
        {
            IDictionary <string, object> dict = null;

            try {
                dict = (IDictionary <string, object>)json;
            } catch (Exception e) {
                SwrveLog.LogError(string.Format("Invalid object passed in to LoadFromJson, expected Dictionary<string, object>, received {0}", json));
                return(null);
            }

            string          eventName  = null;
            SwrveConditions conditions = null;

            try {
                eventName = (string)dict [EVENT_NAME_KEY];
                if (dict.ContainsKey(CONDITIONS_KEY))
                {
                    conditions = SwrveConditions.LoadFromJson((IDictionary <string, object>)dict [CONDITIONS_KEY], true);
                }
            } catch (Exception e) {
                SwrveLog.LogError(string.Format("Error parsing a SwrveTrigger from json {0}, ex: {1}", dict, e));
            }

            if (string.IsNullOrEmpty(eventName) || (conditions == null))
            {
                return(null);
            }

            SwrveTrigger trigger = new SwrveTrigger();

            trigger.eventName  = eventName;
            trigger.conditions = conditions;
            return(trigger);
        }
Example #2
0
 public static SwrveConditions LoadFromJson(IDictionary <string, object> json, bool isRoot)
 {
     if (json.Keys.Count == 0)
     {
         if (isRoot)
         {
             return(new SwrveConditions(null));
         }
         return(null);
     }
     else
     {
         string a = (string)json["op"];
         if (a == "eq")
         {
             string text  = (string)json["key"];
             string text2 = (string)json["value"];
             if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
             {
                 return(null);
             }
             return(new SwrveConditions(new SwrveConditions.TriggerOperatorType?(SwrveConditions.TriggerOperatorType.EQUALS), text, text2));
         }
         else
         {
             if (!isRoot || !(a == "and"))
             {
                 return(null);
             }
             IList <object>         list       = (IList <object>)json["args"];
             List <SwrveConditions> list2      = new List <SwrveConditions>();
             IEnumerator <object>   enumerator = list.GetEnumerator();
             while (enumerator.MoveNext())
             {
                 SwrveConditions swrveConditions = SwrveConditions.LoadFromJson((Dictionary <string, object>)enumerator.Current, false);
                 if (swrveConditions == null)
                 {
                     return(null);
                 }
                 list2.Add(swrveConditions);
             }
             if (list2.Count == 0)
             {
                 return(null);
             }
             return(new SwrveConditions(new SwrveConditions.TriggerOperatorType?(SwrveConditions.TriggerOperatorType.AND), list2));
         }
     }
 }
        public static SwrveConditions LoadFromJson(IDictionary <string, object> json, bool isRoot)
        {
            if (0 == json.Keys.Count)
            {
                if (isRoot)
                {
                    return(new SwrveConditions(null));
                }
                return(null);
            }

            string op = (string)json [OP_KEY];

            if (op == OP_EQ_KEY)
            {
                string key   = (string)json [KEY_KEY];
                string value = (string)json [VALUE_KEY];
                if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
                {
                    return(null);
                }
                return(new SwrveConditions(TriggerOperatorType.EQUALS, key, value));
            }
            else if (isRoot && (op == OP_AND_KEY))
            {
                IList <object>         jsonArgs = (IList <object>)json [ARGS_KEY];
                List <SwrveConditions> args     = new List <SwrveConditions> ();
                IEnumerator <object>   it       = jsonArgs.GetEnumerator();
                while (it.MoveNext())
                {
                    SwrveConditions condition = LoadFromJson((Dictionary <string, object>)it.Current, false);
                    if (condition == null)
                    {
                        return(null);
                    }
                    args.Add(condition);
                }
                if (args.Count == 0)
                {
                    return(null);
                }
                return(new SwrveConditions(TriggerOperatorType.AND, args));
            }

            return(null);
        }
Example #4
0
        public static SwrveTrigger LoadFromJson(object json)
        {
            IDictionary <string, object> dictionary = null;

            try
            {
                dictionary = (IDictionary <string, object>)json;
            }
            catch (Exception ex)
            {
                SwrveLog.LogError(string.Format("Invalid object passed in to LoadFromJson, expected Dictionary<string, object>, received {0}, exception: {1}", json, ex.Message));
                SwrveTrigger result = null;
                return(result);
            }
            string          value           = null;
            SwrveConditions swrveConditions = null;

            try
            {
                value = (string)dictionary["event_name"];
                if (dictionary.ContainsKey("conditions"))
                {
                    swrveConditions = SwrveConditions.LoadFromJson((IDictionary <string, object>)dictionary["conditions"], true);
                }
            }
            catch (Exception arg)
            {
                SwrveLog.LogError(string.Format("Error parsing a SwrveTrigger from json {0}, ex: {1}", dictionary, arg));
            }
            if (string.IsNullOrEmpty(value) || swrveConditions == null)
            {
                return(null);
            }
            return(new SwrveTrigger
            {
                eventName = value,
                conditions = swrveConditions
            });
        }
Example #5
0
        public static SwrveTrigger LoadFromJson(object json)
        {
            IDictionary <string, object> dictionary = null;

            try
            {
                dictionary = (IDictionary <string, object>)json;
            }
            catch (Exception ex)
            {
                SwrveLog.LogError($"Invalid object passed in to LoadFromJson, expected Dictionary<string, object>, received {json}, exception: {ex.Message}");
                return(null);
            }
            string          value           = null;
            SwrveConditions swrveConditions = null;

            try
            {
                value = (string)dictionary["event_name"];
                if (dictionary.ContainsKey("conditions"))
                {
                    swrveConditions = SwrveConditions.LoadFromJson((IDictionary <string, object>)dictionary["conditions"], isRoot: true);
                }
            }
            catch (Exception ex)
            {
                SwrveLog.LogError($"Error parsing a SwrveTrigger from json {dictionary}, ex: {ex}");
            }
            if (string.IsNullOrEmpty(value) || swrveConditions == null)
            {
                return(null);
            }
            SwrveTrigger swrveTrigger = new SwrveTrigger();

            swrveTrigger.eventName  = value;
            swrveTrigger.conditions = swrveConditions;
            return(swrveTrigger);
        }