コード例 #1
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);
        }
コード例 #2
0
 private static void ThrowInvalidPrimitiveCastException(string prop, string type, BlittableJsonReaderObject json)
 {
     throw new InvalidCastException($"Failed to fetch property name = {prop} of type {type} from json with value : [{json}]");
 }
コード例 #3
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);
        }