Skip to content

Commit 8bdd52a

Browse files
committed
Add additional tests for Newtonsoft.Json, with and without attributes
1 parent d3a2ee1 commit 8bdd52a

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

TESTS-JSON-to-CSHARP/TESTS-JSON-to-CSHARP.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
<Content Include="Test_11_NoListSetter_OUTPUT1.txt" />
9393
<Content Include="Test_17_RECORD_TYPES_INPUT.txt" />
9494
<Content Include="Test_17_RECORD_TYPES_OUTPUT.txt" />
95+
<Content Include="Test_17_RECORD_TYPES_OUTPUT_NEWTONSOFT.txt" />
96+
<Content Include="Test_17_RECORD_TYPES_OUTPUT_SYSTEMTEXTJSON.txt" />
9597
<Content Include="Test_5_BASIC_SCENARIO_INPUT.txt" />
9698
<Content Include="Test_5_BASIC_SCENARIO_OUTPUT.txt" />
9799
<Content Include="Test_1_2_SETTINGS_PASCAL_INPUT.txt" />

TESTS-JSON-to-CSHARP/Test_17_RECORD_TYPES.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@ public class Test_17_RECORD_TYPES
1111
{
1212
[TestMethod]
1313
public void Run()
14+
{
15+
DoTest(null);
16+
}
17+
18+
[TestMethod]
19+
public void RunNewtonsoft()
20+
{
21+
DoTest(false, "_NEWTONSOFT");
22+
}
23+
24+
[TestMethod]
25+
public void RunSystemTextJson()
26+
{
27+
DoTest(true, "_SYSTEMTEXTJSON");
28+
}
29+
30+
private static void DoTest(bool? useSystemTextJson, string outputFileSuffix = "")
1431
{
1532
string inputPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_17_RECORD_TYPES_INPUT.txt";
16-
string outputPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_17_RECORD_TYPES_OUTPUT.txt";
33+
string outputPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + $@"Test_17_RECORD_TYPES_OUTPUT{outputFileSuffix}.txt";
1734
string input = File.ReadAllText(inputPath);
1835
string output = File.ReadAllText(outputPath);
1936

2037
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator
2138
{
2239
CodeWriter = new CSharpCodeWriter(),
2340
RecordTypes = true,
24-
UseJsonPropertyName = true, // TODO: also test with newtonsoft json
41+
UseJsonPropertyName = useSystemTextJson == true,
42+
UseJsonAttributes = useSystemTextJson == false,
2543
};
2644

2745
string actual = jsonClassGenerator.GenerateClasses(input, out _).ToString();
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
1+
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
22
public record Value(
3-
[property: JsonPropertyName("id")] int Id,
4-
[property: JsonPropertyName("real")] bool Real
3+
int id,
4+
bool real
55
);
66

77
public record Root(
8-
[property: JsonPropertyName("key")] string Key,
9-
[property: JsonPropertyName("values")] IReadOnlyList<Value> Values
8+
string key,
9+
IReadOnlyList<Value> values
1010
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
2+
public record Value(
3+
[property: JsonProperty("id")] int Id,
4+
[property: JsonProperty("real")] bool Real
5+
);
6+
7+
public record Root(
8+
[property: JsonProperty("key")] string Key,
9+
[property: JsonProperty("values")] IReadOnlyList<Value> Values
10+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
2+
public record Value(
3+
[property: JsonPropertyName("id")] int Id,
4+
[property: JsonPropertyName("real")] bool Real
5+
);
6+
7+
public record Root(
8+
[property: JsonPropertyName("key")] string Key,
9+
[property: JsonPropertyName("values")] IReadOnlyList<Value> Values
10+
);

0 commit comments

Comments
 (0)