Skip to content

Commit 7ae7512

Browse files
Add SystemTextJsonSerializer to Cosmos client or else Cosmos SDK will use JSON.Net by default. (#90)
1 parent 9cfc913 commit 7ae7512

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/CosmosDBSessionStateProviderAsync/CosmosDBSessionStateProviderAsync.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Microsoft.AspNet.SessionState
1313
using System.Globalization;
1414
using System.IO;
1515
using System.IO.Compression;
16+
using System.Text.Json.Serialization;
17+
using System.Text.Json;
1618
using System.Threading;
1719
using System.Threading.Tasks;
1820
using System.Web;
@@ -859,6 +861,7 @@ internal static CosmosClientOptions ParseCosmosDBClientSettings(NameValueCollect
859861

860862
var clientOptions = new CosmosClientOptions
861863
{
864+
Serializer = new SystemTextJsonSerializer(null),
862865
ConnectionMode = conMode,
863866
RequestTimeout = new TimeSpan(0, 0, requestTimeout),
864867
GatewayModeMaxConnectionLimit = maxConnectionLimit,

src/CosmosDBSessionStateProviderAsync/Microsoft.AspNet.SessionState.CosmosDBSessionStateProviderAsync.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<ItemGroup>
6767
<Compile Include="CosmosDBSessionStateProviderAsync.cs" />
6868
<Compile Include="SessionStateActionsConverter.cs" />
69+
<Compile Include="SystemTextJsonSerializer.cs" />
6970
<Compile Include="TimeSpanConverter.cs" />
7071
<Compile Include="Properties\AssemblyInfo.cs" />
7172
<Compile Include="Resources\SR.Designer.cs">
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See the License.txt file in the project root for full license information.
3+
4+
namespace Microsoft.AspNet.SessionState
5+
{
6+
using Microsoft.Azure.Cosmos;
7+
using System.IO;
8+
using System.Text.Json;
9+
10+
internal class SystemTextJsonSerializer : CosmosSerializer
11+
{
12+
private JsonSerializerOptions _opts;
13+
14+
public SystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions)
15+
{
16+
_opts = jsonSerializerOptions ?? default(JsonSerializerOptions);
17+
}
18+
19+
public override T FromStream<T>(Stream stream)
20+
{
21+
using (stream)
22+
{
23+
if (stream.CanSeek
24+
&& stream.Length == 0)
25+
{
26+
return default;
27+
}
28+
29+
if (typeof(Stream).IsAssignableFrom(typeof(T)))
30+
{
31+
return (T)(object)stream;
32+
}
33+
34+
return JsonSerializer.Deserialize<T>(stream, _opts);
35+
}
36+
}
37+
38+
public override Stream ToStream<T>(T input)
39+
{
40+
MemoryStream ms = new MemoryStream();
41+
JsonSerializer.Serialize(ms, input, input.GetType(), _opts);
42+
ms.Position = 0;
43+
return ms;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)