Skip to content

Commit ac1ec13

Browse files
Add deserialization support for GeoBounds and GeoLocation (#8586) (#8587)
(cherry picked from commit 6a41bd9) Co-authored-by: Florian Bernd <[email protected]>
1 parent 6404675 commit ac1ec13

File tree

3 files changed

+117
-7
lines changed

3 files changed

+117
-7
lines changed

src/Elastic.Clients.Elasticsearch/_Shared/Core/UrlParameters/Username/Username.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,24 @@ public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? prov
7979
}
8080

8181
internal sealed class UsernameConverter :
82-
JsonConverter<Name>
82+
JsonConverter<Username>
8383
{
84-
public override Name? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
84+
public override Username? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
8585
{
8686
reader.ValidateToken(JsonTokenType.String);
8787

8888
return reader.GetString()!;
8989
}
9090

91-
public override Name ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert,
91+
public override Username ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert,
9292
JsonSerializerOptions options)
9393
{
9494
reader.ValidateToken(JsonTokenType.PropertyName);
9595

9696
return reader.GetString()!;
9797
}
9898

99-
public override void Write(Utf8JsonWriter writer, Name value, JsonSerializerOptions options)
99+
public override void Write(Utf8JsonWriter writer, Username value, JsonSerializerOptions options)
100100
{
101101
if (value?.Value is null)
102102
{
@@ -106,7 +106,7 @@ public override void Write(Utf8JsonWriter writer, Name value, JsonSerializerOpti
106106
writer.WriteStringValue(value.Value);
107107
}
108108

109-
public override void WriteAsPropertyName(Utf8JsonWriter writer, Name value, JsonSerializerOptions options)
109+
public override void WriteAsPropertyName(Utf8JsonWriter writer, Username value, JsonSerializerOptions options)
110110
{
111111
if (value?.Value is null)
112112
{

src/Elastic.Clients.Elasticsearch/_Shared/Types/GeoBounds.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,70 @@ public sealed partial class GeoBounds
1919
internal sealed class GeoBoundsConverter :
2020
JsonConverter<GeoBounds>
2121
{
22+
// Coordinates.
23+
24+
private static readonly JsonEncodedText PropBottom = JsonEncodedText.Encode("bottom");
25+
private static readonly JsonEncodedText PropLeft = JsonEncodedText.Encode("left");
26+
private static readonly JsonEncodedText PropRight = JsonEncodedText.Encode("right");
27+
private static readonly JsonEncodedText PropTop = JsonEncodedText.Encode("top");
28+
29+
// TopLeftBottomRight.
30+
31+
private static readonly JsonEncodedText PropBottomRight = JsonEncodedText.Encode("bottom_right");
32+
private static readonly JsonEncodedText PropTopLeft = JsonEncodedText.Encode("top_left");
33+
34+
// TopRightBottomLeft.
35+
36+
private static readonly JsonEncodedText PropBottomLeft = JsonEncodedText.Encode("bottom_left");
37+
private static readonly JsonEncodedText PropTopRight = JsonEncodedText.Encode("top_right");
38+
39+
// WKT.
40+
41+
private static readonly JsonEncodedText PropWkt = JsonEncodedText.Encode("wkt");
42+
2243
public override GeoBounds? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2344
{
24-
throw new InvalidOperationException();
45+
reader.ValidateToken(JsonTokenType.StartObject);
46+
47+
var readerSnapshot = reader;
48+
reader.Read();
49+
50+
GeoBounds.Kind? kind = null;
51+
if (reader.TokenType is JsonTokenType.PropertyName)
52+
{
53+
if (reader.ValueTextEquals(PropBottom) || reader.ValueTextEquals(PropLeft) ||
54+
reader.ValueTextEquals(PropRight) || reader.ValueTextEquals(PropTop))
55+
{
56+
kind = GeoBounds.Kind.Coordinates;
57+
}
58+
59+
if (reader.ValueTextEquals(PropBottomRight) || reader.ValueTextEquals(PropTopLeft))
60+
{
61+
kind = GeoBounds.Kind.TopLeftBottomRight;
62+
}
63+
64+
if (reader.ValueTextEquals(PropBottomLeft) || reader.ValueTextEquals(PropTopRight))
65+
{
66+
kind = GeoBounds.Kind.TopRightBottomLeft;
67+
}
68+
69+
if (reader.ValueTextEquals(PropWkt))
70+
{
71+
kind = GeoBounds.Kind.Wkt;
72+
}
73+
}
74+
75+
reader = readerSnapshot;
76+
77+
return kind switch
78+
{
79+
GeoBounds.Kind.Coordinates => new(reader.ReadValue<CoordsGeoBounds>(options)),
80+
GeoBounds.Kind.TopLeftBottomRight => new(reader.ReadValue<TopLeftBottomRightGeoBounds>(options)),
81+
GeoBounds.Kind.TopRightBottomLeft => new(reader.ReadValue<TopRightBottomLeftGeoBounds>(options)),
82+
GeoBounds.Kind.Wkt => new(reader.ReadValue<WktGeoBounds>(options)),
83+
null => throw new JsonException($"Unrecognized '{typeof(GeoBounds)}' variant."),
84+
_ => throw new InvalidOperationException("unreachable")
85+
};
2586
}
2687

2788
public override void Write(Utf8JsonWriter writer, GeoBounds value, JsonSerializerOptions options)

src/Elastic.Clients.Elasticsearch/_Shared/Types/GeoLocation.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,58 @@ public partial class GeoLocation
2121
internal sealed class GeoLocationConverter :
2222
JsonConverter<GeoLocation>
2323
{
24+
// LatitudeLongitude.
25+
26+
private static readonly JsonEncodedText PropLat = JsonEncodedText.Encode("lat");
27+
private static readonly JsonEncodedText PropLon = JsonEncodedText.Encode("lon");
28+
29+
// GeoHash.
30+
31+
private static readonly JsonEncodedText PropGeoHash = JsonEncodedText.Encode("geohash");
32+
2433
public override GeoLocation? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2534
{
26-
throw new InvalidOperationException();
35+
if (reader.TokenType is JsonTokenType.String)
36+
{
37+
return new GeoLocation(reader.GetString()!);
38+
}
39+
40+
if (reader.TokenType is JsonTokenType.StartArray)
41+
{
42+
return new GeoLocation(reader.ReadCollectionValue<double>(options, null)!);
43+
}
44+
45+
if (reader.TokenType is JsonTokenType.StartObject)
46+
{
47+
var readerSnapshot = reader;
48+
reader.Read();
49+
50+
GeoLocation.Kind? kind = null;
51+
if (reader.TokenType is JsonTokenType.PropertyName)
52+
{
53+
if (reader.ValueTextEquals(PropLat) || reader.ValueTextEquals(PropLon))
54+
{
55+
kind = GeoLocation.Kind.LatitudeLongitude;
56+
}
57+
58+
if (reader.ValueTextEquals(PropGeoHash))
59+
{
60+
kind = GeoLocation.Kind.GeoHash;
61+
}
62+
}
63+
64+
reader = readerSnapshot;
65+
66+
return kind switch
67+
{
68+
GeoLocation.Kind.LatitudeLongitude => new(reader.ReadValue<LatLonGeoLocation>(options)),
69+
GeoLocation.Kind.GeoHash => new(reader.ReadValue<GeoHashLocation>(options)),
70+
null => throw new JsonException($"Unrecognized '{typeof(GeoLocation)}' variant."),
71+
_ => throw new InvalidOperationException("unreachable")
72+
};
73+
}
74+
75+
throw new JsonException($"Unrecognized '{typeof(GeoLocation)}' variant.");
2776
}
2877

2978
public override void Write(Utf8JsonWriter writer, GeoLocation value, JsonSerializerOptions options)

0 commit comments

Comments
 (0)