diff --git a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs
index 207af23..130599a 100644
--- a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs
+++ b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs
@@ -51,14 +51,14 @@ public override void Write(Utf8JsonWriter writer, IFeature value, JsonSerializer
StjGeometryConverter.WriteBBox(writer, value.BoundingBox, options);
// geometry
- if (value.Geometry != null || !options.IgnoreNullValues)
+ if (value.Geometry != null || options.ShouldWriteNullValues())
{
writer.WritePropertyName("geometry");
JsonSerializer.Serialize(writer, value.Geometry, options);
}
// properties
- if (value.Attributes != null || !options.IgnoreNullValues)
+ if (value.Attributes != null || options.ShouldWriteNullValues())
{
writer.WritePropertyName("properties");
JsonSerializer.Serialize(writer, value.Attributes, options);
diff --git a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjGeometryConverter.Envelope.cs b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjGeometryConverter.Envelope.cs
index c829487..75e5004 100644
--- a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjGeometryConverter.Envelope.cs
+++ b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjGeometryConverter.Envelope.cs
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
+using System.Text.Json.Serialization;
using NetTopologySuite.Geometries;
namespace NetTopologySuite.IO.Converters
@@ -54,18 +55,18 @@ internal static Envelope ReadBBox(ref Utf8JsonReader reader, JsonSerializerOptio
internal static void WriteBBox(Utf8JsonWriter writer, Envelope value, JsonSerializerOptions options)
{
// if we don't want to write "null" bounding boxes, bail out.
- if ((value?.IsNull != false) && options.IgnoreNullValues)
- return;
-
- value = value ?? new Envelope();
-
- writer.WritePropertyName("bbox");
- if (value.IsNull)
+ if (value?.IsNull != false)
{
- writer.WriteNullValue();
+ if (options.ShouldWriteNullValues())
+ {
+ writer.WriteNull("bbox");
+ }
+
return;
}
+ writer.WritePropertyName("bbox");
+
writer.WriteStartArray();
writer.WriteNumberValue(value.MinX);
writer.WriteNumberValue(value.MinY);
diff --git a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
index f74f8a6..b8d5fb8 100644
--- a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
+++ b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
@@ -1,11 +1,19 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
+using System.Text.Json.Serialization;
using NetTopologySuite.IO.Properties;
namespace NetTopologySuite.IO.Converters
{
internal static class Utility
{
+ internal static bool ShouldWriteNullValues(this JsonSerializerOptions options)
+ {
+#pragma warning disable SYSLIB0020
+ return options.DefaultIgnoreCondition == JsonIgnoreCondition.Never && !options.IgnoreNullValues;
+#pragma warning restore SYSLIB0020
+ }
+
internal static void SkipComments(this ref Utf8JsonReader reader)
{
// Skip comments
diff --git a/src/NetTopologySuite.IO.GeoJSON4STJ/NetTopologySuite.IO.GeoJSON4STJ.csproj b/src/NetTopologySuite.IO.GeoJSON4STJ/NetTopologySuite.IO.GeoJSON4STJ.csproj
index 24aca70..d20ae4c 100644
--- a/src/NetTopologySuite.IO.GeoJSON4STJ/NetTopologySuite.IO.GeoJSON4STJ.csproj
+++ b/src/NetTopologySuite.IO.GeoJSON4STJ/NetTopologySuite.IO.GeoJSON4STJ.csproj
@@ -4,9 +4,9 @@
- 2
- 1
- 1
+ 3
+ 0
+ 0
@@ -34,7 +34,7 @@
-
+
diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs
index b0a2c11..ef55457 100644
--- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs
+++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs
@@ -59,7 +59,7 @@ public void TestSanD(OgcGeometryType type, int num, bool threeD)
}
var options = DefaultOptions;
- options.IgnoreNullValues = true;
+ options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
string json = ToJsonString(fc, options);
var d = Deserialize(json, options);
diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs
index 7baaee1..a886d66 100644
--- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs
+++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs
@@ -59,7 +59,7 @@ public void WriteJsonTest()
IFeature value = new Feature(new Point(23.1, 56.2), attributes);
var options = DefaultOptions;
options.WriteIndented = false;
- options.IgnoreNullValues = true;
+ options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
//GeoJsonConverterFactory.OrdinateFormatString = "0.{}";
string json = ToJsonString(value, options);
@@ -80,7 +80,7 @@ public void WriteJsonWithArrayTest()
IFeature value = new Feature(new Point(23.1, 56.2), attributes);
var options = DefaultOptions;
options.WriteIndented = false;
- options.IgnoreNullValues = true;
+ options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
string json = ToJsonString(value, options);
var deserialized = Deserialize(json, options);
diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue100.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue100.cs
index bc06f1b..833183a 100644
--- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue100.cs
+++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue100.cs
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
+using System.Text.Json.Serialization;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Converters;
@@ -33,33 +34,51 @@ public sealed class Issue100
private static void DoBBoxTest(Geometry g, bool writeBBOX, bool ignoreNull)
{
- Assert.That(g, Is.Not.Null);
- var options = new JsonSerializerOptions()
+ if (ignoreNull)
{
- IgnoreNullValues = ignoreNull,
- Converters = { new GeoJsonConverterFactory(GF, writeBBOX) }
- };
- string geomJson = JsonSerializer.Serialize(g, options);
- Console.WriteLine($"GEOM: {geomJson}");
- TestJsonWithValidBBox(geomJson, g, writeBBOX, ignoreNull);
-
- var feature = new Feature(g, new AttributesTable { { "id", 1 }, { "test", "2" } });
- string featureJson = JsonSerializer.Serialize(feature, options);
- Console.WriteLine($"FEAT: {featureJson}");
- TestJsonWithNullBBox(featureJson, g, writeBBOX, ignoreNull);
- feature.BoundingBox = g.EnvelopeInternal;
- string featureJsonBBox = JsonSerializer.Serialize(feature, options);
- Console.WriteLine($"FEAT+BBox: {featureJsonBBox}");
- TestJsonWithValidBBox(featureJsonBBox, g, writeBBOX, ignoreNull);
-
- var featureColl = new FeatureCollection { feature };
- string featureCollJson = JsonSerializer.Serialize(featureColl, options);
- Console.WriteLine($"COLL: {featureCollJson}");
- TestJsonWithNullBBox(featureCollJson, g, writeBBOX, ignoreNull);
- featureColl.BoundingBox = feature.BoundingBox;
- string featureCollJsonBBox = JsonSerializer.Serialize(featureColl, options);
- Console.WriteLine($"COLL+BBox: {featureCollJsonBBox}");
- TestJsonWithValidBBox(featureCollJsonBBox, g, writeBBOX, ignoreNull);
+ Run(options => options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault);
+ Run(options => options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);
+
+#pragma warning disable SYSLIB0020
+ Run(options => options.IgnoreNullValues = true);
+#pragma warning restore SYSLIB0020
+ }
+ else
+ {
+ Run(options => options.DefaultIgnoreCondition = JsonIgnoreCondition.Never);
+ }
+
+ void Run(Action setIgnoreNull)
+ {
+ Assert.That(g, Is.Not.Null);
+ var options = new JsonSerializerOptions()
+ {
+ Converters = { new GeoJsonConverterFactory(GF, writeBBOX) }
+ };
+ setIgnoreNull(options);
+
+ string geomJson = JsonSerializer.Serialize(g, options);
+ Console.WriteLine($"GEOM: {geomJson}");
+ TestJsonWithValidBBox(geomJson, g, writeBBOX, ignoreNull);
+
+ var feature = new Feature(g, new AttributesTable { { "id", 1 }, { "test", "2" } });
+ string featureJson = JsonSerializer.Serialize(feature, options);
+ Console.WriteLine($"FEAT: {featureJson}");
+ TestJsonWithNullBBox(featureJson, g, writeBBOX, ignoreNull);
+ feature.BoundingBox = g.EnvelopeInternal;
+ string featureJsonBBox = JsonSerializer.Serialize(feature, options);
+ Console.WriteLine($"FEAT+BBox: {featureJsonBBox}");
+ TestJsonWithValidBBox(featureJsonBBox, g, writeBBOX, ignoreNull);
+
+ var featureColl = new FeatureCollection { feature };
+ string featureCollJson = JsonSerializer.Serialize(featureColl, options);
+ Console.WriteLine($"COLL: {featureCollJson}");
+ TestJsonWithNullBBox(featureCollJson, g, writeBBOX, ignoreNull);
+ featureColl.BoundingBox = feature.BoundingBox;
+ string featureCollJsonBBox = JsonSerializer.Serialize(featureColl, options);
+ Console.WriteLine($"COLL+BBox: {featureCollJsonBBox}");
+ TestJsonWithValidBBox(featureCollJsonBBox, g, writeBBOX, ignoreNull);
+ }
}
// NOTE: feature (and feature coll) bbox is always NULL
diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue98.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue98.cs
index d46edd6..ec64ec5 100644
--- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue98.cs
+++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue98.cs
@@ -2,6 +2,7 @@
using System.IO;
using System.Text;
using System.Text.Json;
+using System.Text.Json.Serialization;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Converters;
using NUnit.Framework;
@@ -121,7 +122,7 @@ public void TestSerializeEmptyPointCustomSettingsNullsIgnored()
var settings = new JsonSerializerOptions()
{
Converters = { new GeoJsonConverterFactory(fac) },
- IgnoreNullValues = true
+ DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
string json = JsonSerializer.Serialize(fac.CreatePoint(), settings);
Console.WriteLine(json);
@@ -135,7 +136,6 @@ public void TestSerializeEmptyPointCustomSettingsNullsIncluded()
var settings = new JsonSerializerOptions()
{
Converters = { new GeoJsonConverterFactory(fac) },
- IgnoreNullValues = false
};
string json = JsonSerializer.Serialize(fac.CreatePoint(), settings);
Console.WriteLine(json);