Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] SystemTextJsonSerializer: Adds STJ Serializer #4332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bf78e7c
Code changes to add default STJ serializer.
kundadebdatta Feb 27, 2024
306b929
Code changes to add tests for STJ serializer.
kundadebdatta Feb 28, 2024
49844eb
Code changes to rename files.
kundadebdatta Mar 11, 2024
d49dff9
Code changes to clean up files.
kundadebdatta Mar 11, 2024
2d17302
Code changes to add more tests.
kundadebdatta Mar 11, 2024
c78093a
Merge branch 'master' into users/kundadebdatta/4330_add_default_stj_s…
kundadebdatta Mar 11, 2024
004d08d
Code changes to update respective contracts.
kundadebdatta Mar 11, 2024
3a728b1
Code changes to add more documentation.
kundadebdatta Mar 20, 2024
089095c
Code changes to add LINQ tests. Addressing few review comments.
kundadebdatta Apr 11, 2024
8b208de
Code changes to fix build failures.
kundadebdatta Apr 11, 2024
ca3b6a6
Code changes to update contract files.
kundadebdatta Apr 12, 2024
73178e0
Code changes to add more LINQ specific test cases.
kundadebdatta Apr 15, 2024
683ca13
Code changes to address review comments.
kundadebdatta Apr 15, 2024
61171cd
Merge branch 'master' into users/kundadebdatta/4330_add_default_stj_s…
kundadebdatta Apr 15, 2024
5df9c07
Merge branch 'master' into users/kundadebdatta/4330_add_default_stj_s…
kundadebdatta May 9, 2024
cdbf98c
Code changes to port some of the optimizations from Maya.
kundadebdatta May 9, 2024
4938ac3
Merge branch 'master' into users/kundadebdatta/4330_add_default_stj_s…
kundadebdatta May 10, 2024
37009ca
Code changes to update the remarks since the changes from maya will c…
kundadebdatta May 10, 2024
1ff1f06
Code changes to address review comments.
kundadebdatta Jun 17, 2024
0a07f19
Code changes to update preview and GA contracts.
kundadebdatta Jun 17, 2024
5327c33
Merge branch 'master' into users/kundadebdatta/4330_add_default_stj_s…
kundadebdatta Jun 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ public async Task VerifyDekOperationWithSystemTextSerializer()
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};

CosmosSystemTextJsonSerializer cosmosSystemTextJsonSerializer = new CosmosSystemTextJsonSerializer(jsonSerializerOptions);
LegacyEncryptionTests.CosmosSystemTextJsonSerializer cosmosSystemTextJsonSerializer = new (jsonSerializerOptions);

CosmosClient clientWithCosmosSystemTextJsonSerializer = TestCommon.CreateCosmosClient(builder => builder
.WithCustomSerializer(cosmosSystemTextJsonSerializer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

namespace Microsoft.Azure.Cosmos
{
using System;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

/// <summary>
/// This class provides a default implementation of System.Text.Json Cosmos Linq Serializer.
/// </summary>
internal class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
{
/// <summary>
/// A read-only instance of <see cref="JsonSerializerOptions"/>.
/// </summary>
private readonly JsonSerializerOptions jsonSerializerOptions;

/// <summary>
/// Creates an instance of <see cref="CosmosSystemTextJsonSerializer"/>
/// with the default values for the Cosmos SDK
/// </summary>
/// <param name="jsonSerializerOptions">An instance of <see cref="JsonSerializerOptions"/> containing the json serialization options.</param>
internal CosmosSystemTextJsonSerializer(
JsonSerializerOptions jsonSerializerOptions)
{
this.jsonSerializerOptions = jsonSerializerOptions;
}

/// <inheritdoc/>
public override T FromStream<T>(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));

if (typeof(Stream).IsAssignableFrom(typeof(T)))
{
return (T)(object)stream;
}

if (stream.CanSeek && stream.Length == 0)
{
return default;
}

using (stream)
{
using StreamReader reader = new (stream);
return JsonSerializer.Deserialize<T>(reader.ReadToEnd(), this.jsonSerializerOptions);
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <inheritdoc/>
public override Stream ToStream<T>(T input)
{
MemoryStream streamPayload = new ();
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
using Utf8JsonWriter writer = new (streamPayload);

JsonSerializer.Serialize(writer, input, this.jsonSerializerOptions);

streamPayload.Position = 0;
return streamPayload;
}

/// <summary>
/// Convert a MemberInfo to a string for use in LINQ query translation.
/// </summary>
/// <param name="memberInfo">Any MemberInfo used in the query.</param>
/// <returns>A serialized representation of the member.</returns>
/// <remarks>
/// Note that this is just a default implementation which handles the basic scenarios.To handle any special cases,
/// please create a custom serializer which inherits from the <see cref="CosmosSystemTextJsonSerializer"/> and overrides the
/// SerializeMemberName() method.
/// </remarks>
public override string SerializeMemberName(MemberInfo memberInfo)
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
{
JsonExtensionDataAttribute jsonExtensionDataAttribute =
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
memberInfo.GetCustomAttribute<JsonExtensionDataAttribute>(true);

if (jsonExtensionDataAttribute != null)
{
return null;
}

JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true);

if (!string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name))
{
return jsonPropertyNameAttribute.Name;
}

if (this.jsonSerializerOptions.PropertyNamingPolicy != null)
{
return this.jsonSerializerOptions.PropertyNamingPolicy.ConvertName(memberInfo.Name);
}

return memberInfo.Name;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Results>
<Result>
<Input>
<Description><![CDATA[Avg, Custom serializer: True]]></Description>
<Description><![CDATA[Avg, Serializer Name: SystemTextJsonLinqSerializer]]></Description>
<Expression><![CDATA[query.Average(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -12,7 +12,7 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Sum, Custom serializer: True]]></Description>
<Description><![CDATA[Sum, Serializer Name: SystemTextJsonLinqSerializer]]></Description>
<Expression><![CDATA[query.Sum(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -23,7 +23,7 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Select many -> Filter -> Select -> Average, Custom serializer: True]]></Description>
<Description><![CDATA[Select many -> Filter -> Select -> Average, Serializer Name: SystemTextJsonLinqSerializer]]></Description>
<Expression><![CDATA[query.SelectMany(doc => doc.ArrayField.Where(m => ((m % 3) == 0)).Select(m => m)).Average(), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -36,7 +36,7 @@ WHERE ((m0 % 3) = 0)]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Select number -> Skip -> Count, Custom serializer: True]]></Description>
<Description><![CDATA[Select number -> Skip -> Count, Serializer Name: SystemTextJsonLinqSerializer]]></Description>
<Expression><![CDATA[query.Select(f => f.NumericField).Skip(2).Count(), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -63,7 +63,7 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Avg, Custom serializer: False]]></Description>
<Description><![CDATA[Avg, Serializer Name: SystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Average(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -74,7 +74,7 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Sum, Custom serializer: False]]></Description>
<Description><![CDATA[Sum, Serializer Name: SystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Sum(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -85,7 +85,7 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Select many -> Filter -> Select -> Average, Custom serializer: False]]></Description>
<Description><![CDATA[Select many -> Filter -> Select -> Average, Serializer Name: SystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.SelectMany(doc => doc.ArrayField.Where(m => ((m % 3) == 0)).Select(m => m)).Average(), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -98,7 +98,7 @@ WHERE ((m0 % 3) = 0)]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Select number -> Skip -> Count, Custom serializer: False]]></Description>
<Description><![CDATA[Select number -> Skip -> Count, Serializer Name: SystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Select(f => f.NumericField).Skip(2).Count(), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -120,6 +120,68 @@ FROM (
<Output>
<SqlQuery><![CDATA[
SELECT VALUE MIN(root["NumericField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Avg, Serializer Name: CosmosSystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Average(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE AVG(root["NumberValueDotNet"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Sum, Serializer Name: CosmosSystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Sum(doc => doc.NumericField), Object)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE SUM(root["NumberValueDotNet"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select many -> Filter -> Select -> Average, Serializer Name: CosmosSystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.SelectMany(doc => doc.ArrayField.Where(m => ((m % 3) == 0)).Select(m => m)).Average(), Object)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE AVG(m0)
FROM root
JOIN m0 IN root["ArrayValuesDotNet"]
WHERE ((m0 % 3) = 0)]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select number -> Skip -> Count, Serializer Name: CosmosSystemTextJsonSerializer]]></Description>
<Expression><![CDATA[query.Select(f => f.NumericField).Skip(2).Count(), Object)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE COUNT(1)
FROM (
SELECT VALUE root["NumberValueDotNet"]
FROM root
OFFSET 2 LIMIT 2147483647) AS r0
]]></SqlQuery>
<ErrorMessage><![CDATA[Status Code: BadRequest,{"errors":[{"severity":"Error","location":{"start":77,"end":102},"code":"SC2204","message":"'OFFSET LIMIT' clause is not supported in subqueries."}]},0x800A0B00]]></ErrorMessage>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select number -> Min w/ mapping]]></Description>
<Expression><![CDATA[query.Select(doc => doc.NumericField).Min(num => num), Object)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE MIN(root["NumberValueDotNet"])
FROM root]]></SqlQuery>
</Output>
</Result>
Expand Down
Loading
Loading