Skip to content

Commit

Permalink
Merge pull request #1074 from hypar-io/make-deserialization-thread-safe
Browse files Browse the repository at this point in the history
Make-deserialization-thread-safe (#1074)
  • Loading branch information
anthonie-kramer authored Dec 13, 2023
2 parents 001c0f6 + 77ebc6a commit 3ac8386
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Elements/src/Serialization/JSON/JsonInheritanceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ public static Dictionary<Guid, Element> Elements
}
}

private static List<string> _deserializationWarnings = new List<string>();
[System.ThreadStatic]
private static List<string> _deserializationWarnings;

public static List<string> DeserializationWarnings
{
get
{
if (_deserializationWarnings == null)
{
_deserializationWarnings = new List<string>();
}
return _deserializationWarnings;
}
}

public JsonInheritanceConverter()
{
Expand Down Expand Up @@ -247,8 +260,8 @@ public override bool CanConvert(System.Type objectType)

public static List<string> GetAndClearDeserializationWarnings()
{
var warnings = _deserializationWarnings.ToList();
_deserializationWarnings.Clear();
var warnings = DeserializationWarnings.ToList();
DeserializationWarnings.Clear();
return warnings;
}

Expand All @@ -261,7 +274,7 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type o
var id = Guid.Parse(reader.Value.ToString());
if (!Elements.ContainsKey(id))
{
_deserializationWarnings.Add($"Element {id} was not found during deserialization. Check for other deserialization errors.");
DeserializationWarnings.Add($"Element {id} was not found during deserialization. Check for other deserialization errors.");
return null;
}
return Elements[id];
Expand Down Expand Up @@ -318,7 +331,7 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type o

if (discriminator != null)
{
_deserializationWarnings.Add($"An object with the discriminator, {discriminator}, could not be deserialized. {baseMessage}");
DeserializationWarnings.Add($"An object with the discriminator, {discriminator}, could not be deserialized. {baseMessage}");
return null;

}
Expand Down

0 comments on commit 3ac8386

Please sign in to comment.