Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Cache Version and FrameworkName in json deserialization #785

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;

namespace Microsoft.Fx.Portability.Utils.JsonConverters
{
internal class JsonToStringConverter<T> : JsonConverter<T>
{
private readonly ConcurrentDictionary<string, T> _cache;
private readonly Func<string, T> _factory;

public JsonToStringConverter(Func<string, T> factory)
{
_cache = new ConcurrentDictionary<string, T>(StringComparer.Ordinal);
_factory = factory;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = serializer.Deserialize<JToken>(reader).ToString();

return string.IsNullOrEmpty(value) ? default(T) : _factory(value);
return string.IsNullOrEmpty(value) ? default : _cache.GetOrAdd(value, _factory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still want "default(T)" instead of "default", right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand Down