-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMillisecondsStringJsonConverter.cs
More file actions
98 lines (88 loc) · 3.39 KB
/
MillisecondsStringJsonConverter.cs
File metadata and controls
98 lines (88 loc) · 3.39 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#region License
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2025 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Couchbase.Core.Json;
/// <summary>
/// Serializes and deserializes <see cref="TimeSpan"/> as the number of whole milliseconds in the format
/// "123ms".
/// </summary>
public sealed class MillisecondsStringJsonConverter : JsonConverter<TimeSpan?>
{
public override TimeSpan? Read(ref Utf8JsonReader reader,
Type typeToConvert, JsonSerializerOptions options)
{
var stringValue = reader.GetString();
if (stringValue == null)
{
return null;
}
if (stringValue.EndsWith("ms"))
{
if (double.TryParse(stringValue[..^2], NumberStyles.Float, CultureInfo.InvariantCulture, out var numericValue))
{
return TimeSpan.FromMilliseconds(numericValue);
}
}
else if (stringValue.EndsWith("ns"))
{
if (long.TryParse(stringValue[..^2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var nanoseconds))
{
return TimeSpan.FromTicks(nanoseconds / 100);
}
}
else if (stringValue.EndsWith("µs") || stringValue.EndsWith("μs") || stringValue.EndsWith("us"))
{
if (double.TryParse(stringValue[..^2], NumberStyles.Float, CultureInfo.InvariantCulture, out var microseconds))
{
return TimeSpan.FromTicks((long)(microseconds * 10));
}
}
else if (stringValue.EndsWith('s'))
{
if (double.TryParse(stringValue[..^1], NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds))
{
return TimeSpan.FromSeconds(seconds);
}
}
throw new JsonException(
$"cannot parse {stringValue}. Only 0.0ms, 0.0ns, 0.0µs, and 0.0s formats are supported.");
}
public override void Write(Utf8JsonWriter writer, TimeSpan? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
Span<char> buffer = stackalloc char[32];
if (!((uint)value.GetValueOrDefault().TotalMilliseconds).TryFormat(buffer, out var written) || written > 30)
{
// Fallback if the buffer is too small
var str = (uint)value.GetValueOrDefault().TotalMilliseconds + "ms";
writer.WriteStringValue(str);
return;
}
"ms".AsSpan().CopyTo(buffer.Slice(written));
writer.WriteStringValue(buffer.Slice(0, written + 2));
}
}