Skip to content

Instantly share code, notes, and snippets.

@adamijak
Last active October 1, 2022 18:45
Show Gist options
  • Save adamijak/872bf89dad239461009ecd10617f70a9 to your computer and use it in GitHub Desktop.
Save adamijak/872bf89dad239461009ecd10617f70a9 to your computer and use it in GitHub Desktop.
Simple System.Text.Json serializer for CosmosDB.
using System.Text.Json;
using Microsoft.Azure.Cosmos;
namespace Common.Helpers;
public sealed class SystemTextJsonCosmosSerializer : CosmosSerializer
{
private readonly JsonSerializerOptions options;
public SystemTextJsonCosmosSerializer(JsonSerializerOptions options)
{
this.options = options;
}
public override T FromStream<T>(Stream stream)
{
var value = JsonSerializer.Deserialize<T>(stream, options);
stream.Close();
return value;
}
public override Stream ToStream<T>(T input)
{
var stream = new MemoryStream();
JsonSerializer.Serialize(stream, input, options);
stream.Position = 0;
return stream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment