-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseConverter.cs
More file actions
99 lines (87 loc) · 3.65 KB
/
Copy pathResponseConverter.cs
File metadata and controls
99 lines (87 loc) · 3.65 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
99
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using JSONstat.Models;
namespace JSONstat.IO;
/// <summary>
/// Reads a top-level JSON-stat response by peeking at <c>class</c> and
/// dispatching to <see cref="Dataset"/>, <see cref="Collection"/>, or
/// <see cref="Dimension"/>. Writes back the appropriate inner object.
/// </summary>
internal sealed class ResponseConverter : JsonConverter<Response>
{
public override Response Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var doc = JsonDocument.ParseValue(ref reader);
return Read(doc.RootElement, options);
}
internal static Response Read(JsonElement element, JsonSerializerOptions options)
{
var response = new Response
{
Version = TryString(element, "version"),
Label = TryString(element, "label")
};
var classString = TryString(element, "class");
response.Class = classString switch
{
"collection" => ResponseClass.Collection,
"dimension" => ResponseClass.Dimension,
"bundle" => ResponseClass.Bundle,
_ => ResponseClass.Dataset
};
switch (response.Class)
{
case ResponseClass.Collection:
response.CollectionValue = element.Deserialize<Collection>(options);
CollectEmbeddedDatasets(response.CollectionValue, element, options);
break;
case ResponseClass.Dimension:
response.DimensionValue = element.Deserialize<Dimension>(options);
response.DimensionValue?.Wire(0);
break;
default: // Dataset (and deprecated Bundle, treated as a dataset)
response.DatasetValue = element.Deserialize<Dataset>(options);
break;
}
return response;
}
private static void CollectEmbeddedDatasets(Collection? collection, JsonElement element, JsonSerializerOptions options)
{
if (collection is null) return;
if (!element.TryGetProperty("link", out var link)) return;
if (!link.TryGetProperty("item", out var items)) return;
if (items.ValueKind != JsonValueKind.Array) return;
foreach (var item in items.EnumerateArray())
{
var hasClass = item.TryGetProperty("class", out var c) && c.ValueKind == JsonValueKind.String;
var isDataset = hasClass && c.GetString() == "dataset";
var hasBody = item.TryGetProperty("value", out _) || item.TryGetProperty("dimension", out _);
if (isDataset && hasBody)
{
var ds = item.Deserialize<Dataset>(options);
if (ds != null) collection.EmbeddedDatasets.Add(ds);
}
}
}
public override void Write(Utf8JsonWriter writer, Response value, JsonSerializerOptions options)
{
switch (value.Class)
{
case ResponseClass.Collection:
JsonSerializer.Serialize(writer, value.CollectionValue, typeof(Collection), options);
return;
case ResponseClass.Dimension:
JsonSerializer.Serialize(writer, value.DimensionValue, typeof(Dimension), options);
return;
default:
JsonSerializer.Serialize(writer, value.DatasetValue, typeof(Dataset), options);
return;
}
}
private static string? TryString(JsonElement element, string name) =>
element.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String
? value.GetString()
: null;
}