Skip to content

Commit

Permalink
Obey JsonSerializerOptions.DefaultIgnoreCondition as well as IgnoreNu…
Browse files Browse the repository at this point in the history
…llValues.

This is technically a breaking change: consumers now MUST upgrade their System.Text.Json package reference.
  • Loading branch information
airbreather committed Apr 30, 2022
1 parent bd873b5 commit c2df4bb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.IO.Converters
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<!-- Set these very early in the process, before Directory.Build.props is imported. -->
<PropertyGroup Label="Version numbers">
<!-- MAJOR, MINOR, and PATCH are defined according to SemVer 2.0.0. -->
<NtsMajorVersion Condition=" '$(NtsMajorVersion)' == '' ">2</NtsMajorVersion>
<NtsMinorVersion Condition=" '$(NtsMinorVersion)' == '' ">1</NtsMinorVersion>
<NtsPatchVersion Condition=" '$(NtsPatchVersion)' == '' ">1</NtsPatchVersion>
<NtsMajorVersion Condition=" '$(NtsMajorVersion)' == '' ">3</NtsMajorVersion>
<NtsMinorVersion Condition=" '$(NtsMinorVersion)' == '' ">0</NtsMinorVersion>
<NtsPatchVersion Condition=" '$(NtsPatchVersion)' == '' ">0</NtsPatchVersion>
</PropertyGroup>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
Expand Down Expand Up @@ -34,7 +34,7 @@
<ItemGroup>
<PackageReference Include="NetTopologySuite" Version="[2.0.0, 3.0.0-A)" />
<PackageReference Include="NetTopologySuite.Features" Version="[2.1.0, 3.0.0-A)" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<PackageReference Include="System.Text.Json" Version="6.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
71 changes: 45 additions & 26 deletions test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue100.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<JsonSerializerOptions> 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
Expand Down
4 changes: 2 additions & 2 deletions test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue98.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit c2df4bb

Please sign in to comment.