22
33using GeoJSON . Text . Geometry ;
44using System ;
5+ using System . Collections . Generic ;
56using System . Text . Json ;
67using System . Text . Json . Serialization ;
78
@@ -28,10 +29,9 @@ public override bool CanConvert(Type objectType)
2829 /// <summary>
2930 /// Reads the JSON representation of the object.
3031 /// </summary>
31- /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
32- /// <param name="objectType">Type of the object.</param>
33- /// <param name="existingValue">The existing value of object being read.</param>
34- /// <param name="serializer">The calling serializer.</param>
32+ /// <param name="reader">The <see cref="T:System.Text.Json.Utf8JsonReader" /> to read from.</param>
33+ /// <param name="type">Type of the object.</param>
34+ /// <param name="options">Serializer options.</param>
3535 /// <returns>
3636 /// The object value.
3737 /// </returns>
@@ -40,17 +40,84 @@ public override IPosition Read(
4040 Type type ,
4141 JsonSerializerOptions options )
4242 {
43- double [ ] coordinates ;
44-
4543 try
46- {
47- coordinates = JsonSerializer . Deserialize < double [ ] > ( ref reader , options ) ;
44+ {
45+ if ( reader . TokenType != JsonTokenType . StartArray )
46+ {
47+ throw new ArgumentException ( "Expected start of array" ) ;
48+ }
49+
50+ double lng , lat ;
51+ double ? alt ;
52+
53+ // Read longitude
54+ if ( ! reader . Read ( ) )
55+ {
56+ throw new ArgumentException ( "Expected number, but got end of data" ) ;
57+ }
58+ if ( reader . TokenType == JsonTokenType . EndArray )
59+ {
60+ throw new ArgumentException ( "Expected 2 or 3 coordinates but got 0" ) ;
61+ }
62+ if ( reader . TokenType != JsonTokenType . Number )
63+ {
64+ throw new ArgumentException ( "Expected number but got other type" ) ;
65+ }
66+ lng = reader . GetDouble ( ) ;
67+
68+ // Read latitude
69+ if ( ! reader . Read ( ) )
70+ {
71+ throw new ArgumentException ( "Expected number, but got end of data" ) ;
72+ }
73+ if ( reader . TokenType == JsonTokenType . EndArray )
74+ {
75+ throw new ArgumentException ( "Expected 2 or 3 coordinates but got 1" ) ;
76+ }
77+ if ( reader . TokenType != JsonTokenType . Number )
78+ {
79+ throw new ArgumentException ( "Expected number but got other type" ) ;
80+ }
81+ lat = reader . GetDouble ( ) ;
82+
83+ // Read altitude, or return if end of array is found
84+ if ( ! reader . Read ( ) )
85+ {
86+ throw new ArgumentException ( "Unexpected end of data" ) ;
87+ }
88+ if ( reader . TokenType == JsonTokenType . EndArray )
89+ {
90+ return new Position ( lat , lng ) ;
91+ }
92+ else if ( reader . TokenType == JsonTokenType . Null )
93+ {
94+ alt = null ;
95+ }
96+ else if ( reader . TokenType == JsonTokenType . Number )
97+ {
98+ alt = reader . GetDouble ( ) ;
99+ }
100+ else
101+ {
102+ throw new ArgumentException ( "Expected number but got other type" ) ;
103+ }
104+
105+ // Check what comes next. Expects end of array.
106+ if ( ! reader . Read ( ) )
107+ {
108+ throw new ArgumentException ( "Expected end of array, but got end of data" ) ;
109+ }
110+ if ( reader . TokenType != JsonTokenType . EndArray )
111+ {
112+ throw new ArgumentException ( "Expected 2 or 3 coordinates but got >= 4" ) ;
113+ }
114+
115+ return new Position ( lat , lng , alt ) ;
48116 }
49117 catch ( Exception e )
50118 {
51119 throw new JsonException ( "Error parsing coordinates" , e ) ;
52120 }
53- return coordinates ? . ToPosition ( ) ?? throw new JsonException ( "Coordinates cannot be null" ) ;
54121 }
55122
56123 /// <summary>
0 commit comments