예제 #1
0
        protected virtual IDictionary <string, object> MigrateToNewSchema(IDictionary <string, object> rec, IDictionary <string, Type> recTypes)
        {
            IDictionary <string, object> newRec = new ChoDynamicObject(); //  new Dictionary<string, object>();

            foreach (var kvp in rec)
            {
                //newRec.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture);
                if (recTypes.ContainsKey(kvp.Key))
                {
                    newRec.Add(kvp.Key, kvp.Value.CastObjectTo(recTypes[kvp.Key]));
                }
                else
                {
                    newRec.Add(kvp.Key, typeof(ChoDynamicObject));
                }

                if (kvp.Value == null)
                {
                    var dobj = newRec as ChoDynamicObject;
                    dobj.SetMemberType(kvp.Key, recTypes[kvp.Key]);
                }
            }
            return(newRec);
        }
예제 #2
0
        public static dynamic ConvertToNestedObject(this object @this, char separator = '/')
        {
            if (separator == ChoCharEx.NUL)
            {
                throw new ArgumentException("Invalid separator passed.");
            }

            if (@this == null || [email protected]().IsDynamicType())
            {
                return(@this);
            }

            IDictionary <string, object> expandoDic = null;

            expandoDic = @this is ExpandoObject || @this is ChoDynamicObject ? (IDictionary <string, object>)@this : ToExpandoObject(@this as DynamicObject);
            IDictionary <string, object> root = new ChoDynamicObject();

            foreach (var kvp in expandoDic)
            {
                if (kvp.Key.IndexOf(separator) >= 0)
                {
                    var tokens = kvp.Key.SplitNTrim(separator).Where(e => !e.IsNullOrWhiteSpace()).ToArray();
                    IDictionary <string, object> current = root;
                    foreach (var token in tokens.Take(tokens.Length - 1))
                    {
                        if (token.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        if (!current.ContainsKey(token))
                        {
                            current.Add(token, new ChoDynamicObject());
                        }

                        current = current[token] as IDictionary <string, object>;
                    }
                    current.AddOrUpdate(tokens[tokens.Length - 1], kvp.Value);
                }
                else
                {
                    root.Add(kvp.Key, kvp.Value);
                }
            }

            return(root as ChoDynamicObject);
        }
예제 #3
0
        private object ToDynamicObject(object rec)
        {
            if (!(rec is AvroRecord))
            {
                return(rec);
            }

            var output = new ChoDynamicObject();

            var avroRec = ((AvroRecord)rec);
            var schema  = avroRec.Schema;

            foreach (var f in schema.Fields)
            {
                output.Add(f.Name, ToDynamicObject(avroRec[f.Name]));
            }

            return(output);
        }
예제 #4
0
        public static dynamic ConvertToNestedObject(this object @this, char separator = '/',
                                                    int maxArraySize = 100)
        {
            if (separator == ChoCharEx.NUL)
            {
                throw new ArgumentException("Invalid separator passed.");
            }

            if (@this == null)
            {
                return(@this);
            }
            if (!(@this is ExpandoObject || @this is ChoDynamicObject || @this is IDictionary <string, object>))
            {
                return(@this);
            }

            IDictionary <string, object> expandoDic = (IDictionary <string, object>)@this;
            IDictionary <string, object> root       = new ChoDynamicObject();

            foreach (var kvp in expandoDic)
            {
                if (kvp.Key.IndexOf(separator) >= 0)
                {
                    var tokens = kvp.Key.SplitNTrim(separator).Where(e => !e.IsNullOrWhiteSpace()).ToArray();
                    IDictionary <string, object> current    = root;
                    List <ChoDynamicObject>      currentArr = null;
                    string nextToken = null;
                    string token     = null;

                    int length = tokens.Length - 1;
                    int index  = 0;
                    for (int i = 0; i < length; i++)
                    {
                        nextToken = null;
                        token     = tokens[i];

                        if (i + 1 < length)
                        {
                            nextToken = tokens[i + 1];
                        }

                        if (token.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        if (Int32.TryParse(nextToken, out index) && index >= 0 && index < maxArraySize)
                        {
                            if (!current.ContainsKey(token))
                            {
                                current.Add(token, new List <ChoDynamicObject>());
                            }

                            currentArr = current[token] as List <ChoDynamicObject>;
                        }
                        else
                        {
                            if (Int32.TryParse(token, out index))
                            {
                                if (index >= 0 && index < maxArraySize && currentArr != null)
                                {
                                    if (index < currentArr.Count)
                                    {
                                        current = currentArr[index] as IDictionary <string, object>;
                                    }
                                    else
                                    {
                                        int count = index - currentArr.Count + 1;
                                        for (int j = 0; j < count; j++)
                                        {
                                            currentArr.Add(new ChoDynamicObject());
                                        }
                                    }
                                    current = currentArr[index];
                                }
                            }
                            else if (current != null)
                            {
                                if (!current.ContainsKey(token))
                                {
                                    current.Add(token, new ChoDynamicObject(token));
                                }

                                current    = current[token] as IDictionary <string, object>;
                                currentArr = null;
                            }
                        }
                    }
                    if (current != null)
                    {
                        current.AddOrUpdate(tokens[tokens.Length - 1], kvp.Value);
                    }
                }
                else
                {
                    root.Add(kvp.Key, kvp.Value);
                }
            }

            return(root as ChoDynamicObject);
        }
예제 #5
0
        public static dynamic ConvertMembersToArrayIfAny(this object @this, char separator = '_', bool allowNestedConversion = true)
        {
            if (separator == ChoCharEx.NUL)
            {
                throw new ArgumentException("Invalid separator passed.");
            }

            if (@this == null)
            {
                return(@this);
            }
            if (!(@this is ExpandoObject || @this is ChoDynamicObject || @this is IDictionary <string, object>))
            {
                return(@this);
            }

            IDictionary <string, object> expandoDic = (IDictionary <string, object>)@this;
            IDictionary <string, object> root       = new ChoDynamicObject();

            object value = null;

            foreach (var kvp in expandoDic)
            {
                var pos = kvp.Key.LastIndexOf(separator);
                value = allowNestedConversion ? ConvertMembersToArrayIfAny(kvp.Value, separator, allowNestedConversion) : kvp.Value;

                if (pos <= 0)
                {
                    root.Add(kvp.Key, value);
                }
                else
                {
                    var key        = kvp.Key.Substring(0, pos);
                    var indexValue = kvp.Key.Substring(pos + 1);
                    int index      = 0;

                    if (!int.TryParse(indexValue, out index))
                    {
                        root.Add(kvp.Key, value);
                    }
                    else
                    {
                        if (!root.ContainsKey(key))
                        {
                            root.Add(key, new object[] { });
                        }

                        var arrValue = root[key] as object[];
                        if (index + 1 > arrValue.Length)
                        {
                            Array.Resize(ref arrValue, index + 1);
                            root[key] = arrValue;
                        }

                        arrValue[index] = value;
                    }
                }
            }

            return(root as ChoDynamicObject);
        }
예제 #6
0
        public static dynamic ConvertMembersToArrayIfAny(this object @this, char?startSeparator = null, char?endSeparator = null, bool allowNestedConversion = true)
        {
            if (startSeparator == null || startSeparator.Value == ChoCharEx.NUL)
            {
                startSeparator = '[';
                if (endSeparator == null || endSeparator.Value == ChoCharEx.NUL)
                {
                    endSeparator = ']';
                }
            }

            if (@this == null)
            {
                return(@this);
            }
            if (!(@this is ExpandoObject || @this is ChoDynamicObject || @this is IDictionary <string, object>))
            {
                return(@this);
            }

            IDictionary <string, object> expandoDic = (IDictionary <string, object>)@this;
            IDictionary <string, object> root       = new ChoDynamicObject();

            object value = null;

            foreach (var kvp in expandoDic)
            {
                value = allowNestedConversion ? ConvertMembersToArrayIfAny(kvp.Value, startSeparator, endSeparator, allowNestedConversion) : kvp.Value;

                var pos = kvp.Key.LastIndexOf(startSeparator.Value);
                if (pos <= 0)
                {
                    root.Add(kvp.Key, value);
                }
                else
                {
                    var key        = kvp.Key.Substring(0, pos);
                    var indexValue = kvp.Key.Substring(pos + 1);
                    if (endSeparator != null && indexValue.IndexOf(endSeparator.Value) >= 0)
                    {
                        indexValue = indexValue.Substring(0, indexValue.IndexOf(endSeparator.Value));
                    }

                    int index = 0;

                    if (!int.TryParse(indexValue, out index))
                    {
                        root.Add(kvp.Key, value);
                    }
                    else
                    {
                        if (!root.ContainsKey(key))
                        {
                            root.Add(key, new object[] { });
                        }

                        var arrValue = root[key] as object[];
                        if (index + 1 > arrValue.Length)
                        {
                            Array.Resize(ref arrValue, index + 1);
                            root[key] = arrValue;
                        }

                        arrValue[index] = value;
                    }
                }
            }

            return(root as ChoDynamicObject);
        }