Created
January 28, 2020 00:47
-
-
Save ahsonkhan/8f86114245fd15b46d084ddfc4935d41 to your computer and use it in GitHub Desktop.
Benchmark for System.Text.Json Deserialize with various casing approaches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Licensed to the .NET Foundation under one or more agreements. | |
// The .NET Foundation licenses this file to you under the MIT license. | |
// See the LICENSE file in the project root for more information. | |
using BenchmarkDotNet.Attributes; | |
using MicroBenchmarks; | |
using System.Collections.Generic; | |
namespace System.Text.Json.Serialization.Tests | |
{ | |
public class CaseSensitive | |
{ | |
private JsonSerializerOptions _options; | |
private JsonSerializerOptions _optionsNamingPolicy; | |
private JsonSerializerOptions _aspnetWebDefaults; | |
private const string _jsonStringPascalCase = "{\"MyString\" : \"abc\", \"MyInteger\" : 123, \"MyList\" : [\"abc\", \"123\"]}"; | |
private const string _jsonStringCamelCase = "{\"myString\" : \"abc\", \"myInteger\" : 123, \"myList\" : [\"abc\", \"123\"]}"; | |
private byte[] _jsonBytesPascalCase; | |
private byte[] _jsonBytesCamelCase; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
_options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; | |
_optionsNamingPolicy = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; | |
_aspnetWebDefaults = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true }; | |
_jsonBytesPascalCase = Encoding.UTF8.GetBytes(_jsonStringPascalCase); | |
_jsonBytesCamelCase = Encoding.UTF8.GetBytes(_jsonStringCamelCase); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseSensitive_Pascal() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonStringPascalCase); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseInsensitive_Pascal() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonStringPascalCase, _options); | |
} | |
// Not sure if this benchmark makes sense | |
//[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
//[Benchmark] | |
//public MyClass SystemTextCaseSensitive_Camel() | |
//{ | |
// return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase); | |
//} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass_Annotated SystemTextCaseSensitive_Annotated() | |
{ | |
return JsonSerializer.Deserialize<MyClass_Annotated>(_jsonStringCamelCase); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseInsensitive_Camel() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _options); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseSensitive_Pascal_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonBytesPascalCase); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseInsensitive_Pascal_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonBytesPascalCase, _options); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemTextCaseInsensitive_Camel_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _options); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemText_CamelNamingPolicy() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _optionsNamingPolicy); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemText_CamelNamingPolicy_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _optionsNamingPolicy); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass_Annotated SystemTextCaseSensitive_Annotated_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass_Annotated>(_jsonBytesCamelCase); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemText_AspnetWebDefaults() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _aspnetWebDefaults); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark] | |
public MyClass SystemText_AspnetWebDefaults_Utf8() | |
{ | |
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _aspnetWebDefaults); | |
} | |
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)] | |
[Benchmark(Baseline = true)] | |
public MyClass NewtonSoftJson_Camel() | |
{ | |
return Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(_jsonStringCamelCase); | |
} | |
public class MyClass | |
{ | |
public int MyInteger { get; set; } | |
public string MyString { get; set; } | |
public List<string> MyList { get; set; } | |
} | |
public class MyClass_Annotated | |
{ | |
[JsonPropertyName("myInteger")] | |
public int MyInteger { get; set; } | |
[JsonPropertyName("myString")] | |
public string MyString { get; set; } | |
[JsonPropertyName("myList")] | |
public List<string> MyList { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the context:
https://dotnetcoretutorials.com/2020/01/25/what-those-benchmarks-of-system-text-json-dont-mention/#comment-22211