Exemplo n.º 1
0
        T Deserialize<T>(List<DeserializableObject> objects)
        {
            T result;
            Type type = typeof(T);

            if (objects.First().ObjType == DeserializableObject.ObjectType.Primitive
                    && objects.Count == 1
                    && objects.First().Key == "")
                return (T)Convert.ChangeType(objects.First().Value.Trim('\"'), type);
            if (ParserUtils.IsEnumerable(type) && objects.Count > 0)
            {
                objects.First().ObjType = DeserializableObject.ObjectType.Collection;
                return (T)GetEnumerableInstance(objects.First(), type);
            }
            result = (T)Activator.CreateInstance(type);
            foreach (var obj in objects)
            {
                string key = obj.Key;
                string value = obj.Value.Trim('\"');
                Type memberType = ParserUtils.GetMemberType(type, key);
                if (obj.ObjType == DeserializableObject.ObjectType.Primitive)
                {
                    object converted;
                    if (memberType.IsEnum)
                    {
                        converted = Enum.Parse(memberType, value);
                    }
                    else
                    {
                        converted = Convert.ChangeType(value, memberType);
                    }
                    ParserUtils.SetMemberValue(result, key, converted);
                }
                else if (obj.ObjType == DeserializableObject.ObjectType.ComplexObject)
                {

                    if (ParserUtils.IsEnumerable(ParserUtils.GetMemberType(type, key)))
                    {
                        if (ParserUtils.IsEnumerable(memberType))
                            ParserUtils.SetMemberValue(result, key, GetEnumerableInstance(obj, memberType));
                    }
                    else
                    {
                        object parsed = typeof(XmlParser).GetMethod("Deserialize", BindingFlags.NonPublic)
                                                     .MakeGenericMethod(memberType)
                                                     .Invoke(this, new object[] { Parse(value, false) });
                        ParserUtils.SetMemberValue(result, key, parsed);
                    }

                }
                else
                {
                    if (ParserUtils.IsEnumerable(memberType))
                        ParserUtils.SetMemberValue(result, key, GetEnumerableInstance(obj, memberType));
                    else
                        throw new Exception("Invalid deserialization");
                }
            }
            return result;
        }
Exemplo n.º 2
0
        private static T Deserialize <T>(List <DeserializableObject> objects)
        {
            T   result;
            var type = typeof(T);

            if (objects.Count == 1 && objects.First().Key == "")
            {
                if (objects.First().ObjType == DeserializableObject.ObjectType.Primitive)
                {
                    return((T)Convert.ChangeType(objects.First().Value.Trim('\"'), type));
                }
                if (objects.First().ObjType == DeserializableObject.ObjectType.Collection)
                {
                    if (ParserUtils.IsEnumerable(type))
                    {
                        return((T)GetEnumerableInstance(objects.First(), type));
                    }
                }
            }

            result = (T)Activator.CreateInstance(type);
            foreach (var obj in objects)
            {
                var key        = obj.Key.Trim('\"');
                var value      = obj.Value.Trim('\"');
                var memberType = ParserUtils.GetMemberType(type, key);
                if (obj.ObjType == DeserializableObject.ObjectType.Primitive)
                {
                    object converted;
                    if (memberType.IsEnum)
                    {
                        converted = Enum.Parse(memberType, value);
                    }
                    else
                    {
                        converted = Convert.ChangeType(value, memberType);
                    }
                    ParserUtils.SetMemberValue(result, key, converted);
                }
                else if (obj.ObjType == DeserializableObject.ObjectType.ComplexObject)
                {
                    var parsed = typeof(JsonParser)
                                 .GetMethod("Deserialize")
                                 .MakeGenericMethod(memberType)
                                 .Invoke(null, new object[] { Parse(value) });
                    ParserUtils.SetMemberValue(result, key, parsed);
                }
                else
                {
                    if (ParserUtils.IsEnumerable(memberType))
                    {
                        ParserUtils.SetMemberValue(result, key, GetEnumerableInstance(obj, memberType));
                    }
                    else
                    {
                        throw new Exception("Invalid deserialization");
                    }
                }
            }

            return(result);
        }