-
Notifications
You must be signed in to change notification settings - Fork 8
/
ErrorResultReader.cs
48 lines (42 loc) · 1.3 KB
/
ErrorResultReader.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
using System;
using System.Collections.Generic;
using System.Text.Json;
using MetaBrainz.Common.Json;
using MetaBrainz.Common.Json.Converters;
using MetaBrainz.MusicBrainz.Objects;
namespace MetaBrainz.MusicBrainz.Json.Readers;
internal sealed class ErrorResultReader : ObjectReader<ErrorResult> {
public static readonly ErrorResultReader Instance = new();
protected override ErrorResult ReadObjectContents(ref Utf8JsonReader reader, JsonSerializerOptions options) {
string? error = null;
string? help = null;
Dictionary<string, object?>? rest = null;
while (reader.TokenType == JsonTokenType.PropertyName) {
var prop = reader.GetPropertyName();
try {
reader.Read();
switch (prop) {
case "error":
error = reader.GetString();
break;
case "help":
help = reader.GetString();
break;
default:
rest ??= new Dictionary<string, object?>();
rest[prop] = reader.GetOptionalObject(options);
break;
}
}
catch (Exception e) {
throw new JsonException($"Failed to deserialize the '{prop}' property.", e);
}
reader.Read();
}
return new ErrorResult {
Error = error,
Help = help,
UnhandledProperties = rest
};
}
}