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 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 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); }