@@ -19,9 +19,70 @@ public sealed partial class GeoBounds
1919internal 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 )
0 commit comments