C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
CachedContractResolver.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Linq;
5using System.Reflection;
6
7using Newtonsoft.Json;
8using Newtonsoft.Json.Serialization;
9
11{
19 public class CachedContractResolver : DefaultContractResolver
20 {
21 private readonly ConcurrentDictionary<Type, List<MemberInfo>> _cachedBaseSerializableMembers =
22 new ConcurrentDictionary<Type, List<MemberInfo>>();
23
24 private readonly ConcurrentDictionary<Type, List<MemberInfo>> _cachedFilteredSerializableMembers =
25 new ConcurrentDictionary<Type, List<MemberInfo>>();
26
29 public Func<MemberInfo, bool> AllowMember { get; }
30
36 public CachedContractResolver(Func<MemberInfo, bool> allowMember = null)
37 {
38 AllowMember = allowMember;
39 }
40
45 protected sealed override List<MemberInfo> GetSerializableMembers(Type objectType) =>
46 AllowMember == null ? GetBaseSerializableMembersWithCaching(objectType) :
47 GetFilteredSerializableMembersWithCaching(objectType);
48
52 protected sealed override JsonProperty CreateProperty(MemberInfo member,
53 MemberSerialization memberSerialization) =>
54 AllowMember(member) ? base.CreateProperty(member, memberSerialization) : null;
55
58 private List<MemberInfo> GetBaseSerializableMembersWithCaching(Type objectType) =>
59 _cachedBaseSerializableMembers.GetOrAdd(objectType, type => base.GetSerializableMembers(type));
60
63 private List<MemberInfo> GetFilteredSerializableMembersWithCaching(Type objectType) =>
64 _cachedFilteredSerializableMembers.GetOrAdd(objectType, type =>
65 GetBaseSerializableMembersWithCaching(type).Where(AllowMember).ToList());
66 }
67}
A wrapper for the basic DefaultContractResolver which caches the set of members to be serialized for ...
CachedContractResolver(Func< MemberInfo, bool > allowMember=null)
Construct a CachedContractResolver, optionally configured to return a filtered subset of the base ser...
Func< MemberInfo, bool > AllowMember
If specified on construction, only members that return true for this predicate will be serialized and...
override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
Instantiates a JsonProperty for the specified member if it has not been excluded by the AllowMember p...
override List< MemberInfo > GetSerializableMembers(Type objectType)
Gets the serializable members for the specified type, filtered by the configured predicate (if any)....