Last active
January 10, 2024 19:10
-
-
Save manishtiwari25/e0a5880b5b98b71f4e61b8bb302cbe71 to your computer and use it in GitHub Desktop.
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
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