using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using CosmosDB3Test; using Microsoft.Azure.Cosmos; using Newtonsoft.Json.Serialization; namespace CosmosDBTest.Controllers { public class CosDocument { public string _rid { get; set; } public T Documents{get;set;} } public class CustomSerializer : CosmosSerializer { private readonly JsonSerializerOptions _options; internal CustomSerializer(JsonSerializerOptions options) { _options = options; } /// public override T FromStream(Stream stream) { // Have to dispose of the stream, otherwise the Cosmos SDK throws. // https://github.com/Azure/azure-cosmos-dotnet-v3/blob/0843cae3c252dd49aa8e392623d7eaaed7eb712b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonSerializerWrapper.cs#L22 // https://github.com/Azure/azure-cosmos-dotnet-v3/blob/0843cae3c252dd49aa8e392623d7eaaed7eb712b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs#L73 using (stream) { // TODO Would be more efficient if CosmosSerializer supported async // See https://github.com/Azure/azure-cosmos-dotnet-v3/issues/715 using var memory = new MemoryStream((int)stream.Length); stream.CopyTo(memory); byte[] utf8Json = memory.ToArray(); return JsonSerializer.Deserialize(utf8Json, _options); } } /// public override Stream ToStream(T input) { byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(input, _options); return new MemoryStream(utf8Json); } } }