-
Notifications
You must be signed in to change notification settings - Fork 272
/
ReadMissingAndCaseInsensitive.cs
69 lines (58 loc) · 2.64 KB
/
ReadMissingAndCaseInsensitive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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 BenchmarkDotNet.Attributes.Filters;
using MicroBenchmarks;
using MicroBenchmarks.Serializers;
namespace System.Text.Json.Serialization.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
[GenericTypeArguments(typeof(Location))]
[AotFilter("Currently not supported due to missing metadata.")]
public class ReadMissingAndCaseInsensitive<T>
{
private string _serialized;
private string _serializedCamelCased;
private JsonSerializerOptions _optionsBaseline;
private JsonSerializerOptions _optionsCaseInsensitive;
[GlobalSetup]
public void Setup()
{
T value = DataGenerator.Generate<T>();
_serialized = JsonSerializer.Serialize(value);
JsonSerializerOptions options = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
_serializedCamelCased = JsonSerializer.Serialize(value, options);
_optionsBaseline = new JsonSerializerOptions();
_optionsCaseInsensitive = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
}
/// <summary>
/// Uses default settings of case-sensitive comparison and JSON that matches the Pascal-casing
/// of the properties on the class.
/// </summary>
[Benchmark]
public T Baseline() => JsonSerializer.Deserialize<T>(_serialized, _optionsBaseline);
/// <summary>
/// Properties are missing because the comparison is case-sensitive and the JSON uses camel-casing
/// which does not match the properties on the class.
/// </summary>
[Benchmark]
public T MissingProperties() => JsonSerializer.Deserialize<T>(_serializedCamelCased, _optionsBaseline);
/// <summary>
/// Case-insensitive is enabled and the casing in JSON matches the properties on the class.
/// </summary>
[Benchmark]
public T CaseInsensitiveMatching() => JsonSerializer.Deserialize<T>(_serialized, _optionsCaseInsensitive);
/// <summary>
/// Case-insensitive is enabled and the casing in JSON does not match the properties on the class.
/// </summary>
[Benchmark]
public T CaseInsensitiveNotMatching() => JsonSerializer.Deserialize<T>(_serializedCamelCased, _optionsCaseInsensitive);
}
}