Add async methods to CosmosSerializer
See original GitHub issueIs your feature request related to a problem? Please describe. Currently CosmosSerializer only has synchronous methods.
In order to create a custom serializer that uses the new JsonSerializer in .NET Core 3, async methods should be added to CosmosSerializer. The default implementation can just call the synchronous methods to support backwards compatibility.
Describe the solution you’d like
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// This is an interface to allow a custom serializer to be used by the CosmosClient
/// </summary>
public abstract class CosmosSerializer
{
/// <summary>
/// Convert a Stream of JSON to an object.
/// The caller will ensure the stream is disposed.
/// This will prevent memory leaks if an exception is hit.
/// </summary>
/// <typeparam name="T">Any typed passed to <see cref="Container"/></typeparam>
/// <param name="stream">The Stream response containing JSON from Cosmos</param>
/// <returns>The object deserialized from the stream.</returns>
public abstract T FromStream<T>(Stream stream);
/// <summary>
/// Convert the object to a Stream.
/// The caller will take ownership of the stream and ensure it is correctly disposed of.
/// Stream.CanRead must be true https://docs.microsoft.com/dotnet/api/system.io.stream.canread?view=netcore-2.0
/// </summary>
/// <param name="input">Any typed passed to <see cref="Container"/></param>
/// <returns>A readable Stream containing JSON of the serialized object</returns>
public abstract Stream ToStream<T>(T input);
/// <summary>
/// Convert a Stream of JSON to an object.
/// The caller will ensure the stream is disposed.
/// This will prevent memory leaks if an exception is hit.
/// </summary>
/// <typeparam name="T">Any typed passed to <see cref="Container"/></typeparam>
/// <param name="stream">The Stream response containing JSON from Cosmos</param>
/// <param name="cancellationToken"></param>
/// <returns>The object deserialized from the stream.</returns>
public virtual ValueTask<T> FromStreamAsync<T>(Stream stream, CancellationToken cancellationToken = default)
{
try
{
return new ValueTask<T>(this.FromStream<T>(stream));
}
catch (Exception ex)
{
return new ValueTask<T>(Task.FromException<T>(ex));
}
}
/// <summary>
/// Convert the object to a Stream.
/// The caller will take ownership of the stream and ensure it is correctly disposed of.
/// Stream.CanRead must be true https://docs.microsoft.com/dotnet/api/system.io.stream.canread?view=netcore-2.0
/// </summary>
/// <param name="input">Any typed passed to <see cref="Container"/></param>
/// <param name="cancellationToken"></param>
/// <returns>A readable Stream containing JSON of the serialized object</returns>
public virtual ValueTask<Stream> ToStreamAsync<T>(T input, CancellationToken cancellationToken = default)
{
try
{
return new ValueTask<Stream>(this.ToStream(input));
}
catch (Exception ex)
{
return new ValueTask<Stream>(Task.FromException<Stream>(ex));
}
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Container.CreateItemAsync<T> Method
Creates a item as an asynchronous operation in the Azure Cosmos service. ... CosmosSerializer to implement a custom serializer.
Read more >System.Text.Json custom serializer for CosmosDb ...
Text.Json requires extra wotk to handle serialization of class hierarchies. I changed this : SerializeAsync<T>. to this : SerializeAsync<object>.
Read more >Leveraging Azure Cosmos DB Partial Document Update ...
Utilizing Cosmos DB Partial Updates. The Cosmos DB .NET SDK exposes partial document updates through the PatchItemAsync method on a container.
Read more >Async Streams with IAsyncEnumerable<T> in .NET Core 3
It exposes an enumerator that has a MoveNextAsync() method that can awaited. This means the producer can make asynchronous calls in between ...
Read more >Handling type hierarchies in Cosmos DB (part 2)
Let's see how to apply this feature to our problem. First, let add an abstract Type property to the base class of our...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 to this. I’ve just updated an application to use the v3 Cosmos SDK, and then subsequently run into this while updating the branch of the app using ASP.NET Core 3.0 preview 8.
In the meantime, I came up with this implementation that uses the synchronous APIs for System.Text.Json.
Hi team, we use
CosmosSerializer
for our migration process. As part of our migration, we deserialize the JSON document from Cosmos asJObject
and then update theJObject
as per the latest schema of the Document. The advantage of this approach for us is that when usingCosmosClient
to read a document in our repository the transformation to the latest schema has already happened. Until now, this has worked well for us. However, now we have a requirement where we need to update theJObject
from an external resource by doing a network call. IfFromStream
wasasync
it would have made things much easier for us.