From 86d07c58718ffcd1ee5c34e9cad426fa6cf84dce Mon Sep 17 00:00:00 2001 From: Debdatta Kunda Date: Mon, 11 Nov 2024 12:05:37 -0800 Subject: [PATCH] Code cleanup for serializers. --- .../Serializer/CosmosJsonDotNetSerializer.cs | 33 +++++++++---------- .../CosmosSystemTextJsonSerializer.cs | 29 ++++++++-------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs index 279bf95c71..c86d2f941e 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs @@ -98,29 +98,28 @@ public override T FromStream(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(reader); - } + return jsonSerializer.Deserialize(reader); } - } - - using (StreamReader sr = new (stream)) - { - using (JsonTextReader jsonTextReader = new (sr)) + else { - return jsonSerializer.Deserialize(jsonTextReader); + using (StreamReader sr = new (bufferedStream)) + { + using (JsonTextReader jsonTextReader = new (sr)) + { + return jsonSerializer.Deserialize(jsonTextReader); + } + } } } } diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs index 6fcf5ee2c9..e058ed41c0 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs @@ -52,28 +52,25 @@ public override T FromStream(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(cosmosObject.ToString(), this.jsonSerializerOptions); + } + else { - byte[] content = bufferedStream.ReadAll(); - - if (CosmosObject.TryCreateFromBuffer(content, out CosmosObject cosmosObject)) - { - return System.Text.Json.JsonSerializer.Deserialize(cosmosObject.ToString(), this.jsonSerializerOptions); - } - else - { - using Stream textStream = CosmosSerializationUtil.ConvertToStreamUsingJsonSerializationFormat(content, JsonSerializationFormat.Text); - return this.DeserializeStream(textStream); - } + using Stream textStream = CosmosSerializationUtil.ConvertToStreamUsingJsonSerializationFormat(content, JsonSerializationFormat.Text); + return this.DeserializeStream(textStream); } } - } - return this.DeserializeStream(stream); + return this.DeserializeStream(bufferedStream); + } } }