Beispiel #1
0
        private static List <Dictionary <string, object> > ParseListOfDictionary(IList <object> input)
        {
            List <Dictionary <string, object> > list = new List <Dictionary <string, object> >();

            foreach (object current in input)
            {
                Dictionary <string, object> item = null;
                if (current is IDictionary)
                {
                    item = RequestMap.ParseDictionary((IDictionary <string, object>)current);
                }
                else if (current is JObject)
                {
                    item = RequestMap.ParseDictionary(((JObject)current).ToObject <Dictionary <string, object> >());
                }
                list.Add(item);
            }
            return(list);
        }
Beispiel #2
0
        /// <summary>
        /// Associates the specified value to the specified key path. </summary>
        /// <param name="keyPath"> key path to which the specified value is to be associated. </param>
        /// <param name="value"> the value which is to be associated with the specified key path. </param>
        /// <exception cref="IllegalArgumentException"> if part of the key path does not match the expected type. </exception>
        /// <exception cref="IndexOutOfBoundsException"> if using an array index in the key path is out of bounds. </exception>
        public void Add(String keyPath, Object value)
        {
            string[] properties = keyPath.Split('.');
            IDictionary <string, object> destinationObject = __storage;

            if (properties.Length > 1)
            {
                for (int i = 0; i < (properties.Length - 1); i++)
                {
                    string property = properties [i];
                    if (property.Contains("["))
                    {
                        destinationObject = getDestinationMap(property, destinationObject);
                    }
                    else
                    {
                        destinationObject = getPropertyMapFrom(property, destinationObject);
                    }
                }
            }
            else if (keyPath.Contains("["))
            {
                destinationObject = getDestinationMap(keyPath, destinationObject);
            }

            // TODO: need to take care of the case where we are inserting a value into an array rather than
            // map ( eg map.put("a[2]", 123);

            if (destinationObject == __storage)
            {
                __storage [keyPath] = value;
            }
            else if (value is IDictionary)                 // if putting a map, call put all
            {
                destinationObject.Clear();
                RequestMap newMap = new RequestMap((Dictionary <String, Object>)value);
                destinationObject [properties [properties.Length - 1]] = newMap;
            }
            else
            {
                destinationObject [properties [properties.Length - 1]] = value;
            }
        }
Beispiel #3
0
        private static Dictionary <string, object> ParseDictionary(IDictionary <string, object> input)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> current in input)
            {
                object value;
                if (current.Value is IDictionary)
                {
                    value = RequestMap.ParseDictionary((IDictionary <string, object>)current.Value);
                }
                else if (current.Value is JObject)
                {
                    value = RequestMap.ParseDictionary(((JObject)current.Value).ToObject <IDictionary <string, object> >());
                }
                else if (current.Value is JArray)
                {
                    if (((JArray)current.Value).Count > 0)
                    {
                        JToken jToken = ((JArray)current.Value)[0];
                        if (jToken is JObject || jToken is IDictionary)
                        {
                            value = RequestMap.ParseListOfDictionary(((JArray)current.Value).ToObject <List <object> >());
                        }
                        else
                        {
                            value = RequestMap.ParseListOfObjects(((JArray)current.Value).ToObject <List <object> >());
                        }
                    }
                    else
                    {
                        value = RequestMap.ParseListOfObjects(((JArray)current.Value).ToObject <List <object> >());
                    }
                }
                else
                {
                    value = current.Value;
                }
                dictionary.Add(current.Key, value);
            }
            return(dictionary);
        }
Beispiel #4
0
        public static IDictionary <string, object> AsDictionary(string json)
        {
            IDictionary <string, object> result = new Dictionary <string, object>();

            if (json?.Trim().StartsWith("{") ?? false)
            {
                result = RequestMap.ParseDictionary(JsonConvert.DeserializeObject <IDictionary <string, object> >(json));
            }
            else if (json?.Trim().StartsWith("[") ?? false)
            {
                IList <object> input = JsonConvert.DeserializeObject <List <object> >(json);
                result = new Dictionary <string, object>
                {
                    {
                        "list",
                        RequestMap.ParseListOfDictionary(input)
                    }
                };
            }
            return(result);
        }
 protected BaseObject(RequestMap bm) : base(bm)
 {
 }
Beispiel #6
0
 protected internal void UpdateFromBaseMap(RequestMap baseMapToSet)
 {
     this.__storage = baseMapToSet.__storage;
 }
Beispiel #7
0
 public RequestMap(string jsonMapString)
 {
     this.__storage = new Dictionary <string, object>();
     this.AddAll(RequestMap.AsDictionary(jsonMapString));
 }
Beispiel #8
0
 public RequestMap(RequestMap bm)
 {
     this.__storage = bm.__storage;
 }
Beispiel #9
0
 /// <summary>
 /// Consturcts a map based of the speficied JSON string. </summary>
 /// <param name="jsonMapString"> the JSON string used to construct the map </param>
 public RequestMap(string jsonMapString)
 {
     __storage = new Dictionary <String, Object> ();
     AddAll(RequestMap.AsDictionary(jsonMapString));
 }
Beispiel #10
0
 /// <summary>
 /// Constructs an empty map with the default capacity and load factor.
 /// </summary>
 public RequestMap(RequestMap bm) : base(bm)
 {
 }