Example #1
0
        /// <summary>
        /// Create an Expando from a dictionary
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="expando">Expando instance</param>
        public Expando(IDictionary <string, object> dict)
        {
            var expando = this;

            Initialize(expando);

            Properties = new PropertyBag();

            foreach (var kvp in dict)
            {
                var kvpValue = kvp.Value;
                if (kvpValue is IDictionary <string, object> )
                {
                    var expandoVal = new Expando(kvpValue);
                    expando[kvp.Key] = expandoVal;
                }
                else if (kvp.Value is ICollection)
                {
                    // iterate through the collection and convert any string-object dictionaries
                    // along the way into expando objects
                    var objList = new List <object>();
                    foreach (var item in (ICollection)kvp.Value)
                    {
                        var itemVals = item as IDictionary <string, object>;
                        if (itemVals != null)
                        {
                            var expandoItem = new Expando(itemVals);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }
                    expando[kvp.Key] = objList;
                }
                else
                {
                    expando[kvp.Key] = kvpValue;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Converts an <see cref="IDictionary&lt;string, object&gt;"/> into an <see cref="Expando"/>
        /// </summary>
        /// <returns><see cref="Expando"/></returns>
        public static Expando ToIndexableExpando(IDictionary <string, object> dict)
        {
            var expando = new Expando();


            foreach (var kvp in dict)
            {
                var kvpValue = kvp.Value as IDictionary <string, object>;
                if (kvpValue != null)
                {
                    var expandoVal = ToIndexableExpando(kvpValue);
                    expando[kvp.Key] = expandoVal;
                }
                else if (kvp.Value is ICollection)
                {
                    // iterate through the collection and convert any string-object dictionaries
                    // along the way into expando objects
                    var objList = new List <object>();
                    foreach (var item in (ICollection)kvp.Value)
                    {
                        var itemVals = item as IDictionary <string, object>;
                        if (itemVals != null)
                        {
                            var expandoItem = ToIndexableExpando(itemVals);
                            objList.Add(expandoItem);
                        }
                        else
                        {
                            objList.Add(item);
                        }
                    }
                    expando[kvp.Key] = objList;
                }
                else
                {
                    expando[kvp.Key] = kvp.Value;
                }
            }
            return(expando);
        }