Esempio n. 1
0
        /// <summary>
        /// Find a deserializer for the type defined on class declaration
        /// </summary>
        /// <param name="dataType">The data type to look for</param>
        /// <returns>The type of the deserializer</returns>
        public static Type FindTypeDeserializer(Type dataType)
        {
            LazyJsonAttributeDeserializer jsonAttributeDeserializer = null;

            Object[] jsonAttributeDeserializerArray = dataType.GetCustomAttributes(typeof(LazyJsonAttributeDeserializer), false);

            if (jsonAttributeDeserializerArray.Length > 0)
            {
                jsonAttributeDeserializer = (LazyJsonAttributeDeserializer)jsonAttributeDeserializerArray[0];
            }

            if (jsonAttributeDeserializer != null && jsonAttributeDeserializer.DeserializerType != null && jsonAttributeDeserializer.DeserializerType.IsSubclassOf(typeof(LazyJsonDeserializerBase)) == true)
            {
                return(jsonAttributeDeserializer.DeserializerType);
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Deserialize the json object token to the desired object
        /// </summary>
        /// <param name="jsonObject">The json object token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public static Object DeserializeObject(LazyJsonObject jsonObject, Type dataType)
        {
            if (jsonObject == null || dataType == null)
            {
                return(null);
            }

            Object        data = Activator.CreateInstance(dataType);
            List <String> alreadyDeserializer = new List <String>();

            PropertyInfo[] propertyInfoArray = dataType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo propertyInfo in propertyInfoArray)
            {
                if (propertyInfo.SetMethod == null)
                {
                    continue;
                }

                if (alreadyDeserializer.Contains(propertyInfo.Name) == true)
                {
                    continue;
                }

                if (propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributePropertyIgnore), false).Length > 0)
                {
                    continue;
                }

                alreadyDeserializer.Add(propertyInfo.Name);

                Type     jsonDeserializerType   = null;
                String   propertyName           = propertyInfo.Name;
                Object[] jsonAttributeBaseArray = propertyInfo.GetCustomAttributes(typeof(LazyJsonAttributeBase), false);

                foreach (Object attribute in jsonAttributeBaseArray)
                {
                    if (attribute is LazyJsonAttributePropertyRename)
                    {
                        propertyName = ((LazyJsonAttributePropertyRename)attribute).Name;
                    }
                    else if (attribute is LazyJsonAttributeDeserializer)
                    {
                        LazyJsonAttributeDeserializer jsonAttributeDeserializer = (LazyJsonAttributeDeserializer)attribute;

                        if (jsonAttributeDeserializer.DeserializerType != null && jsonAttributeDeserializer.DeserializerType.IsSubclassOf(typeof(LazyJsonDeserializerBase)) == true)
                        {
                            jsonDeserializerType = jsonAttributeDeserializer.DeserializerType;
                        }
                    }
                }

                if (jsonObject[propertyName] != null)
                {
                    if (jsonDeserializerType != null)
                    {
                        propertyInfo.SetValue(data, ((LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType)).Deserialize(jsonObject[propertyName], propertyInfo.PropertyType));
                    }
                    else
                    {
                        propertyInfo.SetValue(data, DeserializeToken(jsonObject[propertyName].Token, propertyInfo.PropertyType));
                    }
                }
            }

            return(data);
        }