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); } } } } } }
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); }
private static object DeserializeChild(string name, object existingInstance, System.Type type, bool simpleType, SerializationAttribute attr, Tag tag, out bool valueFound) { Tag child = tag.GetChild(name); if (child == null) { if (!attr.Optional) { throw new SdlSerializationException(Resources.MissingNonOptionalTagOrAttribute, type, name); } valueFound = false; return(existingInstance); } else { valueFound = true; return(SdlSerializer.DeserializeChild(child, type, existingInstance, simpleType)); } }
private static void SerializeChild(string name, object value, System.Type type, bool simpleType, SerializationAttribute attr, Tag parent) { Tag tag = new Tag(name); if ((!simpleType || type == typeof(object)) && (value != null && !ReflectionHelper.IsNullable(type)) && value.GetType() != type) { type = value.GetType(); tag["type"] = (object)ReflectionHelper.GetShortAssemblyQualifiedName(type); } if (simpleType) { tag.AddValue(value); } else if (type == typeof(Color)) { Color color = (Color)value; SdlSerializer.AddValueList(tag, (object)color.R, (object)color.G, (object)color.B); if ((int)color.A != (int)byte.MaxValue) { tag.AddValue((object)color.A); } } else if (type == typeof(Vector2)) { Vector2 vector2 = (Vector2)value; SdlSerializer.AddValueList(tag, (object)vector2.X, (object)vector2.Y); } else if (type == typeof(Vector3)) { Vector3 vector3 = (Vector3)value; SdlSerializer.AddValueList(tag, (object)vector3.X, (object)vector3.Y, (object)vector3.Z); } else if (type == typeof(Vector4)) { Vector4 vector4 = (Vector4)value; SdlSerializer.AddValueList(tag, (object)vector4.X, (object)vector4.Y, (object)vector4.Z, (object)vector4.W); } else if (type == typeof(Quaternion)) { Quaternion quaternion = (Quaternion)value; SdlSerializer.AddValueList(tag, (object)quaternion.X, (object)quaternion.Y, (object)quaternion.Z, (object)quaternion.W); } else if (type == typeof(Matrix)) { SdlSerializer.SerializeMatrix(tag, (Matrix)value); } else if (ReflectionHelper.IsGenericDictionary(type)) { SdlSerializer.SerializeDictionary(tag, value as IDictionary, attr.CollectionItemName); } else if (ReflectionHelper.IsGenericCollection(type)) { SdlSerializer.SerializeCollection(tag, value as IEnumerable, attr.CollectionItemName); } else { SdlSerializer.SerializeInternal(value, type, tag); } parent.AddChild(tag); }