Skip to content

Commit

Permalink
Code cleanup for serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kundadebdatta committed Nov 11, 2024
1 parent 7230e75 commit 86d07c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
33 changes: 16 additions & 17 deletions Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,28 @@ public override T FromStream<T>(Stream stream)

JsonSerializer jsonSerializer = this.GetSerializer();

if (stream is CloneableStream cloneableStream)
using (CosmosBufferedStreamWrapper bufferedStream = new (stream, shouldDisposeInnerStream: false))
{
using (CosmosBufferedStreamWrapper bufferedStream = new (cloneableStream, shouldDisposeInnerStream: false))
if (bufferedStream.GetJsonSerializationFormat() == Json.JsonSerializationFormat.Binary)
{
if (bufferedStream.GetJsonSerializationFormat() == Json.JsonSerializationFormat.Binary)
{
byte[] content = bufferedStream.ReadAll();
byte[] content = bufferedStream.ReadAll();

using Json.Interop.CosmosDBToNewtonsoftReader reader = new (
jsonReader: Json.JsonReader.Create(
jsonSerializationFormat: Json.JsonSerializationFormat.Binary,
buffer: content));
using Json.Interop.CosmosDBToNewtonsoftReader reader = new (
jsonReader: Json.JsonReader.Create(
jsonSerializationFormat: Json.JsonSerializationFormat.Binary,
buffer: content));

return jsonSerializer.Deserialize<T>(reader);
}
return jsonSerializer.Deserialize<T>(reader);
}
}

using (StreamReader sr = new (stream))
{
using (JsonTextReader jsonTextReader = new (sr))
else
{
return jsonSerializer.Deserialize<T>(jsonTextReader);
using (StreamReader sr = new (bufferedStream))
{
using (JsonTextReader jsonTextReader = new (sr))
{
return jsonSerializer.Deserialize<T>(jsonTextReader);
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,25 @@ public override T FromStream<T>(Stream stream)

using (stream)
{
if (stream is Documents.CloneableStream cloneableStream)
using (CosmosBufferedStreamWrapper bufferedStream = new (stream, shouldDisposeInnerStream: false))
{
using (CosmosBufferedStreamWrapper bufferedStream = new (cloneableStream, shouldDisposeInnerStream: false))
if (bufferedStream.GetJsonSerializationFormat() == JsonSerializationFormat.Binary)
{
if (bufferedStream.GetJsonSerializationFormat() == JsonSerializationFormat.Binary)
byte[] content = bufferedStream.ReadAll();

if (CosmosObject.TryCreateFromBuffer(content, out CosmosObject cosmosObject))
{
return System.Text.Json.JsonSerializer.Deserialize<T>(cosmosObject.ToString(), this.jsonSerializerOptions);
}
else
{
byte[] content = bufferedStream.ReadAll();

if (CosmosObject.TryCreateFromBuffer(content, out CosmosObject cosmosObject))
{
return System.Text.Json.JsonSerializer.Deserialize<T>(cosmosObject.ToString(), this.jsonSerializerOptions);
}
else
{
using Stream textStream = CosmosSerializationUtil.ConvertToStreamUsingJsonSerializationFormat(content, JsonSerializationFormat.Text);
return this.DeserializeStream<T>(textStream);
}
using Stream textStream = CosmosSerializationUtil.ConvertToStreamUsingJsonSerializationFormat(content, JsonSerializationFormat.Text);
return this.DeserializeStream<T>(textStream);
}
}
}

return this.DeserializeStream<T>(stream);
return this.DeserializeStream<T>(bufferedStream);
}
}
}

Expand Down

0 comments on commit 86d07c5

Please sign in to comment.