Esempio n. 1
0
        private static T ToObject <T>(BlittableJsonReaderObject json, string name, Func <BlittableJsonReaderObject, T> converter) where T : new()
        {
            if (json.TryGet(name, out BlittableJsonReaderObject obj) == false || obj == null)
            {
                return(default(T));
            }

            return(converter(obj));
        }
Esempio n. 2
0
        private static T GetPrimitiveProperty <T>(BlittableJsonReaderObject json, string prop)
        {
            if (json.TryGet(prop, out T val) == false)
            {
                ThrowInvalidPrimitiveCastException(prop, typeof(T).Name, json);
            }

            return(val);
        }
Esempio n. 3
0
        private static T[] ToArray <T>(BlittableJsonReaderObject json, string name, Func <BlittableJsonReaderObject, T> converter)
        {
            var list = new List <T>();

            BlittableJsonReaderArray array;

            if (json.TryGet(name, out array) == false || array == null)
            {
                return(list.ToArray());
            }

            foreach (BlittableJsonReaderObject item in array.Items)
            {
                list.Add(converter(item));
            }

            return(list.ToArray());
        }
Esempio n. 4
0
        private static string[] ToArrayOfString(BlittableJsonReaderObject json, string name)
        {
            var collection = new List <string>();

            BlittableJsonReaderArray jsonArray;

            if (json.TryGet(name, out jsonArray) == false || jsonArray == null)
            {
                return(collection.ToArray());
            }

            foreach (var value in jsonArray)
            {
                collection.Add(value.ToString());
            }

            return(collection.ToArray());
        }
Esempio n. 5
0
        private static TCollection ToCollectionOfString <TCollection>(BlittableJsonReaderObject json, string name)
            where TCollection : ICollection <string>, new()
        {
            var collection = new TCollection();

            BlittableJsonReaderArray jsonArray;

            if (json.TryGet(name, out jsonArray) == false || jsonArray == null)
            {
                return(collection);
            }

            foreach (var value in jsonArray)
            {
                collection.Add(value.ToString());
            }

            return(collection);
        }
Esempio n. 6
0
        private static Dictionary <string, string> ToDictionaryOfString(BlittableJsonReaderObject json, string name)
        {
            var dic = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            BlittableJsonReaderObject obj;

            //should a "null" exist in json? -> not sure that "null" can exist there
            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dic);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                object val;
                if (obj.TryGet(propertyName, out val))
                {
                    dic[propertyName] = val?.ToString();
                }
            }
            return(dic);
        }
Esempio n. 7
0
        private static Dictionary <TEnum, string> ToDictionaryOfEnumKeys <TEnum>(BlittableJsonReaderObject json, string name)
        {
            var dic = new Dictionary <TEnum, string>();

            BlittableJsonReaderObject obj;

            //should a "null" exist in json? -> not sure that "null" can exist there
            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dic);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                object val;
                if (obj.TryGet(propertyName, out val))
                {
                    dic[(TEnum)Enum.Parse(typeof(TEnum), propertyName, true)] = val?.ToString();
                }
            }
            return(dic);
        }
Esempio n. 8
0
        private static Dictionary <string, TEnum> ToDictionaryOfEnum <TEnum>(BlittableJsonReaderObject json, string name)
        {
            var dic = new Dictionary <string, TEnum>(StringComparer.OrdinalIgnoreCase);

            BlittableJsonReaderObject obj;

            //should a "null" exist in json? -> not sure that "null" can exist there
            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dic);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                string val;
                if (obj.TryGet(propertyName, out val))
                {
                    dic[propertyName] = (TEnum)Enum.Parse(typeof(TEnum), val, true);
                }
            }
            return(dic);
        }
Esempio n. 9
0
        private static Dictionary <string, T> ToDictionaryOfPrimitive <T>(BlittableJsonReaderObject json, string name)
            where T : struct
        {
            var dic = new Dictionary <string, T>(StringComparer.OrdinalIgnoreCase);

            BlittableJsonReaderObject obj;

            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dic);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                object val;
                if (obj.TryGetMember(propertyName, out val))
                {
                    dic[propertyName] = (T)val;
                }
            }
            return(dic);
        }
Esempio n. 10
0
        private static Dictionary <string, Dictionary <string, string[]> > ToDictionaryOfDictionaryOfStringArray(BlittableJsonReaderObject json, string name)
        {
            var dic = new Dictionary <string, Dictionary <string, string[]> >(StringComparer.OrdinalIgnoreCase);

            BlittableJsonReaderObject obj;

            //should a "null" exist in json? -> not sure that "null" can exist there
            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dic);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                BlittableJsonReaderObject result;
                if (obj.TryGet(propertyName, out result))
                {
                    var prop = new Dictionary <string, string[]>();
                    dic[propertyName] = prop;
                    foreach (var innerPropName in result.GetPropertyNames())
                    {
                        BlittableJsonReaderArray val;
                        if (result.TryGet(innerPropName, out val))
                        {
                            var array = new string[val.Length];
                            for (int i = 0; i < val.Length; i++)
                            {
                                array[i] = val[i]?.ToString();
                            }
                            prop[innerPropName] = array;
                        }
                    }
                }
            }
            return(dic);
        }
Esempio n. 11
0
        private static Dictionary <TK, TV> ToDictionary <TK, TV>(BlittableJsonReaderObject json, string name, Func <BlittableJsonReaderObject, TV> converter)
        {
            var isStringKey = typeof(TK) == typeof(string);
            var dictionary  = new Dictionary <TK, TV>((IEqualityComparer <TK>)StringComparer.Ordinal); // we need to deserialize it as we got it, keys could be case sensitive - DB-8713

            BlittableJsonReaderObject obj;

            if (json.TryGet(name, out obj) == false || obj == null)
            {
                return(dictionary);
            }

            foreach (var propertyName in obj.GetPropertyNames())
            {
                object val;
                if (obj.TryGetMember(propertyName, out val) == false)
                {
                    continue;
                }

                dynamic key;
                if (isStringKey)
                {
                    key = propertyName;
                }
                else
                {
                    key = (TK)Convert.ChangeType(propertyName, typeof(TK));
                }

                var typeOfValue = typeof(TV);
                if (typeOfValue.IsConstructedGenericType && typeOfValue.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    var keyType      = typeOfValue.GenericTypeArguments[0];
                    var valueType    = typeOfValue.GenericTypeArguments[1];
                    var newConverter = GetConverterFromCache(valueType);
                    var methodInfo   = typeof(JsonDeserializationBase)
                                       .GetMethod("ToDictionary", BindingFlags.NonPublic | BindingFlags.Static);
                    var method = methodInfo.MakeGenericMethod(keyType, valueType);
                    var result = method.Invoke(null, new[] { obj, key, newConverter });
                    dictionary[key] = (TV)Convert.ChangeType(result, typeOfValue);
                }
                else
                {
                    if (typeOfValue != typeof(object) &&
                        val is BlittableJsonReaderObject blittableJsonReaderObject)
                    {
                        dictionary[key] = converter(blittableJsonReaderObject);
                    }
                    else
                    {
                        if (val is BlittableJsonReaderArray)
                        {
                            ThrowNotSupportedBlittableArray(propertyName);
                        }

                        obj.TryGet(propertyName, out TV value);
                        dictionary[key] = value;
                    }
                }
            }
            return(dictionary);
        }