forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNullableIntConverter.cs
More file actions
43 lines (36 loc) · 1.1 KB
/
Copy pathNullableIntConverter.cs
File metadata and controls
43 lines (36 loc) · 1.1 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
namespace Nethermind.Serialization.Json
{
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
public class NullableIntConverter : JsonConverter<int?>
{
private readonly IntConverter _converter = new();
public override int? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
return _converter.Read(ref reader, typeToConvert, options);
}
[SkipLocalsInit]
public override void Write(
Utf8JsonWriter writer,
int? value,
JsonSerializerOptions options)
{
if (!value.HasValue)
{
writer.WriteNullValue();
}
_converter.Write(writer, value.GetValueOrDefault(), options);
}
}
}