Skip to content

Instantly share code, notes, and snippets.

@manishtiwari25
Last active January 10, 2024 19:10
Show Gist options
  • Save manishtiwari25/e0a5880b5b98b71f4e61b8bb302cbe71 to your computer and use it in GitHub Desktop.
Save manishtiwari25/e0a5880b5b98b71f4e61b8bb302cbe71 to your computer and use it in GitHub Desktop.
public class CosmosSystemTextJsonSerializer : CosmosSerializer
{
private readonly JsonObjectSerializer systemTextJsonSerializer;
public CosmosSystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)
{
this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
}
public override T FromStream<T>(Stream stream)
{
using (stream)
{
if (stream.CanSeek && stream.Length == 0)
{
return default;
}
if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)stream;
}
return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
}
}
public override Stream ToStream<T>(T input)
{
MemoryStream streamPayload = new MemoryStream();
this.systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
streamPayload.Position = 0;
return streamPayload;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment