/// <summary>
        /// Deserialize the json list array token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json list array token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(List <>))
            {
                return(null);
            }

            LazyJsonArray jsonArray = (LazyJsonArray)jsonToken;
            Object        list      = Activator.CreateInstance(dataType);
            MethodInfo    methodAdd = dataType.GetMethods().First(x => x.Name == "Add");

            Type jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[0]);

            if (jsonDeserializerType == null)
            {
                jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[0]);
            }

            if (jsonDeserializerType != null)
            {
                LazyJsonDeserializerBase jsonDeserializer = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType);

                for (int index = 0; index < jsonArray.Count; index++)
                {
                    methodAdd.Invoke(list, new Object[] { jsonDeserializer.Deserialize(jsonArray.TokenList[index], dataType.GenericTypeArguments[0]) });
                }
            }
            else
            {
                for (int index = 0; index < jsonArray.Count; index++)
                {
                    methodAdd.Invoke(list, new Object[] { LazyJsonDeserializer.DeserializeToken(jsonArray.TokenList[index], dataType.GenericTypeArguments[0]) });
                }
            }

            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserialize the json array token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json array token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsArray == false)
            {
                return(null);
            }

            LazyJsonArray jsonArray        = (LazyJsonArray)jsonToken;
            Array         array            = Array.CreateInstance(dataType.GetElementType(), jsonArray.Count);
            Type          arrayElementType = dataType.GetElementType();

            Type jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(arrayElementType);

            if (jsonDeserializerType == null)
            {
                jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, arrayElementType);
            }

            if (jsonDeserializerType != null)
            {
                LazyJsonDeserializerBase jsonDeserializer = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType);

                for (int index = 0; index < jsonArray.Count; index++)
                {
                    array.SetValue(jsonDeserializer.Deserialize(jsonArray.TokenList[index], arrayElementType), index);
                }
            }
            else
            {
                for (int index = 0; index < jsonArray.Count; index++)
                {
                    array.SetValue(LazyJsonDeserializer.DeserializeToken(jsonArray.TokenList[index], arrayElementType), index);
                }
            }

            return(array);
        }
        /// <summary>
        /// Deserialize the json dictionary array token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json dictionary array token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
            {
                return(null);
            }

            LazyJsonArray jsonArray  = (LazyJsonArray)jsonToken;
            Object        dictionary = Activator.CreateInstance(dataType);
            MethodInfo    methodAdd  = dataType.GetMethods().First(x => x.Name == "Add");

            Type jsonDeserializerType = null;
            LazyJsonDeserializerBase jsonDeserializerKey   = null;
            LazyJsonDeserializerBase jsonDeserializerValue = null;

            // Keys deserializer
            jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[0]);

            if (jsonDeserializerType == null)
            {
                jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[0]);
            }

            if (jsonDeserializerType != null)
            {
                jsonDeserializerKey = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType);
            }

            // Values deserializer
            jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[1]);

            if (jsonDeserializerType == null)
            {
                jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[1]);
            }

            if (jsonDeserializerType != null)
            {
                jsonDeserializerValue = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType);
            }

            for (int index = 0; index < jsonArray.Count; index++)
            {
                if (jsonArray.TokenList[index].Type != LazyJsonType.Array)
                {
                    continue;
                }

                LazyJsonArray jsonArrayKeyValuePair = (LazyJsonArray)jsonArray.TokenList[index];

                if (jsonArrayKeyValuePair.TokenList.Count != 2)
                {
                    continue;
                }

                Object key   = null;
                Object value = null;

                if (jsonDeserializerKey != null)
                {
                    key = jsonDeserializerKey.Deserialize(jsonArrayKeyValuePair.TokenList[0], dataType.GenericTypeArguments[0]);
                }
                else
                {
                    key = LazyJsonDeserializer.DeserializeToken(jsonArrayKeyValuePair.TokenList[0], dataType.GenericTypeArguments[0]);
                }

                if (jsonDeserializerValue != null)
                {
                    value = jsonDeserializerValue.Deserialize(jsonArrayKeyValuePair.TokenList[1], dataType.GenericTypeArguments[1]);
                }
                else
                {
                    value = LazyJsonDeserializer.DeserializeToken(jsonArrayKeyValuePair.TokenList[1], dataType.GenericTypeArguments[1]);
                }

                methodAdd.Invoke(dictionary, new Object[] { key, value });
            }

            return(dictionary);
        }