/// <summary> /// 枚举值解析 /// </summary> /// <param name="xmlDeSerializer">XML 反序列化</param> /// <param name="value">目标数据</param> /// <param name="index">第一个枚举索引</param> /// <param name="nextIndex">第二个枚举索引</param> protected static void getIndex(XmlDeSerializer xmlDeSerializer, ref T value, out int index, ref int nextIndex) { if ((index = enumSearcher.SearchFlagEnum(xmlDeSerializer)) == -1) { if (xmlDeSerializer.Config.IsMatchEnum) { xmlDeSerializer.State = DeSerializeState.NoFoundEnumValue; return; } else { do { if (xmlDeSerializer.IsNextFlagEnum() == 0) { return; } }while ((index = enumSearcher.SearchFlagEnum(xmlDeSerializer)) == -1); } } do { if (xmlDeSerializer.IsNextFlagEnum() == 0) { value = enumValues[index]; return; } if ((nextIndex = enumSearcher.SearchFlagEnum(xmlDeSerializer)) != -1) { break; } }while (true); }
private static void unbox(XmlDeSerializer deSerializer, ref T value) { if (deSerializer.IsValue() != 0) { memberDeSerializers[0].TryDeSerialize(deSerializer, ref value); } }
public void VerifyThatSerializeDeSerializeWorks() { var obj = CsvParser.ParseProject("testdata"); if (!Directory.Exists(Path.GetDirectoryName(pathToXml))) { Directory.CreateDirectory(Path.GetDirectoryName(pathToXml)); } XmlDeSerializer.WriteXml(obj, pathToXml); Assert.IsTrue(File.Exists(pathToXml)); var obj2 = XmlDeSerializer.LoadXml(pathToXml); Assert.IsNotNull(obj2); Assert.AreEqual(2, obj2.Packages.Count); Assert.IsTrue(pathToXml.Equals(obj2.FileName)); Assert.AreEqual("testdata (testoutput\\Stringtable.xml)", obj2.NodeName); XmlDeSerializer.WriteXml(obj2, pathToSaveXml); Assert.IsTrue(obj2.FileName.Equals(pathToSaveXml)); }
/// <summary> /// Quick save a single project /// </summary> /// <param name="prj">The project to save</param> private static void QuickSaveProject(Project prj) { if (!string.IsNullOrWhiteSpace(prj.FileName)) { XmlDeSerializer.WriteXml(prj, prj.FileName); } }
internal static void DeSerialize(XmlDeSerializer xmlDeSerializer, ref T value) { if (!tryDeSerializeNumber(xmlDeSerializer, ref value)) { deSerialize(xmlDeSerializer, ref value); } }
/// <summary> /// 枚举值解析 /// </summary> /// <param name="xmlDeSerializer">XML 反序列化</param> /// <param name="value">目标数据</param> internal static void DeSerializeFlags(XmlDeSerializer xmlDeSerializer, ref T value) { if (!tryDeSerializeNumber(xmlDeSerializer, ref value)) { if (enumSearcher.State == null) { if (xmlDeSerializer.Config.IsMatchEnum) { xmlDeSerializer.State = DeSerializeState.NoFoundEnumValue; } else { xmlDeSerializer.IgnoreSearchValue(); } } else { int index, nextIndex = -1; getIndex(xmlDeSerializer, ref value, out index, ref nextIndex); if (nextIndex != -1) { ulong intValue = enumInts.ULong[index]; intValue |= enumInts.ULong[nextIndex]; while (xmlDeSerializer.IsNextFlagEnum() != 0) { if ((index = enumSearcher.NextFlagEnum(xmlDeSerializer)) != -1) { intValue |= enumInts.ULong[index]; } } value = AutoCSer.Metadata.EnumGenericType <T, ulong> .FromInt(intValue); } } } }
/// <summary> /// 根据枚举字符串查找目标索引 /// </summary> /// <param name="xmlDeSerializer">XML解析器</param> /// <returns>目标索引,null返回-1</returns> private int searchCDataFlagEnum(XmlDeSerializer xmlDeSerializer) { byte *currentState = State; do { char *prefix = (char *)(currentState + *(int *)currentState); if (*prefix != 0) { if (xmlDeSerializer.NextCDataEnumChar() != *prefix) { return(-1); } while (*++prefix != 0) { if (xmlDeSerializer.NextCDataEnumChar() != *prefix) { return(-1); } } } char value = xmlDeSerializer.NextCDataEnumChar(); if (value == 0 || value == ',') { return(xmlDeSerializer.State == DeSerializeState.Success ? *(int *)(currentState + sizeof(int) * 2) : -1); } if (*(int *)(currentState + sizeof(int)) == 0) { return(-1); } int index = value < 128 ? (int)*(ushort *)(charsAscii + (value << 1)) : getCharIndex(value); byte *table = currentState + *(int *)(currentState + sizeof(int)); if (tableType == 0) { if ((index = *(table + index)) == 0) { return(-1); } currentState = State + index * 3 * sizeof(int); } else if (tableType == 1) { if ((index = (int)*(ushort *)(table + index * sizeof(ushort))) == 0) { return(-1); } currentState = State + index * 3 * sizeof(int); } else { if ((index = *(int *)(table + index * sizeof(int))) == 0) { return(-1); } currentState = State + index; } }while (true); }
internal int SearchFlagEnum(XmlDeSerializer xmlDeSerializer) { if (xmlDeSerializer.IsCData == 0) { return(searchFlagEnum(xmlDeSerializer)); } xmlDeSerializer.SearchCDataValue(); return(xmlDeSerializer.State == DeSerializeState.Success ? searchCDataFlagEnum(xmlDeSerializer) : -1); }
internal static void Array(XmlDeSerializer deSerializer, ref T[] values) { int count = ArrayIndex(deSerializer, ref values); if (count != -1 && count != values.Length) { System.Array.Resize(ref values, count); } }
public void VerifyThatLoadingFromFoldersWorks() { var projects = XmlDeSerializer.LoadXmlFolder("xmltestdata"); Assert.IsNotNull(projects); Assert.AreEqual(3, projects.Count); Assert.IsTrue(projects[0].Name.Equals("testdata")); Assert.IsTrue(projects[1].Name.Equals("testdata2")); Assert.IsTrue(projects[2].Name.Equals("testdata3")); }
internal static void DeSerializeClass(XmlDeSerializer deSerializer, ref T value) { if (DefaultDeSerializer == null) { deSerializeClass(deSerializer, ref value); } else { DefaultDeSerializer(deSerializer, ref value); } }
/// <summary> /// Execute the open folder command /// </summary> private void OpenFolderCommandExecute() { var dlg = new CommonOpenFileDialog { IsFolderPicker = true }; if (dlg.ShowDialog() == CommonFileDialogResult.Ok && Directory.Exists(dlg.FileName)) { this.Projects = XmlDeSerializer.LoadXmlFolder(dlg.FileName); } }
internal static void DeSerializeStruct(XmlDeSerializer deSerializer, ref T value) { if (DefaultDeSerializer == null) { DeSerializeMembers(deSerializer, ref value); } else { DefaultDeSerializer(deSerializer, ref value); } }
public void TestDeserialize() { // ARRANGE var testString = @"<?xml version='1.0' encoding='utf-16'?><TrackingConfig xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><Date>1977-08-12T00:00:00</Date><SearchObjectSizePixels>0</SearchObjectSizePixels><HueMin>189</HueMin><HueMax>278</HueMax><SaturationMin>0.321</SaturationMin><SaturationMax>0.88</SaturationMax><LuminanceMin>0.2</LuminanceMin><LuminanceMax>0.6</LuminanceMax></TrackingConfig>"; // ACT var trackingConfig = XmlDeSerializer.Deserialize <TrackingConfig>(testString); // ASSERT Assert.IsTrue(trackingConfig.LuminanceMax == 0.6f); }
public int Call(XmlDeSerializer deSerializer, ref T value) { //deSerializer.ItemName = ItemName; //if ((deSerializer.Config.MemberFilter & MemberFilter) == MemberFilter) //{ // TryDeSerialize(deSerializer, ref value); // return deSerializer.State == DeSerializeState.Success ? 1 : 0; //} //return deSerializer.IgnoreValue(); deSerializer.ItemName = ItemName; TryDeSerialize(deSerializer, ref value); return(deSerializer.State == DeSerializeState.Success ? 1 : 0); }
/// <summary> /// 枚举值解析 /// </summary> /// <param name="xmlDeSerializer">XML 反序列化</param> /// <param name="value">目标数据</param> protected static void deSerialize(XmlDeSerializer xmlDeSerializer, ref T value) { int index = enumSearcher.SearchEnum(xmlDeSerializer); if (index != -1) { value = enumValues[index]; } else if (xmlDeSerializer.Config.IsMatchEnum) { xmlDeSerializer.State = DeSerializeState.NoFoundEnumValue; } }
/// <summary> /// 数值解析 /// </summary> /// <param name="xmlDeSerializer"></param> /// <param name="value"></param> /// <returns></returns> private static bool tryDeSerializeNumber(XmlDeSerializer xmlDeSerializer, ref T value) { if (xmlDeSerializer.IsEnumNumberUnsigned()) { ulong intValue = 0; xmlDeSerializer.DeSerializeNumber(ref intValue); value = AutoCSer.Metadata.EnumGenericType <T, ulong> .FromInt(intValue); } else if (xmlDeSerializer.State == DeSerializeState.Success) { return(false); } return(true); }
/// <summary> /// 获取成员转换函数信息 /// </summary> /// <param name="type">成员类型</param> /// <param name="isCustom"></param> /// <returns>成员转换函数信息</returns> internal static MethodInfo GetMemberMethodInfo(Type type, ref bool isCustom) { MethodInfo methodInfo = XmlDeSerializer.GetDeSerializeMethod(type); if (methodInfo != null) { return(methodInfo); } if (type.IsArray) { return(GenericType.Get(type.GetElementType()).XmlDeSerializeArrayMethod.Method); } if (type.IsEnum) { if (type.IsDefined(typeof(FlagsAttribute), false)) { return((EnumGenericType.Get(type).XmlDeSerializeEnumFlagsDelegate).Method); } return((EnumGenericType.Get(type).XmlDeSerializeEnumDelegate).Method); } if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); if (genericType == typeof(Nullable <>)) { Type[] parameterTypes = type.GetGenericArguments(); return(parameterTypes[0].IsEnum ? StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableEnumMethod.Method : StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableMethod.Method); } if (genericType == typeof(KeyValuePair <,>)) { return(GenericType2.Get(type.GetGenericArguments()).XmlDeSerializeKeyValuePairMethod.Method); } } if ((methodInfo = GetCustom(type)) != null) { isCustom = type.IsValueType; return(methodInfo); } if ((methodInfo = GetIEnumerableConstructor(type)) != null) { return(methodInfo); } if (type.IsValueType) { return(GetValueType(type)); } //return GetType(type); return(GenericType.Get(type).XmlDeSerializeTypeMethod); }
/// <summary> /// Execute the open command /// </summary> private void OpenCommandExecute() { // clear previous var collection = new ObservableCollection <Project>(); var dlg = new CommonOpenFileDialog(); dlg.Filters.Add(new CommonFileDialogFilter("XML file", "*.xml")); dlg.DefaultFileName = "Stringtable.xml"; if (dlg.ShowDialog() == CommonFileDialogResult.Ok && File.Exists(dlg.FileName)) { collection.Add(XmlDeSerializer.LoadXml(dlg.FileName)); } this.Projects = collection; }
/// <summary> /// Execute the save as command /// </summary> private void SaveAsCommandExecute() { if (this.Projects.Count != 1) { return; } var prjct = this.Projects[0]; var dlg = new CommonSaveFileDialog { DefaultFileName = "Stringtable.xml" }; if (dlg.ShowDialog() == CommonFileDialogResult.Ok) { XmlDeSerializer.WriteXml(prjct, dlg.FileName); } }
/// <summary> /// 引用类型对象解析 /// </summary> /// <param name="deSerializer">XML解析器</param> /// <param name="value">目标数据</param> internal static void DeSerialize(XmlDeSerializer deSerializer, ref T value) { if (DefaultDeSerializer == null) { if (isValueType) { DeSerializeMembers(deSerializer, ref value); } else { deSerializeClass(deSerializer, ref value); } } else { DefaultDeSerializer(deSerializer, ref value); } }
public void TestSerialize() { // ARRANGE var trackingConfig = new TrackingConfig(); trackingConfig.Date = DateTime.Parse("1977-08-12"); trackingConfig.HueMax = 278; trackingConfig.HueMin = 189; trackingConfig.LuminanceMax = 0.6f; trackingConfig.LuminanceMin = 0.2f; trackingConfig.SaturationMax = 0.88f; trackingConfig.SaturationMin = 0.321f; // ACT var serializedTrackingConfig = XmlDeSerializer.Serialize(trackingConfig); // ASSERT // TODO!! }
/// <summary> /// Execute the ok command /// </summary> private async void OkExecute() { // ask if the user wants to proceed if the selected source modifier is not nothing if (this.SelectedSourceAction != SourceAction.Nothing) { var dlg = MessageBox.Show( "You chose to modify the source. This is a unreversible action. Do you want to proceed?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); if (dlg != MessageBoxResult.Yes) { return; } } this.isBusy = true; this.IsProgressVisible = Visibility.Visible; Mouse.OverrideCursor = Cursors.Wait; var prjct = await Task.Run(() => CsvParser.ParseProject(this.SourcePath, this.SelectedSourceAction, this.FillMissing)); this.isBusy = false; this.IsProgressVisible = Visibility.Hidden; Mouse.OverrideCursor = null; // check for override if (File.Exists(this.DestinationPath)) { var dlg = MessageBox.Show( "The selected destination file already exists. Do you want to proceed?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); if (dlg != MessageBoxResult.Yes) { return; } } XmlDeSerializer.WriteXml(prjct, this.DestinationPath); }
public int Call(XmlDeSerializer deSerializer, MemberMap memberMap, ref T value) { //deSerializer.ItemName = ItemName; //if ((deSerializer.Config.MemberFilter & MemberFilter) == MemberFilter) //{ // TryDeSerialize(deSerializer, ref value); // if (deSerializer.State == DeSerializeState.Success) // { // memberMap.SetMember(MemberMapIndex); // return 1; // } // return 0; //} //return deSerializer.IgnoreValue(); deSerializer.ItemName = ItemName; TryDeSerialize(deSerializer, ref value); if (deSerializer.State == DeSerializeState.Success) { memberMap.SetMember(MemberMapIndex); return(1); } return(0); }
internal static void deSerializeClass(XmlDeSerializer deSerializer, ref T value) { if (value == null) { if (AutoCSer.Metadata.DefaultConstructor <T> .Type == DefaultConstructorType.None) { deSerializer.CheckNoConstructor(ref value, isAnonymousType); if (value == null) { return; } } else { value = AutoCSer.Metadata.DefaultConstructor <T> .Constructor(); } } else if (isAnonymousType) { deSerializer.SetAnonymousType(value); } DeSerializeMembers(deSerializer, ref value); }
/// <summary> /// 根据字符串查找目标索引 /// </summary> /// <param name="xmlDeSerializer">XML解析器</param> /// <returns>目标索引,null返回-1</returns> internal int SearchEnum(XmlDeSerializer xmlDeSerializer) { if (State != null) { if (xmlDeSerializer.IsCData == 0) { int index = searchEnumOnly(xmlDeSerializer); if (xmlDeSerializer.State == DeSerializeState.Success) { xmlDeSerializer.SearchValueEnd(); return(index); } } else { xmlDeSerializer.SearchCDataValue(); if (xmlDeSerializer.State == DeSerializeState.Success) { return(searchCDataEnum(xmlDeSerializer)); } } } return(-1); }
static TypeDeSerializer() { Type type = typeof(T); MethodInfo methodInfo = XmlDeSerializer.GetDeSerializeMethod(type); if (methodInfo != null) { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo); return; } if (type.IsArray) { if (type.GetArrayRank() == 1) { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)GenericType.Get(type.GetElementType()).XmlDeSerializeArrayMethod; } else { DefaultDeSerializer = notSupport; } return; } if (type.IsEnum) { if (type.IsDefined(typeof(FlagsAttribute), false)) { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).XmlDeSerializeEnumFlagsDelegate; } else { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)EnumGenericType.Get(type).XmlDeSerializeEnumDelegate; } return; } if (type.IsInterface || type.IsPointer || typeof(Delegate).IsAssignableFrom(type)) { DefaultDeSerializer = notSupport; return; } if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); if (genericType == typeof(Nullable <>)) { Type[] parameterTypes = type.GetGenericArguments(); DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)(parameterTypes[0].IsEnum ? StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableEnumMethod : StructGenericType.Get(parameterTypes[0]).XmlDeSerializeNullableMethod); return; } if (genericType == typeof(KeyValuePair <,>)) { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)GenericType2.Get(type.GetGenericArguments()).XmlDeSerializeKeyValuePairMethod; isValueType = true; return; } } if ((methodInfo = DeSerializeMethodCache.GetCustom(type)) != null) { if (type.IsValueType) { #if NOJIT DefaultDeSerializer = new CustomDeSerializer(methodInfo).DeSerialize; #else DynamicMethod dynamicMethod = new DynamicMethod("CustomXmlDeSerializer", null, new Type[] { typeof(XmlDeSerializer), type.MakeByRefType() }, type, true); ILGenerator generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldarg_0); generator.call(methodInfo); generator.Emit(OpCodes.Ret); DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)dynamicMethod.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>)); #endif } else { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo); } } else { Type attributeType; attribute = type.customAttribute <XmlSerializeAttribute>(out attributeType) ?? XmlSerializer.AllMemberAttribute; if ((methodInfo = DeSerializeMethodCache.GetIEnumerableConstructor(type)) != null) { DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo); } else { if (type.IsValueType) { isValueType = true; } else if (attribute != XmlSerializer.AllMemberAttribute && attributeType != type) { for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType) { XmlSerializeAttribute baseAttribute = baseType.customAttribute <XmlSerializeAttribute>(); if (baseAttribute != null) { if (baseAttribute.IsBaseType) { methodInfo = DeSerializeMethodCache.BaseDeSerializeMethod.MakeGenericMethod(baseType, type); DefaultDeSerializer = (XmlDeSerializer.DeSerializeDelegate <T>)Delegate.CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>), methodInfo); return; } break; } } } if (type.IsValueType) { foreach (AutoCSer.Metadata.AttributeMethod attributeMethod in AutoCSer.Metadata.AttributeMethod.GetStatic(type)) { if (attributeMethod.Method.ReturnType == typeof(bool)) { ParameterInfo[] parameters = attributeMethod.Method.GetParameters(); if (parameters.Length == 2 && parameters[0].ParameterType == typeof(XmlDeSerializer) && parameters[1].ParameterType == Emit.Pub.PointerSizeRefType) { if (attributeMethod.GetAttribute <UnknownNameAttribute>() != null) { #if NOJIT onUnknownName = new UnknownDeSerializer(methodInfo).DeSerialize; #else DynamicMethod dynamicMethod = new DynamicMethod("XmlUnknownDeSerialize", null, new Type[] { typeof(XmlDeSerializer), type.MakeByRefType(), Emit.Pub.PointerSizeRefType }, type, true); ILGenerator generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_2); generator.call(methodInfo); generator.Emit(OpCodes.Ret); onUnknownName = (UnknownDeSerialize)dynamicMethod.CreateDelegate(typeof(UnknownDeSerialize)); #endif break; } } } } } else { Type refType = type.MakeByRefType(); foreach (AutoCSer.Metadata.AttributeMethod attributeMethod in AutoCSer.Metadata.AttributeMethod.GetStatic(type)) { if (attributeMethod.Method.ReturnType == typeof(bool)) { ParameterInfo[] parameters = attributeMethod.Method.GetParameters(); if (parameters.Length == 3 && parameters[0].ParameterType == typeof(XmlDeSerializer) && parameters[1].ParameterType == refType && parameters[2].ParameterType == Emit.Pub.PointerSizeRefType) { if (attributeMethod.GetAttribute <UnknownNameAttribute>() != null) { onUnknownName = (UnknownDeSerialize)Delegate.CreateDelegate(typeof(UnknownDeSerialize), attributeMethod.Method); break; } } } } } FieldIndex defaultMember = null; LeftArray <KeyValue <FieldIndex, XmlSerializeMemberAttribute> > fields = SerializeMethodCache.GetFields(MemberIndexGroup <T> .GetFields(attribute.MemberFilters), attribute); LeftArray <PropertyMethod> properties = DeSerializeMethodCache.GetProperties(MemberIndexGroup <T> .GetProperties(attribute.MemberFilters), attribute); bool isBox = false; if (type.IsValueType && fields.Length + properties.Length == 1) { BoxSerializeAttribute boxSerialize = AutoCSer.Metadata.TypeAttribute.GetAttribute <BoxSerializeAttribute>(type); if (boxSerialize != null && boxSerialize.IsXml) { isBox = true; defaultMember = null; } } TryDeSerializeFilter[] deSerializers = new TryDeSerializeFilter[fields.Length + properties.Length + (defaultMember == null ? 0 : 1)]; //memberMap.type memberMapType = memberMap<valueType>.TypeInfo; string[] names = isBox ? null : new string[deSerializers.Length]; int index = 0, nameLength = 0, maxNameLength = 0; foreach (KeyValue <FieldIndex, XmlSerializeMemberAttribute> member in fields) { TryDeSerializeFilter tryDeSerialize = deSerializers[index] = new TryDeSerializeFilter { #if NOJIT TryDeSerialize = new FieldDeSerializer(member.Key.Member).DeSerializer(), #else TryDeSerialize = (XmlDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Key.Member).CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>)), #endif ItemName = member.Value == null ? null : member.Value.ItemName, MemberMapIndex = member.Key.MemberIndex, //MemberFilter = member.Key.Member.IsPublic ? MemberFilters.PublicInstanceField : MemberFilters.NonPublicInstanceField }; if (!isBox) { string name = member.Key.AnonymousName; if (name.Length > maxNameLength) { maxNameLength = name.Length; } nameLength += (names[index++] = name).Length; if (member.Key == defaultMember) { deSerializers[deSerializers.Length - 1] = tryDeSerialize; names[deSerializers.Length - 1] = string.Empty; } } } foreach (PropertyMethod member in properties) { deSerializers[index] = new TryDeSerializeFilter { #if NOJIT TryDeSerialize = new PropertyDeSerializer(member.Property.Member).DeSerializer(), #else TryDeSerialize = (XmlDeSerializer.DeSerializeDelegate <T>)DeSerializeMethodCache.CreateDynamicMethod(type, member.Property.Member, member.Method).CreateDelegate(typeof(XmlDeSerializer.DeSerializeDelegate <T>)), #endif ItemName = member.Attribute == null ? null : member.Attribute.ItemName, MemberMapIndex = member.Property.MemberIndex, //MemberFilter = member.Method.IsPublic ? MemberFilters.PublicInstanceProperty : MemberFilters.NonPublicInstanceProperty }; if (!isBox) { if (member.Property.Member.Name.Length > maxNameLength) { maxNameLength = member.Property.Member.Name.Length; } nameLength += (names[index++] = member.Property.Member.Name).Length; } } memberDeSerializers = deSerializers; if (isBox) { DefaultDeSerializer = unbox; } else { if (type.Name[0] == '<') { isAnonymousType = true; } if (maxNameLength > (short.MaxValue >> 1) - 2 || nameLength == 0) { memberNames = Unmanaged.NullByte8; } else { memberNames = Unmanaged.GetStaticPointer((nameLength + (names.Length - (defaultMember == null ? 0 : 1)) * 3 + 1) << 1, false); byte *write = memberNames.Byte; foreach (string name in names) { if (name.Length != 0) { *(short *)write = (short)((name.Length + 2) * sizeof(char)); *(char *)(write + sizeof(short)) = '<'; fixed(char *nameFixed = name) AutoCSer.Extensions.StringExtension.SimpleCopyNotNull(nameFixed, (char *)(write + (sizeof(short) + sizeof(char))), name.Length); *(char *)(write += (sizeof(short) + sizeof(char)) + (name.Length << 1)) = '>'; write += sizeof(char); } } *(short *)write = 0; } if (type.IsGenericType) { memberSearcher = DeSerializeMethodCache.GetGenericDefinitionMemberSearcher(type, names); } else { memberSearcher = AutoCSer.StateSearcher.CharBuilder.Create(names, true); } } } } }
private static void notSupport(XmlDeSerializer deSerializer, ref T value) { deSerializer.State = DeSerializeState.NotSupport; }
/// <summary> /// 数据成员解析 /// </summary> /// <param name="deSerializer">XML解析器</param> /// <param name="value">目标数据</param> internal static void DeSerializeMembers(XmlDeSerializer deSerializer, ref T value) { byte * names = memberNames.Byte; DeSerializeConfig config = deSerializer.Config; MemberMap memberMap = deSerializer.MemberMap; int index = 0; if (memberMap == null) { while (deSerializer.IsName(names, ref index)) { if (index == -1) { return; } memberDeSerializers[index].Call(deSerializer, ref value); if (deSerializer.State != DeSerializeState.Success) { return; } if (deSerializer.IsNameEnd(names) == 0) { if (deSerializer.CheckNameEnd((char *)(names + (sizeof(short) + sizeof(char))), (*(short *)names >> 1) - 2) == 0) { return; } break; } ++index; names += *(short *)names + sizeof(short); } AutoCSer.StateSearcher.CharSearcher searcher = new AutoCSer.StateSearcher.CharSearcher(memberSearcher); AutoCSer.Memory.Pointer name = default(AutoCSer.Memory.Pointer); byte isTagEnd = 0; if (onUnknownName == null) { do { if ((name.Data = deSerializer.GetName(ref name.ByteSize, ref isTagEnd)) == null) { return; } if (isTagEnd == 0) { if ((index = searcher.UnsafeSearch(name.Char, name.ByteSize)) == -1) { if (deSerializer.IgnoreValue() == 0) { return; } } else if (memberDeSerializers[index].Call(deSerializer, ref value) == 0) { return; } if (deSerializer.CheckNameEnd(name.Char, name.ByteSize) == 0) { return; } } }while (true); } else { do { if ((name.Data = deSerializer.GetName(ref name.ByteSize, ref isTagEnd)) == null) { return; } if (isTagEnd == 0) { if ((index = searcher.UnsafeSearch(name.Char, name.ByteSize)) == -1) { name.ByteSize <<= 1; if (onUnknownName(deSerializer, ref value, ref name)) { if (deSerializer.State != DeSerializeState.Success) { return; } } else { if (deSerializer.State == DeSerializeState.Success) { deSerializer.State = DeSerializeState.UnknownNameError; } return; } } else if (memberDeSerializers[index].Call(deSerializer, ref value) == 0) { return; } if (deSerializer.CheckNameEnd(name.Char, name.ByteSize) == 0) { return; } } }while (true); } } else if (object.ReferenceEquals(memberMap.Type, MemberMap <T> .MemberMapType)) { memberMap.Empty(); deSerializer.MemberMap = null; while (deSerializer.IsName(names, ref index)) { if (index == -1) { return; } memberDeSerializers[index].Call(deSerializer, memberMap, ref value); if (deSerializer.State != DeSerializeState.Success) { return; } if (deSerializer.IsNameEnd(names) == 0) { if (deSerializer.CheckNameEnd((char *)(names + (sizeof(short) + sizeof(char))), (*(short *)names >> 1) - 2) == 0) { return; } break; } ++index; names += *(short *)names + sizeof(short); } AutoCSer.StateSearcher.CharSearcher searcher = new AutoCSer.StateSearcher.CharSearcher(memberSearcher); AutoCSer.Memory.Pointer name = default(AutoCSer.Memory.Pointer); byte isTagEnd = 0; try { if (onUnknownName == null) { do { if ((name.Data = deSerializer.GetName(ref name.ByteSize, ref isTagEnd)) == null) { return; } if (isTagEnd == 0) { if ((index = searcher.UnsafeSearch(name.Char, name.ByteSize)) == -1) { if (deSerializer.IgnoreValue() == 0) { return; } } else if (memberDeSerializers[index].Call(deSerializer, memberMap, ref value) == 0) { return; } if (deSerializer.CheckNameEnd(name.Char, name.ByteSize) == 0) { return; } } }while (true); } else { do { if ((name.Data = deSerializer.GetName(ref name.ByteSize, ref isTagEnd)) == null) { return; } if (isTagEnd == 0) { if ((index = searcher.UnsafeSearch(name.Char, name.ByteSize)) == -1) { name.ByteSize <<= 1; if (onUnknownName(deSerializer, ref value, ref name)) { if (deSerializer.State != DeSerializeState.Success) { return; } } else { if (deSerializer.State == DeSerializeState.Success) { deSerializer.State = DeSerializeState.UnknownNameError; } return; } } else if (memberDeSerializers[index].Call(deSerializer, memberMap, ref value) == 0) { return; } if (deSerializer.CheckNameEnd(name.Char, name.ByteSize) == 0) { return; } } }while (true); } } finally { deSerializer.MemberMap = memberMap; } } else { deSerializer.State = DeSerializeState.MemberMap; } }
/// <summary> /// 数组解析 /// </summary> /// <param name="deSerializer">XML解析器</param> /// <param name="values">目标数据</param> /// <returns>数据数量,-1表示失败</returns> internal static int ArrayIndex(XmlDeSerializer deSerializer, ref T[] values) { if (values == null) { values = EmptyArray <T> .Array; } string arrayItemName = deSerializer.ArrayItemName; AutoCSer.Memory.Pointer name = default(AutoCSer.Memory.Pointer); int index = 0; byte isTagEnd = 0; fixed(char *itemFixed = arrayItemName) { do { if ((name.Data = deSerializer.GetName(ref name.ByteSize, ref isTagEnd)) == null) { break; } if (isTagEnd == 0) { if (arrayItemName.Length != name.ByteSize || !AutoCSer.Memory.Common.SimpleEqualNotNull((byte *)itemFixed, name.Byte, name.ByteSize << 1)) { deSerializer.State = DeSerializeState.NotArrayItem; return(-1); } if (index == values.Length) { T value = default(T); if (deSerializer.IsArrayItem(itemFixed, arrayItemName.Length) != 0) { DeSerialize(deSerializer, ref value); if (deSerializer.State != DeSerializeState.Success) { return(-1); } if (deSerializer.CheckNameEnd(itemFixed, name.ByteSize) == 0) { break; } } values = values.copyNew(index == 0 ? deSerializer.Config.NewArraySize : (index << 1)); values[index++] = value; } else { if (deSerializer.IsArrayItem(itemFixed, arrayItemName.Length) != 0) { DeSerialize(deSerializer, ref values[index]); if (deSerializer.State != DeSerializeState.Success) { return(-1); } if (deSerializer.CheckNameEnd(itemFixed, name.ByteSize) == 0) { break; } } ++index; } } else { if (index == values.Length) { values = values.copyNew(index == 0 ? deSerializer.Config.NewArraySize : (index << 1)); } values[index++] = default(T); } }while (true); } return(deSerializer.State == DeSerializeState.Success ? index : -1); }