コード例 #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);
        }
コード例 #2
0
        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);
        }