Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 2e85d4e

Browse files
[v2] Improvements (#153)
- rename utility project - move Json related classes into utility project - add utility methods for deserializing streams
1 parent 87d6a73 commit 2e85d4e

File tree

16 files changed

+79
-49
lines changed

16 files changed

+79
-49
lines changed

src/Trakt.NET.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trakt.NET.Extensions.TMDB",
128128
EndProject
129129
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
130130
EndProject
131-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json.Utilities", "libs\shared\Json.Utilities\Json.Utilities.csproj", "{6BFCF722-CE3B-47FC-A360-E9876A4284F8}"
132-
EndProject
133131
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{B88173E7-DD80-40D7-B443-73E914D48D26}"
134132
ProjectSection(SolutionItems) = preProject
135133
libs\shared\Directory.Build.props = libs\shared\Directory.Build.props
136134
EndProjectSection
137135
EndProject
136+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trakt.NET.Utilities", "libs\shared\Trakt.NET.Utilities\Trakt.NET.Utilities.csproj", "{16328885-FE44-B25E-AB0F-32A4802C0F5B}"
137+
EndProject
138138
Global
139139
GlobalSection(SolutionConfigurationPlatforms) = preSolution
140140
Debug|Any CPU = Debug|Any CPU
@@ -181,10 +181,10 @@ Global
181181
{5E6FE4C0-07D9-010B-E19B-DA162549E9E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
182182
{5E6FE4C0-07D9-010B-E19B-DA162549E9E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
183183
{5E6FE4C0-07D9-010B-E19B-DA162549E9E3}.Release|Any CPU.Build.0 = Release|Any CPU
184-
{6BFCF722-CE3B-47FC-A360-E9876A4284F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
185-
{6BFCF722-CE3B-47FC-A360-E9876A4284F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
186-
{6BFCF722-CE3B-47FC-A360-E9876A4284F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
187-
{6BFCF722-CE3B-47FC-A360-E9876A4284F8}.Release|Any CPU.Build.0 = Release|Any CPU
184+
{16328885-FE44-B25E-AB0F-32A4802C0F5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
185+
{16328885-FE44-B25E-AB0F-32A4802C0F5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
186+
{16328885-FE44-B25E-AB0F-32A4802C0F5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
187+
{16328885-FE44-B25E-AB0F-32A4802C0F5B}.Release|Any CPU.Build.0 = Release|Any CPU
188188
EndGlobalSection
189189
GlobalSection(SolutionProperties) = preSolution
190190
HideSolutionNode = FALSE
@@ -218,8 +218,8 @@ Global
218218
{AE64986B-39E8-49C2-A3E0-48FBA888B437} = {E7B2A7FC-F550-4546-8F2C-6FC9AA49F6B5}
219219
{5E6FE4C0-07D9-010B-E19B-DA162549E9E3} = {E7B2A7FC-F550-4546-8F2C-6FC9AA49F6B5}
220220
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {E7B2A7FC-F550-4546-8F2C-6FC9AA49F6B5}
221-
{6BFCF722-CE3B-47FC-A360-E9876A4284F8} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
222221
{B88173E7-DD80-40D7-B443-73E914D48D26} = {377A7F6A-A4B9-45F6-AB2A-492F0E1F71AD}
222+
{16328885-FE44-B25E-AB0F-32A4802C0F5B} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
223223
EndGlobalSection
224224
GlobalSection(ExtensibilityGlobals) = postSolution
225225
SolutionGuid = {7025A39E-11A6-4587-98F7-E887B941D5A0}

src/libs/Trakt.NET.Extensions.TMDB/Trakt.NET.Extensions.TMDB.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<ProjectReference Include="..\Trakt.NET\Trakt.NET.csproj" />
10+
<ProjectReference Include="..\shared\Trakt.NET.Utilities\Trakt.NET.Utilities.csproj" />
1011
</ItemGroup>
1112

1213
</Project>

src/libs/Trakt.NET/Internal/Constants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Net;
1+
using System.Net;
22
using System.Text.Json;
3+
using TraktNET.Utilities.Json;
34

45
namespace TraktNET
56
{

src/libs/Trakt.NET/Internal/Extensions/StreamExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System.Text.Json;
2-
31
#if NET6_0_OR_GREATER
4-
using System.Text.Json.Serialization;
2+
using TraktNET.Utilities.Json;
3+
#else
4+
using System.Text.Json;
55
#endif
66

77
namespace TraktNET
@@ -14,10 +14,7 @@ internal static class StreamExtensions
1414
TJsonObjectType? value;
1515

1616
#if NET6_0_OR_GREATER
17-
JsonSerializerContext jsonSerializerContext = JsonSerializerContextFactoryRegistry.GetContext<TJsonObjectType>(Constants.Json.FactoryKey);
18-
19-
value = await JsonSerializer.DeserializeAsync(stream, typeof(TJsonObjectType),
20-
jsonSerializerContext, cancellationToken).ConfigureAwait(false) as TJsonObjectType;
17+
value = await JsonContextSerializer.DeserializeAsync<TJsonObjectType>(Constants.Json.FactoryKey, stream, cancellationToken);
2118
#else
2219
value = await JsonSerializer.DeserializeAsync<TJsonObjectType>(stream,
2320
Constants.Json.JsonOptions, cancellationToken).ConfigureAwait(false);
@@ -32,10 +29,7 @@ internal static class StreamExtensions
3229
IReadOnlyList<TJsonObjectType>? values;
3330

3431
#if NET6_0_OR_GREATER
35-
JsonSerializerContext jsonSerializerContext = JsonSerializerContextFactoryRegistry.GetContext<TJsonObjectType>(Constants.Json.FactoryKey);
36-
37-
values = await JsonSerializer.DeserializeAsync(stream, typeof(IReadOnlyList<TJsonObjectType>),
38-
jsonSerializerContext, cancellationToken).ConfigureAwait(false) as IReadOnlyList<TJsonObjectType>;
32+
values = await JsonContextSerializer.DeserializeArrayAsync<TJsonObjectType>(Constants.Json.FactoryKey, stream, cancellationToken);
3933
#else
4034
values = await JsonSerializer.DeserializeAsync<IReadOnlyList<TJsonObjectType>>(stream,
4135
Constants.Json.JsonOptions, cancellationToken).ConfigureAwait(false);

src/libs/Trakt.NET/Internal/Json/SerializerContexts/JsonSerializerContextFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#if NET6_0_OR_GREATER
1+
#if NET6_0_OR_GREATER
22
using System.Diagnostics;
33
using System.Text.Json;
44
using System.Text.Json.Serialization;
5+
using TraktNET.Utilities.Json;
56

67
#if NET8_0_OR_GREATER
78
using System.Collections.Frozen;

src/libs/Trakt.NET/Internal/TraktClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace TraktNET
1+
using TraktNET.Utilities.Json;
2+
3+
namespace TraktNET
24
{
35
public sealed partial class TraktClient
46
{

src/libs/Trakt.NET/Trakt.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<ItemGroup>
99
<ProjectReference Include="..\..\tools\sourcegeneration\Trakt.NET.SourceGeneration\Trakt.NET.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
10-
<ProjectReference Include="..\shared\Json.Utilities\Json.Utilities.csproj" />
10+
<ProjectReference Include="..\shared\Trakt.NET.Utilities\Trakt.NET.Utilities.csproj" />
1111
</ItemGroup>
1212

1313
</Project>

src/libs/shared/Json.Utilities/Json.Utilities.csproj

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/libs/shared/Json.Utilities/IJsonSerializerContextFactory.cs renamed to src/libs/shared/Trakt.NET.Utilities/Json/IJsonSerializerContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#if NET6_0_OR_GREATER
22
using System.Text.Json.Serialization;
33

4-
namespace TraktNET
4+
namespace TraktNET.Utilities.Json
55
{
66
public interface IJsonSerializerContextFactory
77
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if NET6_0_OR_GREATER
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace TraktNET.Utilities.Json
6+
{
7+
public static class JsonContextSerializer
8+
{
9+
public static async Task<TJsonObjectType?> DeserializeAsync<TJsonObjectType>(string factoryKey, Stream stream,
10+
CancellationToken cancellationToken = default) where TJsonObjectType : class
11+
{
12+
JsonSerializerContext jsonSerializerContext = JsonSerializerContextFactoryRegistry.Get(factoryKey).GetContext<TJsonObjectType>();
13+
14+
return await JsonSerializer.DeserializeAsync(stream, typeof(TJsonObjectType),
15+
jsonSerializerContext, cancellationToken).ConfigureAwait(false) as TJsonObjectType;
16+
}
17+
18+
public static async Task<IReadOnlyList<TJsonObjectType>?> DeserializeArrayAsync<TJsonObjectType>(string factoryKey, Stream stream,
19+
CancellationToken cancellationToken = default)
20+
{
21+
JsonSerializerContext jsonSerializerContext = JsonSerializerContextFactoryRegistry.Get(factoryKey).GetContext<TJsonObjectType>();
22+
23+
return await JsonSerializer.DeserializeAsync(stream, typeof(IReadOnlyList<TJsonObjectType>),
24+
jsonSerializerContext, cancellationToken).ConfigureAwait(false) as IReadOnlyList<TJsonObjectType>;
25+
}
26+
}
27+
}
28+
#endif

0 commit comments

Comments
 (0)