-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryConfigConverter.cs
More file actions
65 lines (59 loc) · 2.57 KB
/
Copy pathQueryConfigConverter.cs
File metadata and controls
65 lines (59 loc) · 2.57 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
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Interfaces;
using LantanaGroup.Link.DataAcquisition.Domain.Infrastructure.Models.QueryConfig;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace LantanaGroup.Link.DataAcquisition.Domain.Application.Serializers;
public class QueryConfigConverter : JsonConverter<IQueryConfig>
{
public override IQueryConfig Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
{
JsonElement typeElement;
string configType = null;
if (doc.RootElement.TryGetProperty("QueryConfigType", out typeElement) ||
doc.RootElement.TryGetProperty("queryConfigType", out typeElement))
{
configType = typeElement.GetString();
}
else if (doc.RootElement.TryGetProperty("$type", out typeElement))
{
var typeName = typeElement.GetString();
if (typeName?.Contains("ParameterQueryConfig") == true)
{
configType = "Parameter";
}
else if (typeName?.Contains("ReferenceQueryConfig") == true)
{
configType = "Reference";
}
}
if (configType == null)
{
// Fallback to property inspection if no type discriminator is found
if (doc.RootElement.TryGetProperty("Parameters", out _))
{
configType = "Parameter";
}
else if (doc.RootElement.TryGetProperty("Paged", out _))
{
configType = "Reference";
}
else
{
throw new JsonException("Unable to determine QueryConfigType. Missing type discriminator or distinguishing properties.");
}
}
return configType switch
{
"Parameter" => JsonSerializer.Deserialize<ParameterQueryConfig>(doc.RootElement.GetRawText(), options),
"Reference" => JsonSerializer.Deserialize<ReferenceQueryConfig>(doc.RootElement.GetRawText(), options),
_ => throw new JsonException($"Unknown QueryConfigType: {configType}")
};
}
}
public override void Write(Utf8JsonWriter writer, IQueryConfig value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}