From 7a57cee2672ef11366732dded74588af9c0e782c Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Tue, 22 Sep 2020 16:19:35 +1000 Subject: [PATCH 1/2] Don't use Serializer context Relates:elastic/elasticsearch-net#4791 This commit updates the AttributesTableConverter and FeatureConverter to not use the serializer context when deserializing AttributesTable. The serializer context is not safe to use multithreaded, which can be the case when JsonConverters from NetTopologySuite.IO.GeoJson are added to an singleton instance of JsonSerializer not under the control of NetTopologySuite.IO.GeoJson. --- .gitignore | 1 + .../Converters/AttributesTableConverter.cs | 12 +---------- .../Converters/FeatureConverter.cs | 21 +++++++++++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index b26d879..3a87ac3 100644 --- a/.gitignore +++ b/.gitignore @@ -203,3 +203,4 @@ NetTopologySuite.Samples.Shapefiles/tmp*.shx NetTopologySuite.Samples.Shapefiles/tmp*.dbf NetTopologySuite.Samples.Shapefiles/test_arcview.shp NetTopologySuite.Samples.Shapefiles/test_buffer.shp +.idea/ diff --git a/src/NetTopologySuite.IO.GeoJSON/Converters/AttributesTableConverter.cs b/src/NetTopologySuite.IO.GeoJSON/Converters/AttributesTableConverter.cs index cce5200..8443fbe 100644 --- a/src/NetTopologySuite.IO.GeoJSON/Converters/AttributesTableConverter.cs +++ b/src/NetTopologySuite.IO.GeoJSON/Converters/AttributesTableConverter.cs @@ -148,20 +148,10 @@ private static object InternalReadJson(JsonReader reader, JsonSerializer seriali // Advance reader reader.Read(); reader.SkipComments(); - - IAttributesTable attributesTable = null; - if (!innerObject && serializer.Context.Context is IFeature feature) - { - attributesTable = feature.Attributes; - } + var attributesTable = new AttributesTable(); if (reader.TokenType != JsonToken.Null) { - if (attributesTable is null) - { - attributesTable = new AttributesTable(); - } - while (reader.TokenType == JsonToken.PropertyName) { string attributeName = (string)reader.Value; diff --git a/src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs b/src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs index 5ca4305..416bbd1 100644 --- a/src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs +++ b/src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs @@ -173,10 +173,23 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist throw new ArgumentException("Expected token '{' not found."); } - var context = serializer.Context; - serializer.Context = new StreamingContext(serializer.Context.State, feature); - feature.Attributes = serializer.Deserialize(reader); - serializer.Context = context; + var attributes = serializer.Deserialize(reader); + + if (feature.Attributes is null) + { + feature.Attributes = attributes; + } + else + { + foreach (var attribute in attributes) + { + if (!feature.Attributes.Exists(attribute.Key)) + { + feature.Attributes.Add(attribute.Key, attribute.Value); + } + } + } + if (reader.TokenType != JsonToken.EndObject) { throw new ArgumentException("Expected token '}' not found."); From 2f5abea0e04d9020264e872ef16d11cc18b5e37a Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Wed, 23 Sep 2020 20:12:33 +1000 Subject: [PATCH 2/2] Add gitignore for JetBrains Rider --- .gitignore | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 3a87ac3..215ac3f 100644 --- a/.gitignore +++ b/.gitignore @@ -188,6 +188,9 @@ UpgradeLog*.htm # Microsoft Fakes FakesAssemblies/ +# JetBrains Rider +.idea/ + # Data generated from unit tests NetTopologySuite.Samples.Shapefiles/ZMtest.shp NetTopologySuite.Samples.Shapefiles/ZMtest.shx @@ -202,5 +205,4 @@ NetTopologySuite.Samples.Shapefiles/tmp*.shp NetTopologySuite.Samples.Shapefiles/tmp*.shx NetTopologySuite.Samples.Shapefiles/tmp*.dbf NetTopologySuite.Samples.Shapefiles/test_arcview.shp -NetTopologySuite.Samples.Shapefiles/test_buffer.shp -.idea/ +NetTopologySuite.Samples.Shapefiles/test_buffer.shp \ No newline at end of file