Skip to content
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 @@ -156,14 +156,17 @@ internal void ClearCaches()
internal sealed class CachingContext
{
private readonly ConcurrentDictionary<Type, JsonTypeInfo?> _jsonTypeInfoCache = new();
#if !NETCOREAPP
private readonly Func<Type, JsonTypeInfo?> _jsonTypeInfoFactory;
#endif

public CachingContext(JsonSerializerOptions options, int hashCode)
{
Options = options;
HashCode = hashCode;

_jsonTypeInfoFactory = options.GetTypeInfoNoCaching;
#if !NETCOREAPP
_jsonTypeInfoFactory = Options.GetTypeInfoNoCaching;
#endif
}

public JsonSerializerOptions Options { get; }
Expand All @@ -172,7 +175,12 @@ public CachingContext(JsonSerializerOptions options, int hashCode)
// If changing please ensure that src/ILLink.Descriptors.LibraryBuild.xml is up-to-date.
public int Count => _jsonTypeInfoCache.Count;

public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory);
public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) =>
#if NETCOREAPP
_jsonTypeInfoCache.GetOrAdd(type, static (type, options) => options.GetTypeInfoNoCaching(type), Options);
#else
_jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory);
#endif

public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo);

Expand Down