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