Skip to content

Commit

Permalink
Don't use Serializer context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
russcam committed Sep 22, 2020
1 parent 5a5fa15 commit 7a57cee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,10 @@ private static IList<object> InternalReadJsonArray(JsonReader reader, JsonSerial
// 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;
Expand Down
21 changes: 17 additions & 4 deletions src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributesTable>(reader);
serializer.Context = context;
var attributes = serializer.Deserialize<AttributesTable>(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.");
Expand Down

0 comments on commit 7a57cee

Please sign in to comment.