CamelCasePropertyNamesContractResolver
Resolves member mappings for a type, camel casing property names.
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Serialization
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class CamelCasePropertyNamesContractResolver : DefaultContractResolver
{
private static readonly object TypeContractCacheLock = new object();
private static readonly DefaultJsonNameTable NameTable = new DefaultJsonNameTable();
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
1,
1
})]
private static Dictionary<StructMultiKey<Type, Type>, JsonContract> _contractCache;
public CamelCasePropertyNamesContractResolver()
{
base.NamingStrategy = new CamelCaseNamingStrategy {
ProcessDictionaryKeys = true,
OverrideSpecifiedNames = true
};
}
public override JsonContract ResolveContract(Type type)
{
if ((object)type == null)
throw new ArgumentNullException("type");
StructMultiKey<Type, Type> key = new StructMultiKey<Type, Type>(GetType(), type);
Dictionary<StructMultiKey<Type, Type>, JsonContract> contractCache = _contractCache;
if (contractCache == null || !contractCache.TryGetValue(key, out JsonContract value)) {
value = CreateContract(type);
lock (TypeContractCacheLock) {
contractCache = _contractCache;
Dictionary<StructMultiKey<Type, Type>, JsonContract> obj = (contractCache != null) ? new Dictionary<StructMultiKey<Type, Type>, JsonContract>(contractCache) : new Dictionary<StructMultiKey<Type, Type>, JsonContract>();
obj[key] = value;
_contractCache = obj;
return value;
}
}
return value;
}
internal override DefaultJsonNameTable GetNameTable()
{
return NameTable;
}
}
}