diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Program.cs b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Program.cs index f0da40db5f..32906d9d7d 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Program.cs +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Program.cs @@ -1,12 +1,4 @@ using BenchmarkDotNet.Running; +using ReadAndParseAJSONFileInCSharp; -namespace ReadAndParseAJSONFileInCSharp -{ - internal class Program - { - static void Main(string[] args) - { - BenchmarkRunner.Run(); - } - } -} \ No newline at end of file +BenchmarkRunner.Run(); diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp.csproj b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp.csproj index d84826b2dd..15d2794976 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp.csproj +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp.csproj @@ -2,14 +2,18 @@ Exe - net7.0 + net10.0 enable disable - - + + + + + + diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJSONFileMethodsBenchMark.cs b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJSONFileMethodsBenchMark.cs index 4c8c813ca3..e9e5e48846 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJSONFileMethodsBenchMark.cs +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJSONFileMethodsBenchMark.cs @@ -7,10 +7,13 @@ namespace ReadAndParseAJSONFileInCSharp [Orderer(SummaryOrderPolicy.FastestToSlowest)] public class ReadAndParseJSONFileMethodsBenchMark { + private static readonly string _benchmarkJsonFilePath + = Path.Combine(AppContext.BaseDirectory, "Data", "MethodsBenchmark-json.json"); + private readonly ReadAndParseJsonFileWithNewtonsoftJson _readWithNewtonsoftJson - = new(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.Parent.FullName, "Data", "MethodsBenchmark-json.json")); + = new(_benchmarkJsonFilePath); private readonly ReadAndParseJsonFileWithSystemTextJson _readWithSystemTextJson - = new(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.Parent.FullName, "Data", "MethodsBenchmark-json.json")); + = new(_benchmarkJsonFilePath); [Benchmark] public void UseUserDefinedObjectWithNewtonsoftJson() diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJsonFileWithSystemTextJson.cs b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJsonFileWithSystemTextJson.cs index d6a7361847..8f3ce94c1c 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJsonFileWithSystemTextJson.cs +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/ReadAndParseJsonFileWithSystemTextJson.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Nodes; namespace ReadAndParseAJSONFileInCSharp { @@ -40,5 +41,21 @@ public List UseFileOpenReadTextWithSystemTextJson() return teachers; } + + public async Task> UseFileOpenReadAsyncWithSystemTextJson() + { + using FileStream json = File.OpenRead(_sampleJsonFilePath); + List teachers = await JsonSerializer.DeserializeAsync>(json, _options); + + return teachers; + } + + public JsonNode UseJsonNodeWithSystemTextJson() + { + var json = File.ReadAllText(_sampleJsonFilePath); + JsonNode teachers = JsonNode.Parse(json); + + return teachers; + } } } \ No newline at end of file diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Teacher.cs b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Teacher.cs index a43ecc4672..56f9f9804c 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Teacher.cs +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/ReadAndParseAJSONFileInCSharp/Teacher.cs @@ -7,6 +7,6 @@ public class Teacher public string LastName { get; set; } = string.Empty; public int BirthYear { get; set; } public int Level { get; set; } - public List Courses {get; set; } + public List Courses { get; set; } } } \ No newline at end of file diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/SystemTextJsonReadAndParseMethodsTest.cs b/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/SystemTextJsonReadAndParseMethodsTest.cs index d577d06e87..10ae60fdec 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/SystemTextJsonReadAndParseMethodsTest.cs +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/SystemTextJsonReadAndParseMethodsTest.cs @@ -1,4 +1,5 @@ -using ReadAndParseAJSONFileInCSharp; +using System.Text.Json.Nodes; +using ReadAndParseAJSONFileInCSharp; namespace Tests { @@ -63,5 +64,25 @@ public void GivenJsonFile_WhenUsingFileOpenReadTextWithSystemTextJson_ThenParses Assert.IsType>(teachers); Assert.Equivalent(_expectedTeacher, firstTeacher, true); } + + [Fact] + public async Task GivenJsonFile_WhenUsingFileOpenReadAsyncWithSystemTextJson_ThenParsesToAList() + { + var teachers = await _readJson.UseFileOpenReadAsyncWithSystemTextJson(); + var firstTeacher = teachers.FirstOrDefault(); + + Assert.IsType>(teachers); + Assert.Equivalent(_expectedTeacher, firstTeacher, true); + } + + [Fact] + public void GivenJsonFile_WhenUsingJsonNodeWithSystemTextJson_ThenParsesToAJsonNode() + { + var teachers = _readJson.UseJsonNodeWithSystemTextJson(); + + Assert.IsType(teachers); + Assert.Equal(_expectedTeacher.FirstName, teachers[0]["firstName"].GetValue()); + Assert.Equal(_expectedTeacher.BirthYear, teachers[0]["birthYear"].GetValue()); + } } } \ No newline at end of file diff --git a/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/Tests.csproj b/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/Tests.csproj index 83a93bd42b..34d40e5fd3 100644 --- a/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/Tests.csproj +++ b/json-csharp/ReadAndParseAJSONFileInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable disable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all