Пример #1
0
        /// <summary>
        /// 设置预定义的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool SetPredefinedKeyValue(string key, object value)
        {
            var val = JToken.FromObject(value);

            if (ROOT_KEYS.Contains(key))
            {
                RootJson.Add(key, val);
            }
            else if (APS_KEYS.Contains(key))
            {
                JObject apsJson     = null;
                JObject payloadJson = null;
                if (RootJson.ContainsKey("payload"))
                {
                    payloadJson = RootJson.Value <JObject>("payload");
                }
                else
                {
                    payloadJson = new JObject();
                    RootJson.Add("payload", payloadJson);
                }
                if (payloadJson.ContainsKey("aps"))
                {
                    apsJson = payloadJson.Value <JObject>("aps");
                }
                else
                {
                    apsJson = new JObject();
                    payloadJson.Add("aps", apsJson);
                }
                apsJson.Add(key, val);
            }
            else if (POLICY_KEYS.Contains(key))
            {
                JObject policyJson = null;
                if (RootJson.ContainsKey("policy"))
                {
                    policyJson = RootJson.Value <JObject>("policy");
                }
                else
                {
                    policyJson = new JObject();
                    RootJson.Add("policy", policyJson);
                }
                policyJson.Add(key, val);
            }
            else
            {
                if (key == "payload" || key == "aps" || key == "policy")
                {
                    throw new Exception(" 未找到 " + key + "在集合中");
                }
                else
                {
                    throw new Exception("未知 key: " + key);
                }
            }
            return(true);
        }
Пример #2
0
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: @Override public boolean setPredefinedKeyValue(String key, Object value) throws Exception
        public override bool setPredefinedKeyValue(JObject rootJson, string key, string value)
        {
            if (ROOT_KEYS.Contains(key))
            {
                // This key should be in the root level
                rootJson.Add(key, value);
            }
            else if (APS_KEYS.Contains(key))
            {
                // This key should be in the aps level
                JObject apsJson     = null;
                JObject payloadJson = null;
                if (rootJson.Property("payload") != null)
                {
                    payloadJson = rootJson.GetValue("payload") as JObject;
                }
                else
                {
                    payloadJson = new JObject();
                    rootJson.Add("payload", payloadJson);
                }
                if (payloadJson.Property("aps") != null)
                {
                    apsJson = payloadJson.GetValue("aps") as JObject;
                }
                else
                {
                    apsJson = new JObject();
                    payloadJson.Add("aps", apsJson);
                }
                apsJson.Add(key, value);
            }
            else if (POLICY_KEYS.Contains(key))
            {
                // This key should be in the body level
                JObject policyJson = null;
                if (rootJson.Property("policy") != null)
                {
                    policyJson = rootJson.GetValue("policy") as JObject;
                }
                else
                {
                    policyJson = new JObject();
                    rootJson.Add("policy", policyJson);
                }
                policyJson.Add(key, value);
            }
            else
            {
                if (key == "payload" || key == "aps" || key == "policy")
                {
                    throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
                }
                else
                {
                    throw new Exception("Unknownd key: " + key);
                }
            }

            return(true);
        }
Пример #3
0
        // Set key/value in the rootJson, for the keys can be set please see ROOT_KEYS, PAYLOAD_KEYS,
        // BODY_KEYS and POLICY_KEYS.
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: @Override public boolean setPredefinedKeyValue(String key, Object value) throws Exception
        public override bool setPredefinedKeyValue(JObject rootJson, string key, string value)
        {
            if (ROOT_KEYS.Contains(key))
            {
                // This key should be in the root level
                rootJson.Add(key, value);
            }
            else if (PAYLOAD_KEYS.Contains(key))
            {
                // This key should be in the payload level
                JObject payloadJson = null;

                if (rootJson.Property("payload") != null)
                {
                    payloadJson = rootJson.GetValue("payload") as JObject;
                }
                else
                {
                    payloadJson = new JObject();
                    rootJson.Add("payload", payloadJson);
                }
                payloadJson.Add(key, value);
            }
            else if (BODY_KEYS.Contains(key))
            {
                // This key should be in the body level
                JObject bodyJson    = null;
                JObject payloadJson = null;
                // 'body' is under 'payload', so build a payload if it doesn't exist
                if (rootJson.Property("payload") != null)
                {
                    payloadJson = rootJson.GetValue("payload") as JObject;
                }
                else
                {
                    payloadJson = new JObject();
                    rootJson.Add("payload", payloadJson);
                }
                // Get body JsonObject, generate one if not existed
                if (payloadJson.Property("body") != null)
                {
                    bodyJson = payloadJson.GetValue("body") as JObject;
                }
                else
                {
                    bodyJson = new JObject();
                    payloadJson.Add("body", bodyJson);
                }
                bodyJson.Add(key, value);
            }
            else if (POLICY_KEYS.Contains(key))
            {
                // This key should be in the body level
                JObject policyJson = null;
                if (rootJson.Property("policy") != null)
                {
                    policyJson = rootJson.GetValue("policy") as JObject;
                }
                else
                {
                    policyJson = new JObject();
                    rootJson.Add("policy", policyJson);
                }
                policyJson.Add(key, value);
            }
            else
            {
                if (key == "payload" || key == "body" || key == "policy" || key == "extra")
                {
                    throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
                }
                else
                {
                    throw new Exception("Unknown key: " + key);
                }
            }
            return(true);
        }
Пример #4
0
        /// <summary>
        /// 设置预定义的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool SetPredefinedKeyValue(string key, object value)
        {
            var val = JToken.FromObject(value);

            if (ROOT_KEYS.Contains(key))
            {
                RootJson.Add(key, val);
            }
            else if (PAYLOAD_KEYS.Contains(key))
            {
                JObject payloadJson = null;
                if (RootJson.ContainsKey("payload"))
                {
                    payloadJson = RootJson.Value <JObject>("payload");
                }
                else
                {
                    payloadJson = new JObject();
                    RootJson.Add("payload", payloadJson);
                }
                payloadJson.Add(key, val);
            }
            else if (BODY_KEYS.Contains(key))
            {
                JObject bodyJson    = null;
                JObject payloadJson = null;
                //
                if (RootJson.ContainsKey("payload"))
                {
                    payloadJson = RootJson.Value <JObject>("payload");
                }
                else
                {
                    payloadJson = new JObject();
                    RootJson.Add("payload", payloadJson);
                }
                if (payloadJson.ContainsKey("body"))
                {
                    bodyJson = payloadJson.Value <JObject>("body");
                }
                else
                {
                    bodyJson = new JObject();
                    payloadJson.Add("body", bodyJson);
                }
                bodyJson.Add(key, val);
            }
            else if (POLICY_KEYS.Contains(key))
            {
                JObject policyJson = null;
                if (RootJson.ContainsKey("policy"))
                {
                    policyJson = RootJson.Value <JObject>("policy");
                }
                else
                {
                    policyJson = new JObject();
                    RootJson.Add("policy", policyJson);
                }
                policyJson.Add(key, val);
            }
            else
            {
                if (key == "payload" || key == "body" || key == "policy" || key == "extra")
                {
                    throw new Exception("没有添加 " + key + " , 到HashSet集合");
                }
                else
                {
                    throw new Exception("未知的 key: " + key);
                }
            }
            return(true);
        }