Skip to content

Commit 93a0f9c

Browse files
committed
Code changes to hide STJ serializer implementation behind a boolean flag.
1 parent bfdb850 commit 93a0f9c

File tree

7 files changed

+67
-117
lines changed

7 files changed

+67
-117
lines changed

Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,15 @@ public ConnectionMode ConnectionMode
392392
/// <seealso cref="ItemRequestOptions.EnableContentResponseOnWrite"/>
393393
/// <seealso cref="TransactionalBatchItemRequestOptions.EnableContentResponseOnWrite"/>
394394
public bool? EnableContentResponseOnWrite { get; set; }
395+
396+
/// <summary>
397+
/// Gets or sets the boolean flag to indicate if the default STJ serializer <see cref="CosmosSystemTextJsonSerializer"/> needed to be
398+
/// used for JSON serialization.
399+
/// </summary>
400+
/// <value>
401+
/// The default value is false
402+
/// </value>
403+
public bool UseSystemTextJsonForSerialization { get; set; } = false;
395404

396405
/// <summary>
397406
/// Gets or sets the advanced replica selection flag. The advanced replica selection logic keeps track of the replica connection
@@ -583,6 +592,11 @@ public CosmosSerializer Serializer
583592
throw new ArgumentException(
584593
$"{nameof(this.Serializer)} is not compatible with {nameof(this.SerializerOptions)}. Only one can be set. ");
585594
}
595+
596+
if (this.UseSystemTextJsonForSerialization)
597+
{
598+
throw new ArgumentException($"Cannot set a custom {nameof(this.Serializer)} when {nameof(this.UseSystemTextJsonForSerialization)} is enabled. Either specify a custom or set {nameof(this.UseSystemTextJsonForSerialization)} to the default STJ serializer.");
599+
}
586600

587601
this.serializerInternal = value;
588602
}
@@ -833,8 +847,11 @@ internal Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> GetServerCerti
833847
internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
834848
{
835849
if (this.serializerInternal == null)
836-
{
837-
this.serializerInternal = serializer ?? throw new ArgumentNullException(nameof(serializer));
850+
{
851+
this.serializerInternal = this.UseSystemTextJsonForSerialization
852+
? new CosmosSystemTextJsonSerializer(
853+
new System.Text.Json.JsonSerializerOptions())
854+
: serializer ?? throw new ArgumentNullException(nameof(serializer));
838855
}
839856
}
840857

Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,16 @@ internal CosmosClientBuilder WithPartitionLevelFailoverEnabled()
726726
this.clientOptions.EnablePartitionLevelFailover = true;
727727
return this;
728728
}
729+
730+
/// <summary>
731+
/// Enables the usage of <see cref="CosmosSystemTextJsonSerializer"/> as the default
732+
/// serializer.
733+
/// </summary>
734+
internal CosmosClientBuilder WithSystemTextJsonSerializerEnabled()
735+
{
736+
this.clientOptions.UseSystemTextJsonForSerialization = true;
737+
return this;
738+
}
729739

730740
/// <summary>
731741
/// Enables SDK to inject fault. Used for testing applications.

Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.Azure.Cosmos
1313
/// <summary>
1414
/// This class provides a default implementation of System.Text.Json Cosmos Linq Serializer.
1515
/// </summary>
16-
public class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
16+
internal class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
1717
{
1818
/// <summary>
1919
/// A read-only instance of <see cref="JsonSerializerOptions"/>.
@@ -25,7 +25,7 @@ public class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
2525
/// with the default values for the Cosmos SDK
2626
/// </summary>
2727
/// <param name="jsonSerializerOptions">An instance of <see cref="JsonSerializerOptions"/> containing the json serialization options.</param>
28-
public CosmosSystemTextJsonSerializer(
28+
internal CosmosSystemTextJsonSerializer(
2929
JsonSerializerOptions jsonSerializerOptions)
3030
{
3131
this.jsonSerializerOptions = jsonSerializerOptions;

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Linq/LinqAggregateCustomSerializationBaseline.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class LinqAggregateCustomSerializationBaseline : BaselineTests<LinqAggreg
2929
private static Cosmos.Database testDb;
3030
private static Container testContainer;
3131

32-
private static CosmosSerializer stjCosmosSerializer;
3332
private static CosmosClient stjClient;
3433
private static Cosmos.Database testDbSTJ;
3534
private static Container testContainerSTJ;
@@ -64,10 +63,8 @@ public async static Task Initialize(TestContext textContext)
6463
testDb = await client.CreateDatabaseAsync(dbName);
6564
testContainer = testDb.CreateContainerAsync(new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: "/Pk")).Result;
6665

67-
stjCosmosSerializer = new CosmosSystemTextJsonSerializer(new JsonSerializerOptions());
68-
6966
stjClient = TestCommon.CreateCosmosClient((cosmosClientBuilder)
70-
=> cosmosClientBuilder.WithCustomSerializer(stjCosmosSerializer));
67+
=> cosmosClientBuilder.WithSystemTextJsonSerializerEnabled());
7168

7269
// Set a callback to get the handle of the last executed query to do the verification
7370
// This is neede because aggregate queries return type is a scalar so it can't be used

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/LinqTranslationWithCustomSerializerBaseline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async static Task Initialize(TestContext textContext)
5757
TestDb = await CosmosClient.CreateDatabaseAsync(dbName);
5858

5959
CosmosDefaultSTJClient = TestCommon.CreateCosmosClient((cosmosClientBuilder)
60-
=> cosmosClientBuilder.WithCustomSerializer(new CosmosSystemTextJsonSerializer(new JsonSerializerOptions())));
60+
=> cosmosClientBuilder.WithSystemTextJsonSerializerEnabled());
6161

6262
string dbNameSTJ = $"{nameof(LinqTranslationBaselineTests)}-{Guid.NewGuid():N}";
6363
TestDbSTJDefault = await CosmosDefaultSTJClient.CreateDatabaseAsync(dbNameSTJ);

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetSDKAPI.json

Lines changed: 21 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,11 +2711,23 @@
27112711
],
27122712
"MethodInfo": "Boolean get_LimitToEndpoint();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
27132713
},
2714+
"Boolean get_UseSystemTextJsonForSerialization()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
2715+
"Type": "Method",
2716+
"Attributes": [
2717+
"CompilerGeneratedAttribute"
2718+
],
2719+
"MethodInfo": "Boolean get_UseSystemTextJsonForSerialization();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
2720+
},
27142721
"Boolean LimitToEndpoint": {
27152722
"Type": "Property",
27162723
"Attributes": [],
27172724
"MethodInfo": "Boolean LimitToEndpoint;CanRead:True;CanWrite:True;Boolean get_LimitToEndpoint();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_LimitToEndpoint(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
27182725
},
2726+
"Boolean UseSystemTextJsonForSerialization": {
2727+
"Type": "Property",
2728+
"Attributes": [],
2729+
"MethodInfo": "Boolean UseSystemTextJsonForSerialization;CanRead:True;CanWrite:True;Boolean get_UseSystemTextJsonForSerialization();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_UseSystemTextJsonForSerialization(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
2730+
},
27192731
"Int32 GatewayModeMaxConnectionLimit": {
27202732
"Type": "Property",
27212733
"Attributes": [],
@@ -3165,6 +3177,13 @@
31653177
],
31663178
"MethodInfo": "Void set_TokenCredentialBackgroundRefreshInterval(System.Nullable`1[System.TimeSpan]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
31673179
},
3180+
"Void set_UseSystemTextJsonForSerialization(Boolean)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
3181+
"Type": "Method",
3182+
"Attributes": [
3183+
"CompilerGeneratedAttribute"
3184+
],
3185+
"MethodInfo": "Void set_UseSystemTextJsonForSerialization(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3186+
},
31683187
"Void set_WebProxy(System.Net.IWebProxy)": {
31693188
"Type": "Method",
31703189
"Attributes": [],
@@ -3407,34 +3426,7 @@
34073426
"NestedTypes": {}
34083427
},
34093428
"Microsoft.Azure.Cosmos.CosmosLinqSerializer;Microsoft.Azure.Cosmos.CosmosSerializer;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
3410-
"Subclasses": {
3411-
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
3412-
"Subclasses": {},
3413-
"Members": {
3414-
"System.IO.Stream ToStream[T](T)": {
3415-
"Type": "Method",
3416-
"Attributes": [],
3417-
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3418-
},
3419-
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
3420-
"Type": "Method",
3421-
"Attributes": [],
3422-
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3423-
},
3424-
"T FromStream[T](System.IO.Stream)": {
3425-
"Type": "Method",
3426-
"Attributes": [],
3427-
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3428-
},
3429-
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
3430-
"Type": "Constructor",
3431-
"Attributes": [],
3432-
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
3433-
}
3434-
},
3435-
"NestedTypes": {}
3436-
}
3437-
},
3429+
"Subclasses": {},
34383430
"Members": {
34393431
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
34403432
"Type": "Method",
@@ -3685,65 +3677,12 @@
36853677
"Microsoft.Azure.Cosmos.CosmosSerializer;System.Object;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
36863678
"Subclasses": {
36873679
"Microsoft.Azure.Cosmos.CosmosLinqSerializer;Microsoft.Azure.Cosmos.CosmosSerializer;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
3688-
"Subclasses": {
3689-
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
3690-
"Subclasses": {},
3691-
"Members": {
3692-
"System.IO.Stream ToStream[T](T)": {
3693-
"Type": "Method",
3694-
"Attributes": [],
3695-
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3696-
},
3697-
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
3698-
"Type": "Method",
3699-
"Attributes": [],
3700-
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3701-
},
3702-
"T FromStream[T](System.IO.Stream)": {
3703-
"Type": "Method",
3704-
"Attributes": [],
3705-
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3706-
},
3707-
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
3708-
"Type": "Constructor",
3709-
"Attributes": [],
3710-
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
3711-
}
3712-
},
3713-
"NestedTypes": {}
3714-
}
3715-
},
3716-
"Members": {
3717-
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
3718-
"Type": "Method",
3719-
"Attributes": [],
3720-
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3721-
}
3722-
},
3723-
"NestedTypes": {}
3724-
},
3725-
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
37263680
"Subclasses": {},
37273681
"Members": {
3728-
"System.IO.Stream ToStream[T](T)": {
3729-
"Type": "Method",
3730-
"Attributes": [],
3731-
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3732-
},
37333682
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
37343683
"Type": "Method",
37353684
"Attributes": [],
3736-
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3737-
},
3738-
"T FromStream[T](System.IO.Stream)": {
3739-
"Type": "Method",
3740-
"Attributes": [],
3741-
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3742-
},
3743-
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
3744-
"Type": "Constructor",
3745-
"Attributes": [],
3746-
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
3685+
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
37473686
}
37483687
},
37493688
"NestedTypes": {}
@@ -3763,32 +3702,6 @@
37633702
},
37643703
"NestedTypes": {}
37653704
},
3766-
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
3767-
"Subclasses": {},
3768-
"Members": {
3769-
"System.IO.Stream ToStream[T](T)": {
3770-
"Type": "Method",
3771-
"Attributes": [],
3772-
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3773-
},
3774-
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
3775-
"Type": "Method",
3776-
"Attributes": [],
3777-
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
3778-
},
3779-
"T FromStream[T](System.IO.Stream)": {
3780-
"Type": "Method",
3781-
"Attributes": [],
3782-
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
3783-
},
3784-
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
3785-
"Type": "Constructor",
3786-
"Attributes": [],
3787-
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
3788-
}
3789-
},
3790-
"NestedTypes": {}
3791-
},
37923705
"Microsoft.Azure.Cosmos.CosmosThresholdOptions;System.Object;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
37933706
"Subclasses": {},
37943707
"Members": {

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientOptionsUnitTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,19 @@ public void ThrowOnCustomSerializerWithSerializerOptions()
501501

502502
options.Serializer = new CosmosJsonDotNetSerializer();
503503
}
504+
505+
[TestMethod]
506+
[ExpectedException(typeof(ArgumentException))]
507+
public void ThrowOnCustomSerializerWithSTJSerializerEnabled()
508+
{
509+
CosmosClientOptions options = new CosmosClientOptions()
510+
{
511+
Serializer = new CosmosJsonDotNetSerializer()
512+
};
513+
514+
options.SerializerOptions = new CosmosSerializationOptions();
515+
options.UseSystemTextJsonForSerialization = true;
516+
}
504517

505518
[TestMethod]
506519
[ExpectedException(typeof(ArgumentNullException))]

0 commit comments

Comments
 (0)