/// <summary> /// Called during serialization to write an object of the specified <paramref name="type"/> /// to the specified <paramref name="writeStream"/>. /// </summary> /// <param name="type">The type of object to write.</param> /// <param name="value">The object to write.</param> /// <param name="writeStream">The <see cref="Stream"/> to which to write.</param> /// <param name="content">The <see cref="HttpContent"/> for the content being written.</param> /// <param name="transportContext">The <see cref="TransportContext"/>.</param> /// <returns>A <see cref="Task"/> that will write the value to the stream.</returns> public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) { if (type == null) { throw Error.ArgumentNull("type"); } if (writeStream == null) { throw Error.ArgumentNull("writeStream"); } #if !NETFX_CORE if (UseDataContractJsonSerializer && Indent) { throw Error.NotSupported(Properties.Resources.UnsupportedIndent, typeof(DataContractJsonSerializer)); } #endif return(TaskHelpers.RunSynchronously(() => { Encoding effectiveEncoding = SelectCharacterEncoding(content == null ? null : content.Headers); #if !NETFX_CORE if (UseDataContractJsonSerializer) { if (MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type)) { if (value != null) { value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value }); } } DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type); using (XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter(writeStream, effectiveEncoding, ownsStream: false)) { dataContractSerializer.WriteObject(writer, value); } } else #endif { using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(writeStream, effectiveEncoding)) { CloseOutput = false }) { if (Indent) { jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented; } JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings); jsonSerializer.Serialize(jsonTextWriter, value); jsonTextWriter.Flush(); } } })); }
/// <summary> /// Called during serialization to write an object of the specified <paramref name="type"/> /// to the specified <paramref name="writeStream"/>. /// </summary> /// <param name="type">The type of object to write.</param> /// <param name="value">The object to write.</param> /// <param name="writeStream">The <see cref="Stream"/> to which to write.</param> /// <param name="content">The <see cref="HttpContent"/> for the content being written.</param> /// <param name="transportContext">The <see cref="TransportContext"/>.</param> /// <returns>A <see cref="Task"/> that will write the value to the stream.</returns> public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) { if (type == null) { throw Error.ArgumentNull("type"); } if (writeStream == null) { throw Error.ArgumentNull("writeStream"); } return(TaskHelpers.RunSynchronously(() => { bool isRemapped = false; if (UseXmlSerializer) { isRemapped = MediaTypeFormatter.TryGetDelegatingTypeForIEnumerableGenericOrSame(ref type); } else { isRemapped = MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type); } if (isRemapped && value != null) { value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value }); } Encoding effectiveEncoding = SelectCharacterEncoding(content != null ? content.Headers : null); XmlWriterSettings writerSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = Indent, Encoding = effectiveEncoding, CloseOutput = false }; object serializer = GetSerializerForType(type); using (XmlWriter writer = XmlWriter.Create(writeStream, writerSettings)) { XmlSerializer xmlSerializer = serializer as XmlSerializer; if (xmlSerializer != null) { xmlSerializer.Serialize(writer, value); } else { XmlObjectSerializer xmlObjectSerializer = (XmlObjectSerializer)serializer; xmlObjectSerializer.WriteObject(writer, value); } } })); }
/// <inheritdoc /> public override void WriteToStream( Type type, object value, Stream writeStream, Encoding effectiveEncoding ) { if (type == null) { throw Error.ArgumentNull("type"); } if (writeStream == null) { throw Error.ArgumentNull("writeStream"); } if (effectiveEncoding == null) { throw Error.ArgumentNull("effectiveEncoding"); } if (UseDataContractJsonSerializer) { if (MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type)) { if (value != null) { value = MediaTypeFormatter .GetTypeRemappingConstructor(type) .Invoke(new object[] { value }); } } DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type); using ( XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter( writeStream, effectiveEncoding, ownsStream: false ) ) { dataContractSerializer.WriteObject(writer, value); } } else { base.WriteToStream(type, value, writeStream, effectiveEncoding); } }
private void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) { bool isRemapped = false; if (UseXmlSerializer) { isRemapped = MediaTypeFormatter.TryGetDelegatingTypeForIEnumerableGenericOrSame( ref type ); } else { isRemapped = MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame( ref type ); } if (isRemapped && value != null) { value = MediaTypeFormatter .GetTypeRemappingConstructor(type) .Invoke(new object[] { value }); } object serializer = GetSerializer(type, value, content); using (XmlWriter writer = CreateXmlWriter(writeStream, content)) { XmlSerializer xmlSerializer = serializer as XmlSerializer; if (xmlSerializer != null) { xmlSerializer.Serialize(writer, value); } else { XmlObjectSerializer xmlObjectSerializer = serializer as XmlObjectSerializer; if (xmlObjectSerializer == null) { ThrowInvalidSerializerException(serializer, "GetSerializer"); } xmlObjectSerializer.WriteObject(writer, value); } } }
private void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) { Encoding effectiveEncoding = SelectCharacterEncoding(content == null ? null : content.Headers); #if !NETFX_CORE // No DataContractJsonSerializer in portable library version if (UseDataContractJsonSerializer) { if (MediaTypeFormatter.TryGetDelegatingTypeForIQueryableGenericOrSame(ref type)) { if (value != null) { value = MediaTypeFormatter.GetTypeRemappingConstructor(type).Invoke(new object[] { value }); } } DataContractJsonSerializer dataContractSerializer = GetDataContractSerializer(type); using (XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter(writeStream, effectiveEncoding, ownsStream: false)) { dataContractSerializer.WriteObject(writer, value); } } else #endif { using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(writeStream, effectiveEncoding)) { CloseOutput = false }) { if (Indent) { jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented; } JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings); jsonSerializer.Serialize(jsonTextWriter, value); jsonTextWriter.Flush(); } } }