/// <inheritdoc /> public async Task <long> SerializeCollection <T>(IAsyncEnumerable <T> collection, Stream stream, IRequest request, CancellationToken cancellationToken) where T : class { cancellationToken.ThrowIfCancellationRequested(); if (collection == null) { return(0); } var swr = new StreamWriter ( stream: stream, encoding: JsonSettings.Encoding, bufferSize: 4096, leaveOpen: true ); #if NETSTANDARD2_1 await using (swr) #else using (swr) #endif { using var jwr = new NewtonsoftJsonWriter(swr, JsonSettings.LineEndings, 0) { Formatting = JsonSettings.PrettyPrint ? Indented : None }; jwr.StartCountObjectsWritten(); Serializer.Serialize(jwr, collection.ToEnumerable()); return(jwr.StopCountObjectsWritten()); } }
/// <summary> /// Serializes an object into a stream /// </summary> /// <returns></returns> public void SerializeToStream(Stream stream, object entity, bool?prettyPrint, bool ignoreNulls = false) { var formatting = prettyPrint ?? JsonSettings.PrettyPrint ? Indented : None; var serializer = ignoreNulls ? SerializerIgnoreNulls : Serializer; using var swr = new StreamWriter(stream, JsonSettings.Encoding, 4096, true); using var jwr = new NewtonsoftJsonWriter(swr, JsonSettings.LineEndings, 0) { Formatting = formatting }; serializer.Serialize(jwr, entity); }