Exemplo n.º 1
0
        private static void SerializeInternal(object instance, System.Type declaredType, Tag tag)
        {
            System.Type type = declaredType;
            TypeSerializationAttribute serializationAttribute = ReflectionHelper.GetFirstAttribute <TypeSerializationAttribute>(type) ?? new TypeSerializationAttribute();

            foreach (MemberInfo memberInfo in ReflectionHelper.GetSerializableMembers(type))
            {
                SerializationAttribute attr = ReflectionHelper.GetFirstAttribute <SerializationAttribute>(memberInfo) ?? new SerializationAttribute();
                if (!attr.Ignore)
                {
                    System.Type memberType = ReflectionHelper.GetMemberType(memberInfo);
                    object      obj1       = ReflectionHelper.GetValue(memberInfo, instance);
                    if (!attr.Optional || serializationAttribute.FlattenToList || obj1 != null && (!attr.DefaultValueOptional || !SdlSerializer.IsDefault(obj1)))
                    {
                        bool   simpleType;
                        object obj2 = SdlSerializer.TryCoerce(obj1, out simpleType);
                        if ((serializationAttribute.FlattenToList || attr.UseAttribute) && !simpleType)
                        {
                            throw new SdlSerializationException(Resources.SimpleTypeRequired, type, memberInfo);
                        }
                        if (serializationAttribute.FlattenToList)
                        {
                            tag.AddValue(obj2);
                        }
                        else
                        {
                            string name = SdlSerializer.LowerCamelCase(attr.Name ?? memberInfo.Name);
                            if (attr.UseAttribute)
                            {
                                tag[name] = obj2;
                            }
                            else
                            {
                                SdlSerializer.SerializeChild(name, obj2, memberType, simpleType, attr, tag);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static object DeserializeInternal(System.Type declaredType, Tag tag, object existingInstance)
        {
            System.Type type     = declaredType;
            object      instance = existingInstance;
            string      typeName = tag["type"] as string;

            if (SdlSerializer.Compiling && typeName == "FezEngine.Structure.TrileSet, FezEngine")
            {
                typeName = "FezContentPipeline.Content.TrileSetContent, FezContentPipeline";
            }
            if (SdlSerializer.Compiling && typeName == "FezEngine.Structure.ArtObject, FezEngine")
            {
                typeName = "FezContentPipeline.Content.ArtObjectContent, FezContentPipeline";
            }
            if (SdlSerializer.Compiling && typeName != null)
            {
                typeName = typeName.Replace(", FezEngine", ", FezContentPipeline");
            }
            if (typeName != null)
            {
                type = System.Type.GetType(typeName);
            }
            if ((instance == null || declaredType != type) && !SdlSerializer.IsCoercible(type))
            {
                instance = ReflectionHelper.Instantiate(type);
            }
            TypeSerializationAttribute serializationAttribute = ReflectionHelper.GetFirstAttribute <TypeSerializationAttribute>(type) ?? new TypeSerializationAttribute();
            int num = 0;

            foreach (MemberInfo memberInfo in ReflectionHelper.GetSerializableMembers(type))
            {
                SerializationAttribute attr = ReflectionHelper.GetFirstAttribute <SerializationAttribute>(memberInfo) ?? new SerializationAttribute();
                if (!attr.Ignore)
                {
                    bool   valueFound = true;
                    object obj        = (object)null;
                    if (serializationAttribute.FlattenToList)
                    {
                        obj = tag[num++];
                    }
                    else
                    {
                        System.Type memberType = ReflectionHelper.GetMemberType(memberInfo);
                        string      index      = SdlSerializer.LowerCamelCase(attr.Name ?? memberInfo.Name);
                        if (attr.UseAttribute)
                        {
                            if (tag.Attributes.ContainsKey(index))
                            {
                                obj = SdlSerializer.DeCoerce(tag[index], memberType);
                            }
                            else
                            {
                                if (!attr.Optional)
                                {
                                    throw new SdlSerializationException(Resources.MissingNonOptionalTagOrAttribute, type, memberInfo);
                                }
                                valueFound = false;
                            }
                        }
                        else
                        {
                            object existingInstance1 = ReflectionHelper.GetValue(memberInfo, instance);
                            bool   simpleType        = SdlSerializer.IsCoercible(memberType);
                            obj = SdlSerializer.DeserializeChild(index, existingInstance1, memberType, simpleType, attr, tag, out valueFound);
                        }
                    }
                    if (valueFound)
                    {
                        ReflectionHelper.SetValue(memberInfo, instance, obj);
                    }
                }
            }
            if (instance is IDeserializationCallback)
            {
                (instance as IDeserializationCallback).OnDeserialization();
            }
            return(instance);
        }