System.Net.Http.Formatting.BetterJsonMediaTypeFormatter.OnWriteToStreamAsync C# (CSharp) Method

OnWriteToStreamAsync() protected method

Called during serialization to write an object of the specified type to the specified stream.
protected OnWriteToStreamAsync ( Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext ) : System.Threading.Tasks.Task
type Type The type of object to write.
value object The object to write.
stream System.IO.Stream The to which to write.
contentHeaders System.Net.Http.Headers.HttpContentHeaders The for the content being written.
formatterContext FormatterContext
transportContext TransportContext The .
return System.Threading.Tasks.Task
        protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Encoding effectiveEncoding = Encoding.UTF8;

            using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(stream, effectiveEncoding)) { CloseOutput = false })
            {
                if (Indent)
                {
                    jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                }
                JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings);
                jsonSerializer.Serialize(jsonTextWriter, value);
                jsonTextWriter.Flush();
            }

            var s = new TaskCompletionSource<AsyncVoid>();
            s.SetResult(new AsyncVoid());
            return s.Task;
        }