summaryrefslogtreecommitdiffstats
path: root/src/Cepis/GeoConverter.cs
blob: f5c78defd7279b316670b4ad903302f06dfa10d1 (plain)
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
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Cepis;

/// <summary>
/// Reads the <c>geo</c> block, yielding <see langword="null"/> for anything it
/// cannot make sense of.
/// </summary>
/// <remarks>
/// A malformed coordinate is not worth losing the whole address over: the rest
/// of the response is still useful, and an address with no coordinate is a
/// shape callers already handle, since about 43% of CEPs have none.
/// </remarks>
internal sealed class GeoConverter : JsonConverter<Geo?>
{
    public override Geo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        using var document = JsonDocument.ParseValue(ref reader);
        var root = document.RootElement;
        if (root.ValueKind != JsonValueKind.Object)
        {
            return null;
        }

        var lat = ReadDouble(root, "lat");
        var lon = ReadDouble(root, "lon");
        var raio = ReadDouble(root, "raio_m");
        var precisao = ReadString(root, "precisao");

        if (lat is null || lon is null || raio is null || precisao is null)
        {
            return null;
        }

        return new Geo
        {
            Lat = lat.Value,
            Lon = lon.Value,
            RaioM = (int)Math.Round(raio.Value),
            Precisao = precisao,
        };
    }

    public override void Write(Utf8JsonWriter writer, Geo? value, JsonSerializerOptions options)
    {
        ArgumentNullException.ThrowIfNull(writer);

        if (value is null)
        {
            writer.WriteNullValue();
            return;
        }

        writer.WriteStartObject();
        writer.WriteNumber("lat", value.Lat);
        writer.WriteNumber("lon", value.Lon);
        writer.WriteNumber("raio_m", value.RaioM);
        writer.WriteString("precisao", value.Precisao);
        writer.WriteEndObject();
    }

    private static double? ReadDouble(JsonElement owner, string name)
    {
        if (!owner.TryGetProperty(name, out var element))
        {
            return null;
        }

        return element.ValueKind switch
        {
            JsonValueKind.Number => element.TryGetDouble(out var number) ? number : null,
            JsonValueKind.String => double.TryParse(
                element.GetString(),
                NumberStyles.Float,
                CultureInfo.InvariantCulture,
                out var parsed)
                ? parsed
                : null,
            _ => null,
        };
    }

    private static string? ReadString(JsonElement owner, string name) =>
        owner.TryGetProperty(name, out var element) && element.ValueKind == JsonValueKind.String
            ? element.GetString()
            : null;
}