Last active
October 1, 2022 18:45
-
-
Save adamijak/872bf89dad239461009ecd10617f70a9 to your computer and use it in GitHub Desktop.
Simple System.Text.Json serializer for CosmosDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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