public void ReflectionWriteClass(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract) { _reflectionClassWriter.ReflectionWriteClass(xmlWriter, obj, context, classContract, null /*memberNames*/); }
public override void WriteXmlElement(XmlWriterDelegator writer, object?obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString?ns) { context.WriteQName(writer, (XmlQualifiedName?)obj, name, ns); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object?obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString?ns) { throw new NotImplementedException(); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { // write nothing }
public override void WriteXmlElement(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString ns) { writer.WriteTimeSpan((TimeSpan)obj, name, ns); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteString((string)obj); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteBase64((byte[])obj); }
private bool TryWritePrimitive(Type type, object value, XmlWriterDelegator writer, XmlObjectSerializerWriteContext context) { PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(type); if (primitiveContract == null || primitiveContract.UnderlyingType == Globals.TypeOfObject) { return(false); } writer.WriteAttributeString(Globals.XsiPrefix, "type", null, "x:" + primitiveContract.StableName.Name); primitiveContract.WriteXmlValue(writer, value, context); return(true); }
public override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context) { WriteEnumValue(xmlWriter, obj); }
internal void InternalWriteObjectContent(XmlWriterDelegator writer, object?graph, DataContractResolver?dataContractResolver) { if (MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph))); } DataContract contract = RootContract; Type declaredType = contract.UnderlyingType; Type graphType = (graph == null) ? declaredType : graph.GetType(); if (_serializationSurrogateProvider != null) { graph = SurrogateToDataContractType(_serializationSurrogateProvider, graph, declaredType, ref graphType); } if (dataContractResolver == null) { dataContractResolver = this.DataContractResolver; } if (graph == null) { if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeNull, declaredType))); } WriteNull(writer); } else { if (declaredType == graphType) { if (contract.CanContainReferences) { XmlObjectSerializerWriteContext context = XmlObjectSerializerWriteContext.CreateContext(this, contract, dataContractResolver); context.HandleGraphAtTopLevel(writer, graph, contract); context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle); } else { contract.WriteXmlValue(writer, graph, null); } } else { XmlObjectSerializerWriteContext?context = null; if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeSerializedAsDerivedType, graphType, contract.UnderlyingType))); } contract = GetDataContract(contract, declaredType, graphType); context = XmlObjectSerializerWriteContext.CreateContext(this, RootContract, dataContractResolver); if (contract.CanContainReferences) { context.HandleGraphAtTopLevel(writer, graph, contract); } context.OnHandleIsReference(writer, contract, graph); context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType); } } }
private void WriteSystemExceptionRequiredValues(XmlWriterDelegator writer, object value, XmlObjectSerializerWriteContext context) { Dictionary <string, object> exceptionFields = GetExceptionFieldValues((Exception)value); object val; foreach (string key in exceptionFields.Keys) { if (!exceptionFields.TryGetValue(key, out val)) { continue; } Type fieldType; FieldInfo FieldFind = Globals.TypeOfException.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic); if (FieldFind == null) { val = null; // need to nullify because the private fields that are necessary in Exception have changed. fieldType = typeof(int); // can be any type, it doesn't matter. field type will be used to recover a contract, but the type won't be utilized. } else { fieldType = FieldFind.FieldType; } string fieldDisplayName; if (EssentialExceptionFields.TryGetValue(key, out fieldDisplayName)) { writer.WriteStartElement(fieldDisplayName, ""); } else { writer.WriteStartElement(key, ""); } DataContract fieldDataContract = context.GetDataContract(fieldType); if (val != null && fieldDataContract != null && !TryCheckIfNoCountIDictionary(fieldType, val)) { if (!TryWritePrimitive(fieldType, val, writer, context)) { writer.WriteAttributeString(Globals.XsiPrefix, "type", null, "a:" + fieldDataContract.StableName.Name); writer.WriteXmlnsAttribute("a", new XmlDictionary(1).Add(fieldDataContract.StableName.Namespace)); fieldDataContract.WriteXmlValue(writer, val, context); } } else { writer.WriteAttributeString(Globals.XsiPrefix, "nil", null, "true"); } writer.WriteEndElement(); } }
public void ReflectionWriteValue(XmlWriterDelegator xmlWriter, XmlObjectSerializerWriteContext context, Type type, object?value, bool writeXsiType, PrimitiveDataContract?primitiveContractForParamType) { Type memberType = type; object?memberValue = value; bool originValueIsNullableOfT = (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == Globals.TypeOfNullable); if (memberType.IsValueType && !originValueIsNullableOfT) { Debug.Assert(memberValue != null); PrimitiveDataContract?primitiveContract = primitiveContractForParamType; if (primitiveContract != null && !writeXsiType) { primitiveContract.WriteXmlValue(xmlWriter, memberValue, context); } else { ReflectionInternalSerialize(xmlWriter, context, memberValue, memberValue.GetType().TypeHandle.Equals(memberType.TypeHandle), writeXsiType, memberType); } } else { if (originValueIsNullableOfT) { if (memberValue == null) { memberType = Nullable.GetUnderlyingType(memberType) !; } else { MethodInfo getValue = memberType.GetMethod("get_Value", Type.EmptyTypes) !; memberValue = getValue.Invoke(memberValue, Array.Empty <object>()) !; memberType = memberValue.GetType(); } } if (memberValue == null) { context.WriteNull(xmlWriter, memberType, DataContract.IsTypeSerializable(memberType)); } else { PrimitiveDataContract?primitiveContract = originValueIsNullableOfT ? PrimitiveDataContract.GetPrimitiveDataContract(memberType) : primitiveContractForParamType; if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject && !writeXsiType) { primitiveContract.WriteXmlValue(xmlWriter, memberValue, context); } else { if (memberValue == null && (memberType == Globals.TypeOfObject || (originValueIsNullableOfT && memberType.IsValueType))) { context.WriteNull(xmlWriter, memberType, DataContract.IsTypeSerializable(memberType)); } else { ReflectionInternalSerialize(xmlWriter, context, memberValue !, memberValue !.GetType().TypeHandle.Equals(memberType.TypeHandle), writeXsiType, memberType, originValueIsNullableOfT); } } } } }
protected abstract int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, ClassDataContract derivedMostClassContract, int childElementIndex, XmlDictionaryString[]?memberNames);
public void ReflectionWriteCollection(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, CollectionDataContract collectionDataContract) { XmlDictionaryString ns = collectionDataContract.Namespace; XmlDictionaryString itemName = collectionDataContract.CollectionItemName; if (collectionDataContract.ChildElementNamespace != null) { xmlWriter.WriteNamespaceDecl(collectionDataContract.ChildElementNamespace); } if (collectionDataContract.Kind == CollectionKind.Array) { context.IncrementArrayCount(xmlWriter, (Array)obj); Type itemType = collectionDataContract.ItemType; if (!ReflectionTryWritePrimitiveArray(xmlWriter, obj, collectionDataContract.UnderlyingType, itemType, itemName, ns)) { Array array = (Array)obj; PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(itemType); for (int i = 0; i < array.Length; ++i) { _reflectionClassWriter.ReflectionWriteStartElement(xmlWriter, itemType, ns, ns.Value, itemName.Value, 0); _reflectionClassWriter.ReflectionWriteValue(xmlWriter, context, itemType, array.GetValue(i), false, primitiveContract); _reflectionClassWriter.ReflectionWriteEndElement(xmlWriter); } } } else { collectionDataContract.IncrementCollectionCount(xmlWriter, obj, context); Type enumeratorReturnType; IEnumerator enumerator = collectionDataContract.GetEnumeratorForCollection(obj, out enumeratorReturnType); PrimitiveDataContract primitiveContractForType = PrimitiveDataContract.GetPrimitiveDataContract(enumeratorReturnType); if (primitiveContractForType != null && primitiveContractForType.UnderlyingType != Globals.TypeOfObject) { while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); primitiveContractForType.WriteXmlElement(xmlWriter, current, context, itemName, ns); } } else { bool isDictionary = collectionDataContract.Kind == CollectionKind.Dictionary || collectionDataContract.Kind == CollectionKind.GenericDictionary; while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); _reflectionClassWriter.ReflectionWriteStartElement(xmlWriter, enumeratorReturnType, ns, ns.Value, itemName.Value, 0); if (isDictionary) { collectionDataContract.ItemContract.WriteXmlValue(xmlWriter, current, context); } else { _reflectionClassWriter.ReflectionWriteValue(xmlWriter, context, enumeratorReturnType, current, false, primitiveContractForParamType: null); } _reflectionClassWriter.ReflectionWriteEndElement(xmlWriter); } } } }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteDecimal((decimal)obj); }
public override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context) { xmlWriter.WriteAnyType(obj); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString ns) { xmlWriter.WriteDecimal((decimal)obj, name, ns); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteQName((XmlQualifiedName)obj); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString ns) { context.WriteString(xmlWriter, (string)obj, name, ns); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteSignedByte((sbyte)obj); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString ns) { xmlWriter.WriteStartElement(name, ns); xmlWriter.WriteBase64((byte[])obj); xmlWriter.WriteEndElement(); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { throw new NotImplementedException(); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteTimeSpan((TimeSpan)obj); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteUnsignedLong((ulong)obj); }
public override void WriteXmlElement(XmlWriterDelegator writer, object?obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString?ns) { writer.WriteUri((Uri?)obj, name, ns); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString ns) { xmlWriter.WriteUnsignedLong((ulong)obj, name, ns); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object?obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString?ns) { xmlWriter.WriteUnsignedShort((ushort)obj !, name, ns); }
public override void WriteXmlValue(XmlWriterDelegator writer, object obj, XmlObjectSerializerWriteContext context) { writer.WriteFloat((float)obj); }
public override void WriteXmlElement(XmlWriterDelegator xmlWriter, object?obj, XmlObjectSerializerWriteContext context, XmlDictionaryString name, XmlDictionaryString?ns) { xmlWriter.WriteDouble((double)obj !, name, ns); }
protected override int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, ClassDataContract derivedMostClassContract, int childElementIndex, XmlDictionaryString[] emptyStringArray) { int memberCount = (classContract.BaseContract == null) ? 0 : ReflectionWriteMembers(xmlWriter, obj, context, classContract.BaseContract, derivedMostClassContract, childElementIndex, emptyStringArray); childElementIndex += memberCount; Type classType = classContract.UnadaptedClassType; XmlDictionaryString[] memberNames = classContract.MemberNames; XmlDictionaryString ns = classContract.Namespace; context.IncrementItemCount(classContract.Members.Count); for (int i = 0; i < classContract.Members.Count; i++, memberCount++) { DataMember member = classContract.Members[i]; Type memberType = member.MemberType; if (member.IsGetOnlyCollection) { context.StoreIsGetOnlyCollection(); } bool shouldWriteValue = true; object memberValue = null; if (!member.EmitDefaultValue) { memberValue = ReflectionGetMemberValue(obj, member); object defaultValue = XmlFormatGeneratorStatics.GetDefaultValue(memberType); if ((memberValue == null && defaultValue == null) || (memberValue != null && memberValue.Equals(defaultValue))) { shouldWriteValue = false; if (member.IsRequired) { XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(member.Name, classContract.UnderlyingType); } } } if (shouldWriteValue) { bool writeXsiType = CheckIfMemberHasConflict(member, classContract, derivedMostClassContract); if (memberValue == null) { memberValue = ReflectionGetMemberValue(obj, member); } PrimitiveDataContract primitiveContract = member.MemberPrimitiveContract; if (writeXsiType || !ReflectionTryWritePrimitive(xmlWriter, context, memberType, memberValue, memberNames[i + childElementIndex] /*name*/, ns, primitiveContract)) { ReflectionWriteStartElement(xmlWriter, memberType, ns, ns.Value, member.Name, 0); if (classContract.ChildElementNamespaces[i + childElementIndex] != null) { var nsChildElement = classContract.ChildElementNamespaces[i + childElementIndex]; xmlWriter.WriteNamespaceDecl(nsChildElement); } ReflectionWriteValue(xmlWriter, context, memberType, memberValue, writeXsiType, primitiveContractForParamType: null); ReflectionWriteEndElement(xmlWriter); } if (classContract.HasExtensionData) { context.WriteExtensionData(xmlWriter, ((IExtensibleDataObject)obj).ExtensionData, memberCount); } } } return(memberCount); }