Skip to content

Commit 2e619e9

Browse files
committed
Add SystemTextJsonTestGeneratorTest
1 parent 0269ece commit 2e619e9

3 files changed

Lines changed: 107 additions & 18 deletions

File tree

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
68
</PropertyGroup>
79

10+
<PropertyGroup>
11+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
12+
<CompilerGeneratedFilesOutputPath>_Generated</CompilerGeneratedFilesOutputPath>
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<Compile Remove="_Generated\**\*.g.cs" />
16+
<None Include="_Generated\**\*.g.cs" />
17+
</ItemGroup>
18+
819
<ItemGroup>
920
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1021
</ItemGroup>
1122

23+
<ItemGroup>
24+
<Folder Include="_Generated\System.Text.Json.SourceGeneration\" />
25+
</ItemGroup>
26+
1227
</Project>
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
namespace NewtonsoftJsonTest
1+
namespace NewtonsoftJsonTest;
2+
3+
internal sealed class Program
24
{
3-
internal sealed class Program
5+
6+
#region Constants & Statics
7+
8+
private static void Main(string[] args)
49
{
5-
private static void Main(string[] args)
6-
{
7-
DeserializeTest.PrivateProp_DeserializeWithJsonProperty_IsAssigned();
8-
DeserializeTest.GetonlyProp_DeserializeWithJsonProperty_IsNotAssigned();
9-
DeserializeTest.GetonlyProp_DeserializeWithField_IsAssigned();
10-
DeserializeTest.ProtectedProp_DeserializeWithField_IsAssigned();
11-
DeserializeTest.ProtectedProp_DeserializeWithJsonProperty_IsAssigned();
10+
//DeserializeTest.PrivateProp_DeserializeWithJsonProperty_IsAssigned();
11+
//DeserializeTest.GetonlyProp_DeserializeWithJsonProperty_IsNotAssigned();
12+
//DeserializeTest.GetonlyProp_DeserializeWithField_IsAssigned();
13+
//DeserializeTest.ProtectedProp_DeserializeWithField_IsAssigned();
14+
//DeserializeTest.ProtectedProp_DeserializeWithJsonProperty_IsAssigned();
1215

13-
SystemTextJsonTest.ProtectedProp_WithCtor_IsAssigned();
14-
SystemTextJsonTest.ProtectedProp_NoCtor_IsNotAssigned();
15-
SystemTextJsonTest.ProtectedProp_DeserializeWithJsonInclude_IsAssigned();
16-
SystemTextJsonTest.PrivateProp_DeserializeWithJsonInclude_IsAssigned();
17-
SystemTextJsonTest.GetonlyProp_DeserializeWithJsonInclude_IsNotAssigned();
18-
SystemTextJsonTest.GetonlyProp_DeserializeWithJsonConstructor_IsAssigned();
19-
}
16+
//SystemTextJsonTest.ProtectedProp_WithCtor_IsAssigned();
17+
//SystemTextJsonTest.ProtectedProp_NoCtor_IsNotAssigned();
18+
//SystemTextJsonTest.ProtectedProp_DeserializeWithJsonInclude_IsAssigned();
19+
//SystemTextJsonTest.PrivateProp_DeserializeWithJsonInclude_IsAssigned();
20+
//SystemTextJsonTest.GetonlyProp_DeserializeWithJsonInclude_IsNotAssigned();
21+
//SystemTextJsonTest.GetonlyProp_DeserializeWithJsonConstructor_IsAssigned();
22+
23+
SystemTextJsonTestGeneratorTest.SerializeTest();
24+
SystemTextJsonTestGeneratorTest.DeserializeTest();
2025
}
26+
27+
#endregion
28+
2129
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace NewtonsoftJsonTest;
5+
6+
public class WeatherForecast
7+
{
8+
9+
#region Properties
10+
11+
public object? Data { get; set; }
12+
13+
public List<object>? DataList { get; set; }
14+
15+
#endregion
16+
17+
}
18+
19+
[JsonSourceGenerationOptions(WriteIndented = true, GenerationMode = JsonSourceGenerationMode.Default)]
20+
[JsonSerializable(typeof(WeatherForecast))]
21+
[JsonSerializable(typeof(List<WeatherForecast>))]
22+
[JsonSerializable(typeof(bool))]
23+
[JsonSerializable(typeof(int))]
24+
[JsonSerializable(typeof(string))]
25+
internal sealed partial class WeatherForecastContext : JsonSerializerContext
26+
{
27+
}
28+
29+
public static class SystemTextJsonTestGeneratorTest
30+
{
31+
32+
#region Constants & Statics
33+
34+
public static void DeserializeTest()
35+
{
36+
const string? jsonString = """
37+
{
38+
"Data": "Sunny",
39+
"DataList": [
40+
true,
41+
1
42+
]
43+
}
44+
""";
45+
46+
var data = JsonSerializer.Deserialize(jsonString, WeatherForecastContext.Default.WeatherForecast);
47+
}
48+
49+
public static void SerializeTest()
50+
{
51+
var data = new WeatherForecast { Data = "Sunny", DataList = [true, 1] };
52+
53+
var jsonString = JsonSerializer.Serialize(data, WeatherForecastContext.Default.WeatherForecast);
54+
//output:
55+
//{
56+
// "Data": "Sunny",
57+
// "DataList": [
58+
// true,
59+
// 1
60+
// ]
61+
//}
62+
}
63+
64+
#endregion
65+
66+
}

0 commit comments

Comments
 (0)