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); }
private static object DeserializeChild(Tag childTag, System.Type type, object existingInstance, bool simpleType) { string typeName = childTag["type"] as string; if (typeName != null) { if (SdlSerializer.Compiling) { typeName = typeName.Replace(", FezEngine", ", FezContentPipeline"); } type = System.Type.GetType(typeName); simpleType = SdlSerializer.IsCoercible(type); } object existingInstance1 = existingInstance; if (SdlSerializer.IsNull(childTag)) { existingInstance1 = (object)null; } else if (simpleType) { existingInstance1 = SdlSerializer.DeCoerce(childTag.Value, type); } else if (type.Name == "Color") { existingInstance1 = (object)(childTag.Values.Count == 4 ? new Color((int)(byte)(int)childTag.Values[0], (int)(byte)(int)childTag.Values[1], (int)(byte)(int)childTag.Values[2], (int)(byte)(int)childTag.Values[3]) : new Color((int)(byte)(int)childTag.Values[0], (int)(byte)(int)childTag.Values[1], (int)(byte)(int)childTag.Values[2])); } else if (type.Name == "Vector2") { existingInstance1 = (object)new Vector2((float)childTag.Values[0], (float)childTag.Values[1]); } else if (type.Name == "Vector3") { existingInstance1 = (object)new Vector3(Convert.ToSingle(childTag.Values[0]), Convert.ToSingle(childTag.Values[1]), Convert.ToSingle(childTag.Values[2])); } else if (type.Name == "Vector4") { existingInstance1 = (object)new Vector4((float)childTag.Values[0], (float)childTag.Values[1], (float)childTag.Values[2], (float)childTag.Values[3]); } else if (type.Name == "Quaternion") { existingInstance1 = (object)new Quaternion((float)childTag.Values[0], (float)childTag.Values[1], (float)childTag.Values[2], (float)childTag.Values[3]); } else if (type.Name == "Matrix") { existingInstance1 = (object)SdlSerializer.DeserializeMatrix(childTag); } else if (ReflectionHelper.IsGenericDictionary(type)) { SdlSerializer.DeserializeDictionary(childTag, existingInstance1 as IDictionary, type); } else { existingInstance1 = !ReflectionHelper.IsGenericCollection(type) ? SdlSerializer.DeserializeInternal(type, childTag, existingInstance1) : (object)SdlSerializer.DeserializeCollection(childTag, existingInstance1 as IEnumerable, type); } return(existingInstance1); }
public static void Serialize <T>(string filePath, T instance) { System.Type type = typeof(T); Tag tag = new Tag(SdlSerializer.LowerCamelCase(type.Name)); tag["type"] = (object)ReflectionHelper.GetShortAssemblyQualifiedName(type); SdlSerializer.SerializeInternal((object)instance, type, tag); tag.WriteFile(filePath, true); }
private static void SerializeDictionary(Tag tag, IDictionary dictionary, string customItemName) { bool simpleType1 = false; foreach (object index in (IEnumerable)dictionary.Keys) { SdlSerializer.TryCoerce(index, out simpleType1); if (simpleType1) { SdlSerializer.TryCoerce(dictionary[index], out simpleType1); if (!simpleType1) { break; } } else { break; } } if (simpleType1) { foreach (object index1 in (IEnumerable)dictionary.Keys) { bool simpleType2; string index2 = SdlSerializer.TryCoerce(index1, out simpleType2).ToString(); object obj = SdlSerializer.TryCoerce(dictionary[index1], out simpleType2); tag[index2] = obj; } } else { System.Type[] genericArguments = dictionary.GetType().GetGenericArguments(); System.Type type1 = genericArguments[0]; System.Type type2 = genericArguments[1]; foreach (object index in (IEnumerable)dictionary.Keys) { object instance = dictionary[index]; bool simpleType2; object obj = SdlSerializer.TryCoerce(index, out simpleType2); Tag tag1 = new Tag(SdlSerializer.LowerCamelCase(customItemName ?? type2.Name)); tag.AddChild(tag1); if (simpleType2) { tag1["key"] = obj; SdlSerializer.SerializeInternal(instance, type2, tag1); } else { SdlSerializer.SerializeChild("key", index, type1, false, SdlSerializer.DefaultAttribute, tag1); bool simpleType3; SdlSerializer.SerializeChild("value", SdlSerializer.TryCoerce(instance, out simpleType3), type2, simpleType3, SdlSerializer.DefaultAttribute, tag1); } } } }
private static void DeserializeDictionary(Tag tag, IDictionary dictionary, System.Type declaredType) { bool flag = tag.Children.Count == 0; if (dictionary == null) { dictionary = ReflectionHelper.Instantiate(declaredType) as IDictionary; } System.Type[] genericArguments = dictionary.GetType().GetGenericArguments(); System.Type type1 = genericArguments[0]; System.Type type2 = genericArguments[1]; if (flag) { foreach (string index in (IEnumerable <string>)tag.Attributes.Keys) { object key = SdlSerializer.DeCoerce((object)index, type1); object obj = SdlSerializer.DeCoerce(tag[index], type2); SdlSerializer.SafeAddToDictionary(dictionary, key, obj); } } else { foreach (Tag tag1 in (IEnumerable <Tag>)tag.Children) { object coerced = tag1["key"]; Tag child = tag1.GetChild("key"); if (!(coerced != null ^ child != null)) { throw new SdlSerializationException(Resources.IllegalCollectionStructure, dictionary.GetType(), tag.Name); } if (coerced != null) { object key = SdlSerializer.DeCoerce(coerced, type1); object obj = SdlSerializer.DeserializeInternal(type2, tag1, (object)null); SdlSerializer.SafeAddToDictionary(dictionary, key, obj); } else { object key = SdlSerializer.DeserializeInternal(type1, child, (object)null); bool simpleType = SdlSerializer.IsCoercible(type2); bool valueFound; object obj = SdlSerializer.DeserializeChild("value", (object)null, type2, simpleType, SdlSerializer.DefaultAttribute, tag1, out valueFound); SdlSerializer.SafeAddToDictionary(dictionary, key, obj); } } } }
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 SerializeMatrix(Tag tag, Matrix matrix) { Tag tag1 = new Tag("content"); tag.AddChild(tag1); SdlSerializer.AddValueList(tag1, (object)matrix.M11, (object)matrix.M12, (object)matrix.M13, (object)matrix.M14); Tag tag2 = new Tag("content"); tag.AddChild(tag2); SdlSerializer.AddValueList(tag2, (object)matrix.M21, (object)matrix.M22, (object)matrix.M23, (object)matrix.M24); Tag tag3 = new Tag("content"); tag.AddChild(tag3); SdlSerializer.AddValueList(tag3, (object)matrix.M31, (object)matrix.M32, (object)matrix.M33, (object)matrix.M34); Tag tag4 = new Tag("content"); tag.AddChild(tag4); SdlSerializer.AddValueList(tag4, (object)matrix.M41, (object)matrix.M42, (object)matrix.M43, (object)matrix.M44); }
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 void SerializeCollection(Tag tag, IEnumerable collection, string customItemName) { List <object> list = new List <object>(); System.Type type1 = collection.GetType(); System.Type type2 = type1.IsArray ? type1.GetElementType() : type1.GetGenericArguments()[0]; bool simpleType1 = false; foreach (object obj1 in collection) { object obj2 = SdlSerializer.TryCoerce(obj1, out simpleType1); if (simpleType1) { list.Add(obj2); } else { break; } } if (simpleType1) { foreach (object obj in list) { tag.AddValue(obj); } } else { foreach (object obj in collection) { bool simpleType2; SdlSerializer.SerializeChild(SdlSerializer.LowerCamelCase(customItemName ?? type2.Name), SdlSerializer.TryCoerce(obj, out simpleType2), type2, simpleType2, SdlSerializer.DefaultAttribute, tag); } } }
private static IEnumerable DeserializeCollection(Tag tag, IEnumerable collection, System.Type declaredType) { bool flag1 = tag.Values.Count > 0; bool flag2 = tag.Children.Count > 0; if (collection == null) { collection = !declaredType.IsArray ? ReflectionHelper.Instantiate(declaredType) as IEnumerable : (IEnumerable)Array.CreateInstance(declaredType.GetElementType(), 0); } if (!flag1 && !flag2) { return(collection); } System.Type type1 = collection.GetType(); bool flag3 = collection is IList; bool isArray = type1.IsArray; bool flag4 = ReflectionHelper.IsGenericSet(type1); System.Type type2 = isArray ? type1.GetElementType() : type1.GetGenericArguments()[0]; bool simpleType = SdlSerializer.IsCoercible(type2); DynamicMethodDelegate dynamicMethodDelegate = (DynamicMethodDelegate)null; if (flag4) { dynamicMethodDelegate = ReflectionHelper.GetDelegate(type1.GetMethod("Add")); } if (flag1 && flag2) { throw new SdlSerializationException(Resources.IllegalCollectionStructure, type1, tag.Name); } if (isArray) { int length = flag1 ? tag.Values.Count : tag.Children.Count; collection = (IEnumerable)Array.CreateInstance(type2, length); } int num = 0; if (flag1) { foreach (object coerced in (IEnumerable <object>)tag.Values) { object obj1 = SdlSerializer.DeCoerce(coerced, type2); if (isArray) { ((IList)collection)[num++] = obj1; } else if (flag4) { object obj2 = dynamicMethodDelegate((object)collection, new object[1] { obj1 }); } else { if (!flag3) { throw new NotImplementedException(); } ((IList)collection).Add(obj1); } } } else { foreach (Tag tag1 in (IEnumerable <Tag>)tag.Children) { object obj1 = SdlSerializer.IsNull(tag1) ? (object)null : SdlSerializer.DeserializeChild(tag1, type2, (object)null, simpleType); if (isArray) { ((IList)collection)[num++] = obj1; } else if (flag4) { object obj2 = dynamicMethodDelegate((object)collection, new object[1] { obj1 }); } else { if (!flag3) { throw new NotImplementedException(); } ((IList)collection).Add(obj1); } } } return(collection); }
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); }
public static T Deserialize <T>(string filePath) { return((T)SdlSerializer.DeserializeInternal((System.Type)null, new Tag("content").ReadFile(filePath).Children[0], (object)null)); }
public static T Deserialize <T>(StreamReader reader) { return((T)SdlSerializer.DeserializeInternal((System.Type)null, new Tag("content").Read(reader).Children[0], (object)null)); }
private static void AddValueList(Tag tag, params object[] list) { SdlSerializer.AddValueList(tag, (IList)list); }