Skip to content

Commit b8731e1

Browse files
Initial commit
0 parents  commit b8731e1

7 files changed

Lines changed: 147 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin/
2+
obj/
3+
/packages/
4+
riderModule.iml
5+
/_ReSharper.Caches/

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# System.Text.Json.Extensions
2+
3+
This project tries to collect some extensions and converters that ease the usage of `System.Text.Json`.

System.Text.Json.Extensions.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.Extensions", "System.Text.Json.Extensions\System.Text.Json.Extensions.csproj", "{79DECB74-8532-4491-BCD8-B87789686689}"
4+
EndProject
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E18A8452-2B48-4C7F-89C1-F0B81FDA6D7E}"
6+
ProjectSection(SolutionItems) = preProject
7+
Readme.md = Readme.md
8+
EndProjectSection
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Text.Json.ExtensionsTest", "System.Text.Json.ExtensionsTest\System.Text.Json.ExtensionsTest.csproj", "{160C8D7A-C96E-4FCC-8585-7CE264641458}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{79DECB74-8532-4491-BCD8-B87789686689}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{79DECB74-8532-4491-BCD8-B87789686689}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{79DECB74-8532-4491-BCD8-B87789686689}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{79DECB74-8532-4491-BCD8-B87789686689}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{160C8D7A-C96E-4FCC-8585-7CE264641458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{160C8D7A-C96E-4FCC-8585-7CE264641458}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{160C8D7A-C96E-4FCC-8585-7CE264641458}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{160C8D7A-C96E-4FCC-8585-7CE264641458}.Release|Any CPU.Build.0 = Release|Any CPU
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="System.Text.Json" Version="4.7.2" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Text.Json.Serialization;
2+
using System.Text.Json.Serialization.Converters;
3+
4+
namespace System.Text.Json.Extensions
5+
{
6+
public static class Utf8JsonReaderExtensions
7+
{
8+
/// <summary>
9+
/// Tries to read the next token as object of type <typeparamref name="T"/>.
10+
///
11+
/// Based on <see cref="JsonKeyValuePairConverter{TKey,TValue}.ReadProperty{T}"/>
12+
/// </summary>
13+
public static T ReadObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options)
14+
{
15+
do
16+
{
17+
reader.Read();
18+
} while (reader.TokenType == JsonTokenType.Comment
19+
&& options.ReadCommentHandling == JsonCommentHandling.Skip);
20+
21+
return GetObject<T>(ref reader, options);
22+
}
23+
24+
/// <summary>
25+
/// Tries to read the current token as object of type <typeparamref name="T"/>.
26+
///
27+
/// Based on <see cref="JsonKeyValuePairConverter{TKey,TValue}.ReadProperty{T}"/>
28+
/// </summary>
29+
public static T GetObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options)
30+
{
31+
var typeToConvert = typeof(T);
32+
33+
// Attempt to use existing converter first before re-entering through JsonSerializer.Deserialize().
34+
// The default converter for objects does not parse null objects as null, so it is not used here.
35+
if (typeToConvert != typeof(object)
36+
&& options?.GetConverter(typeToConvert) is JsonConverter<T> keyConverter)
37+
{
38+
return keyConverter.Read(ref reader, typeToConvert, options);
39+
}
40+
else
41+
{
42+
return JsonSerializer.Deserialize<T>(ref reader, options);
43+
}
44+
}
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
11+
<PackageReference Include="Shouldly" Version="3.0.2" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="1.3.0">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\System.Text.Json.Extensions\System.Text.Json.Extensions.csproj" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Text.Json.Extensions;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace System.Text.Json.ExtensionsTest
6+
{
7+
public class Utf8JsonReaderExtensionsFixture
8+
{
9+
private class DummyObject
10+
{
11+
public string MyValue { get; set; }
12+
}
13+
14+
[Fact]
15+
public void ReadObjectReadDummyObject()
16+
{
17+
var jsonReaderOptions = new JsonReaderOptions();
18+
var jsonSerializerOptions = new JsonSerializerOptions();
19+
var utf8Bytes = Encoding.UTF8.GetBytes("{\"MyValue\": \"MyValue\"}");
20+
var reader = new Utf8JsonReader(utf8Bytes, jsonReaderOptions);
21+
22+
var dummyObject = reader.ReadObject<DummyObject>(jsonSerializerOptions);
23+
24+
dummyObject.MyValue
25+
.ShouldBe("MyValue");
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)