diff --git a/src/lib/Microsoft.Fx.Portability/Utils/JsonConverters/JsonToStringConverter.cs b/src/lib/Microsoft.Fx.Portability/Utils/JsonConverters/JsonToStringConverter.cs index 553e2233e..873e840de 100644 --- a/src/lib/Microsoft.Fx.Portability/Utils/JsonConverters/JsonToStringConverter.cs +++ b/src/lib/Microsoft.Fx.Portability/Utils/JsonConverters/JsonToStringConverter.cs @@ -4,15 +4,18 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; +using System.Collections.Concurrent; namespace Microsoft.Fx.Portability.Utils.JsonConverters { internal class JsonToStringConverter : JsonConverter { + private readonly ConcurrentDictionary _cache; private readonly Func _factory; public JsonToStringConverter(Func factory) { + _cache = new ConcurrentDictionary(StringComparer.Ordinal); _factory = factory; } @@ -20,7 +23,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist { var value = serializer.Deserialize(reader).ToString(); - return string.IsNullOrEmpty(value) ? default(T) : _factory(value); + return string.IsNullOrEmpty(value) ? default : _cache.GetOrAdd(value, _factory); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)