Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using BenchmarkDotNet.Running;
using ReadAndParseAJSONFileInCSharp;

namespace ReadAndParseAJSONFileInCSharp
{
internal class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<ReadAndParseJSONFileMethodsBenchMark>();
}
}
}
BenchmarkRunner.Run<ReadAndParseJSONFileMethodsBenchMark>();
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

<ItemGroup>
<None Include="Data\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ReadAndParseAJSONFileInCSharp
{
Expand Down Expand Up @@ -40,5 +41,21 @@ public List<Teacher> UseFileOpenReadTextWithSystemTextJson()

return teachers;
}

public async Task<List<Teacher>> UseFileOpenReadAsyncWithSystemTextJson()
{
using FileStream json = File.OpenRead(_sampleJsonFilePath);
List<Teacher> teachers = await JsonSerializer.DeserializeAsync<List<Teacher>>(json, _options);

return teachers;
}

public JsonNode UseJsonNodeWithSystemTextJson()
{
var json = File.ReadAllText(_sampleJsonFilePath);
JsonNode teachers = JsonNode.Parse(json);

return teachers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course> Courses {get; set; }
public List<Course> Courses { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ReadAndParseAJSONFileInCSharp;
using System.Text.Json.Nodes;
using ReadAndParseAJSONFileInCSharp;

namespace Tests
{
Expand Down Expand Up @@ -63,5 +64,25 @@ public void GivenJsonFile_WhenUsingFileOpenReadTextWithSystemTextJson_ThenParses
Assert.IsType<List<Teacher>>(teachers);
Assert.Equivalent(_expectedTeacher, firstTeacher, true);
}

[Fact]
public async Task GivenJsonFile_WhenUsingFileOpenReadAsyncWithSystemTextJson_ThenParsesToAList()
{
var teachers = await _readJson.UseFileOpenReadAsyncWithSystemTextJson();
var firstTeacher = teachers.FirstOrDefault();

Assert.IsType<List<Teacher>>(teachers);
Assert.Equivalent(_expectedTeacher, firstTeacher, true);
}

[Fact]
public void GivenJsonFile_WhenUsingJsonNodeWithSystemTextJson_ThenParsesToAJsonNode()
{
var teachers = _readJson.UseJsonNodeWithSystemTextJson();

Assert.IsType<JsonArray>(teachers);
Assert.Equal(_expectedTeacher.FirstName, teachers[0]["firstName"].GetValue<string>());
Assert.Equal(_expectedTeacher.BirthYear, teachers[0]["birthYear"].GetValue<int>());
}
}
}
10 changes: 5 additions & 5 deletions json-csharp/ReadAndParseAJSONFileInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading