Skip to content
Merged
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
Expand Up @@ -5,6 +5,6 @@ public class Club
public string Name { get; set; } = string.Empty;
public int YearFounded { get; set; }
public string Country { get; set; } = string.Empty;
public string NumberOfPlayers { get; set; } = string.Empty;
public int NumberOfPlayers { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static List<Club> Generate10000ListsForBenchmark()
Name = $"Club-{i}",
YearFounded = 1000 + i,
Country = "England",
NumberOfPlayers = "30",
NumberOfPlayers = 30,
};
englishClubs.Add(club);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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

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

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using BenchmarkDotNet.Running;
using HowToSerializeAListToJsonInCSharp;

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

namespace HowToSerializeAListToJsonInCSharp
{
public class SerializeListToJsonWithNewtonsoftJson
public class SerializeListToJsonWithNewtonsoftJson(List<Club> clubList)
{
private readonly List<Club> _clubList;
private readonly JsonSerializerSettings _settings
= new()
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() },
};

public SerializeListToJsonWithNewtonsoftJson(List<Club> clubList)
{
_clubList = clubList;
}

public string SerializeObjectMethod()
{
return JsonConvert.SerializeObject(_clubList, _settings);
return JsonConvert.SerializeObject(clubList, _settings);
}

public string JsonSerializerClass()
Expand All @@ -30,7 +24,7 @@ public string JsonSerializerClass()
var stringBuilder = new StringBuilder();
using (var writer = new JsonTextWriter(new StringWriter(stringBuilder)))
{
serializer.Serialize(writer, _clubList);
serializer.Serialize(writer, clubList);
}

return stringBuilder.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

namespace HowToSerializeAListToJsonInCSharp
{
public class SerializeListToJsonWithSystemTextJson
public class SerializeListToJsonWithSystemTextJson(List<Club> clubList)
{
private readonly List<Club> _clubList;
private readonly JsonSerializerOptions _options
= new()
private readonly JsonSerializerOptions _options
= new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};

public SerializeListToJsonWithSystemTextJson(List<Club> clubList)
{
_clubList = clubList;
}

public string SerializeMethod()
{
return JsonSerializer.Serialize(_clubList, _options);
return JsonSerializer.Serialize(clubList, _options);
}

public string SerializeToUtf8BytesMethod()
{
var result = JsonSerializer.SerializeToUtf8Bytes(_clubList, _options);
var result = JsonSerializer.SerializeToUtf8Bytes(clubList, _options);

return System.Text.Encoding.UTF8.GetString(result);
}

public async Task SerializeToStreamAsync(Stream stream)
{
await JsonSerializer.SerializeAsync(stream, clubList, _options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ public class NewtonsoftJsonMethodsTests
Name = "Arsenal",
YearFounded = 1886,
Country = "England",
NumberOfPlayers = "26",
NumberOfPlayers = 26,
},
new Club
{
Name = "Manchester City",
YearFounded = 1880,
Country = "England",
NumberOfPlayers = "25",
NumberOfPlayers = 25,
},
new Club
{
Name = "Sunderland",
YearFounded = 1879,
Country = "England",
NumberOfPlayers = "30",
NumberOfPlayers = 30,
}
};

Expand All @@ -39,19 +39,19 @@ private readonly string _expectedResult
"name": "Arsenal",
"yearFounded": 1886,
"country": "England",
"numberOfPlayers": "26"
"numberOfPlayers": 26
},
{
"name": "Manchester City",
"yearFounded": 1880,
"country": "England",
"numberOfPlayers": "25"
"numberOfPlayers": 25
},
{
"name": "Sunderland",
"yearFounded": 1879,
"country": "England",
"numberOfPlayers": "30"
"numberOfPlayers": 30
}
]
""";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HowToSerializeAListToJsonInCSharp;
using System.Text;
using HowToSerializeAListToJsonInCSharp;

namespace Tests
{
Expand All @@ -11,21 +12,21 @@ public class SystemTextJsonMethodsTests
Name = "Arsenal",
YearFounded = 1886,
Country = "England",
NumberOfPlayers = "26",
NumberOfPlayers = 26,
},
new Club
{
Name = "Manchester City",
YearFounded = 1880,
Country = "England",
NumberOfPlayers = "25",
NumberOfPlayers = 25,
},
new Club
{
Name = "Sunderland",
YearFounded = 1879,
Country = "England",
NumberOfPlayers = "30",
NumberOfPlayers = 30,
}
};

Expand All @@ -39,19 +40,19 @@ private readonly string _expectedResult
"name": "Arsenal",
"yearFounded": 1886,
"country": "England",
"numberOfPlayers": "26"
"numberOfPlayers": 26
},
{
"name": "Manchester City",
"yearFounded": 1880,
"country": "England",
"numberOfPlayers": "25"
"numberOfPlayers": 25
},
{
"name": "Sunderland",
"yearFounded": 1879,
"country": "England",
"numberOfPlayers": "30"
"numberOfPlayers": 30
}
]
""";
Expand All @@ -71,5 +72,16 @@ public void GivenAList_WhenUsingTheSerializeToUtf8BytesMethod_ThenReturnsAJsonSt

Assert.Equal(_expectedResult, englishClubsJson);
}

[Fact]
public async Task GivenAList_WhenUsingTheSerializeToStreamAsyncMethod_ThenWritesAJsonStringToTheStream()
{
using var stream = new MemoryStream();

await _serializeList.SerializeToStreamAsync(stream);

var englishClubsJson = Encoding.UTF8.GetString(stream.ToArray());
Assert.Equal(_expectedResult, englishClubsJson);
}
}
}
10 changes: 5 additions & 5 deletions json-csharp/HowToSerializeAListToJsonInCSharp/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>enable</Nullable>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<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.1.2">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading