Skip to content

Commit

Permalink
#46 fixed self-referencing deserialization of attribute tables
Browse files Browse the repository at this point in the history
  • Loading branch information
DGuidi committed Jan 23, 2020
1 parent 60dc589 commit 915f37e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
/// </returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return InternalReadJson(reader, serializer);
return InternalReadJson(reader, serializer, false);
}

private static IList<object> InternalReadJsonArray(JsonReader reader, JsonSerializer serializer)
Expand All @@ -87,7 +87,7 @@ private static IList<object> InternalReadJsonArray(JsonReader reader, JsonSerial
switch (reader.TokenType)
{
case JsonToken.StartObject:
res.Add(InternalReadJson(reader, serializer));
res.Add(InternalReadJson(reader, serializer, true));
Debug.Assert(reader.TokenType == JsonToken.EndObject);
// advance
reader.Read();
Expand Down Expand Up @@ -122,7 +122,8 @@ private static IList<object> InternalReadJsonArray(JsonReader reader, JsonSerial
return res;
}

private static object InternalReadJson(JsonReader reader, JsonSerializer serializer)
private static object InternalReadJson(JsonReader reader, JsonSerializer serializer,
bool innerObject)
{
//// TODO: refactor to remove check when reading TopoJSON
//if (reader.TokenType == JsonToken.StartArray)
Expand All @@ -149,7 +150,7 @@ private static object InternalReadJson(JsonReader reader, JsonSerializer seriali
reader.SkipComments();

IAttributesTable attributesTable = null;
if (serializer.Context.Context is IFeature feature)
if (!innerObject && serializer.Context.Context is IFeature feature)
{
attributesTable = feature.Attributes;
}
Expand All @@ -169,7 +170,7 @@ private static object InternalReadJson(JsonReader reader, JsonSerializer seriali
if (reader.TokenType == JsonToken.StartObject)
{
// inner object
attributeValue = InternalReadJson(reader, serializer);
attributeValue = InternalReadJson(reader, serializer, true);
if (reader.TokenType != JsonToken.EndObject)
{
throw new ArgumentException("Expected token '}' not found.");
Expand Down
65 changes: 65 additions & 0 deletions test/NetTopologySuite.IO.GeoJSON.Test/Issue46Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using NetTopologySuite.Features;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NetTopologySuite.IO.GeoJSON.Test
{
[TestFixture]
public class Issue46Fixture
{
[Test, Category("Issue46")]
public void test_deserialize_nested_geojson()
{
#pragma warning disable CS0219 // Variable is assigned but its value is never used
const string sample = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"id\":\"63a72ea5-45d6-4d4c-a77c-948e5c814317\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[1,0],[1,1],[0,0]]]},\"properties\":{\"a\":[],\"b\":[],\"c\":0.15403640270233154,\"d\":0.15403640270233154,\"e\":null,\"f\":null,\"g\":null,\"h\":null,\"i\":0,\"n\":\"2018-05-08T00:00:00\",\"o\":{\"a1\":\"2018-09-14T00:00:00\",\"b1\":86400,\"c1\":\"R6\",\"d1\":4,\"e1\":12.47,\"f1\":1563.25,\"g1\":129,\"h1\":1,\"i1\":0.23666,\"j1\":0.00056,\"k1\":0.8}}}]}";
#pragma warning restore CS0219 // Variable is assigned but its value is never used
const string formatted =
@"
{
""type"": ""FeatureCollection"",
""features"": [{
""type"": ""Feature"",
""id"": ""63a72ea5-45d6-4d4c-a77c-948e5c814317"",
""geometry"": {
""type"": ""Polygon"",
""coordinates"": [[[0, 0], [1, 0], [1, 1], [0, 0]]]
},
""properties"": {
""a"": 0,
""b"": {
""a1"": 1
},
""c"": [""s1"", ""s2""],
}
}
]
}";

var reader = new GeoJsonReader();
var coll = reader.Read<FeatureCollection>(formatted);

Assert.AreEqual(1, coll.Count);
var feature = coll.Single();
var attributes = feature.Attributes;
Assert.NotNull(attributes);
Assert.AreEqual(4, attributes.Count);
Assert.IsNotNull(attributes["id"]);
Assert.IsNotNull(attributes["a"]);
Assert.AreEqual(0, attributes["a"]);
Assert.IsNotNull(attributes["b"]);
Assert.IsInstanceOf(typeof(AttributesTable), attributes["b"]);
var inner = (AttributesTable)attributes["b"];
Assert.AreEqual(1, inner.Count);
Assert.IsNotNull(inner["a1"]);
Assert.AreEqual(1, inner["a1"]);
Assert.IsNotNull(attributes["c"]);
Assert.IsInstanceOf(typeof(List<object>), attributes["c"]);
var list = (List<object>)attributes["c"];
Assert.AreEqual(2, list.Count);
Assert.IsTrue(list.All(i => i is string));
Assert.IsTrue(list.All(i => !String.IsNullOrEmpty(i as string)));
}
}
}

0 comments on commit 915f37e

Please sign in to comment.