Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a JsonException that gets thrown when there are ignored properties under some circumstances. #144

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override FeatureCollection Read(ref Utf8JsonReader reader, Type objectTyp
else
{
reader.ReadOrThrow();
reader.Skip();
reader.SkipOrThrow();
reader.ReadOrThrow();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public override IFeature Read(ref Utf8JsonReader reader, Type objectType, JsonSe

default:
// If property name is not one of the above: skip it entirely (foreign member)
reader.Skip();
reader.SkipOrThrow();
// Advance
while (reader.Read())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override Geometry Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
break;
default: // included "bbox" property
//read, but can't do anything with it (see NetTopologySuite.IO.GeoJSON => NetTopologySuite.IO.Converters.GeometryConverter.ParseGeometry)
reader.Skip();
reader.SkipOrThrow();
reader.Read();
break;
}
Expand Down
19 changes: 19 additions & 0 deletions src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ internal static void AssertToken(this ref Utf8JsonReader reader, JsonTokenType r
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void SkipOrThrow(this ref Utf8JsonReader reader)
{
// #143: Even though JsonConverter<T> always seems to see a Utf8JsonReader positioned on
// a full node, that node might be contained in a larger incomplete block. For whatever
// reason, reader.Skip() throws JsonException immediately if the reader is in a partial
// block, even if the current block has plenty of data to read past it. TrySkip will do
// the right thing, but we should still check the return value to make sure that we get
// a JsonException if the reader *does* terminate abruptly (airbreather 2024-04-24).
if (!reader.TrySkip())
{
ThrowForUnexpectedPartialJson();
}
}

internal static object ObjectFromJsonNode(JsonNode node, JsonSerializerOptions serializerOptions)
{
switch (node)
Expand Down Expand Up @@ -93,6 +108,10 @@ internal static JsonNode ObjectToJsonNode(object obj, JsonSerializerOptions seri
private static void ThrowForUnexpectedEndOfStream()
=> throw new JsonException(Resources.EX_UnexpectedEndOfStream);

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowForUnexpectedPartialJson()
=> throw new JsonException(Resources.EX_UnexpectedPartialJson);

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowForUnexpectedToken(JsonTokenType requiredNextTokenType, ref Utf8JsonReader reader)
=> throw new JsonException(string.Format(Resources.EX_UnexpectedToken, requiredNextTokenType, reader.TokenType, CurrentTokenAsString(in reader)));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/NetTopologySuite.IO.GeoJSON4STJ/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@
<data name="EX_UnexpectedToken" xml:space="preserve">
<value>Expected token is '{0}' but was '{1}' (Value '{2}').</value>
</data>
<data name="EX_UnexpectedPartialJson" xml:space="preserve">
<value>JsonConverter received partial JSON. This is likely the result of a bug in the System.Text.Json library.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
Expand Down Expand Up @@ -68,5 +70,20 @@ public void TestSanD(OgcGeometryType type, int num, bool threeD)
for (int i = 0; i < fc.Count; i++)
FeatureConverterTest.CheckEquality(fc[i], d[i]);
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(new FeatureCollection(), options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<FeatureCollection[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -128,6 +129,21 @@ public void WriteJsonWithIdTest(string idPropertyName, object id)
CheckEquality(value, deserialized, idPropertyName);
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(new Feature(), options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<IFeature[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}

public static IEnumerable<object[]> FeatureIdTestCases
{
get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using NetTopologySuite.Geometries;
using NUnit.Framework;
Expand Down Expand Up @@ -184,5 +185,20 @@ public void TestWriteReadWkt(string wkt)

Assert.That(geomS.IsEmpty ? geomD.IsEmpty : geomS.EqualsTopologically(geomD));
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(Point.Empty, options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<Geometry[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NetTopologySuite.IO.GeoJSON4STJ.Test;

/// <summary>
/// A <see cref="MemoryStream"/> implementation that will never read more than one byte at a time.
/// </summary>
internal sealed class SingleByteReadingMemoryStream : MemoryStream
{
public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, Math.Min(1, count));
}

public override int Read(Span<byte> buffer)
{
return base.Read(buffer[.. Math.Min(1, buffer.Length)]);
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return base.ReadAsync(buffer, offset, Math.Min(1, count), cancellationToken);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
return base.ReadAsync(buffer[.. Math.Min(1, buffer.Length)], cancellationToken);
}
}
Loading