-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjEnvelopeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using NetTopologySuite.Geometries; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace NetTopologySuite.IO.Converters | ||
{ | ||
internal class StjEnvelopeConverter : JsonConverter<Envelope> | ||
{ | ||
public override Envelope Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
Envelope res = null; | ||
|
||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
// #57: callers expect us to have read past the last token | ||
reader.Read(); | ||
} | ||
else | ||
{ | ||
reader.ReadToken(JsonTokenType.StartArray); | ||
|
||
double minX = reader.GetDouble(); | ||
reader.Read(); | ||
double minY = reader.GetDouble(); | ||
reader.Read(); | ||
double maxX = reader.GetDouble(); | ||
reader.Read(); | ||
double maxY = reader.GetDouble(); | ||
reader.Read(); | ||
|
||
if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
maxX = maxY; | ||
maxY = reader.GetDouble(); | ||
reader.Read(); | ||
reader.Read(); | ||
} | ||
|
||
reader.ReadToken(JsonTokenType.EndArray); | ||
|
||
res = new Envelope(minX, maxX, minY, maxY); | ||
} | ||
|
||
//reader.Read(); // move away from array end | ||
return res; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Envelope value, JsonSerializerOptions options) | ||
{ | ||
// if we don't want to write "null" bounding boxes, bail out. | ||
if (value?.IsNull != false) | ||
{ | ||
writer.WriteNullValue(); | ||
|
||
return; | ||
} | ||
|
||
writer.WriteStartArray(); | ||
writer.WriteNumberValue(value.MinX); | ||
writer.WriteNumberValue(value.MinY); | ||
writer.WriteNumberValue(value.MaxX); | ||
writer.WriteNumberValue(value.MaxY); | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters