-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUIDJsonConverter.cs
More file actions
31 lines (26 loc) · 1.25 KB
/
UUIDJsonConverter.cs
File metadata and controls
31 lines (26 loc) · 1.25 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
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DaanV2.UUID;
/// <summary>The class that converts <see cref="UUID"/> to and from json data</summary>
public partial class UUIDJsonConverter : JsonConverter<UUID> {
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public override UUID Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
ReadOnlySpan<Byte> data = reader.ValueSpan;
if (data == Span<Byte>.Empty || data.Length != Format.UUID_STRING_LENGTH) {
throw new JsonException("Unknown UUID format");
}
Vector128<Byte> d = Format.Parse(data);
return new UUID(d);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public override void Write(Utf8JsonWriter writer, UUID value, JsonSerializerOptions options) {
//Converting first to string is faster, but the validation on the string is slower
//So we convert to guid, which skips that
var guid = value.ToGuid();
writer.WriteStringValue(guid);
}
}