-Use static nametable in camel case contract resolver

This commit is contained in:
James Newton-King
2017-01-29 16:10:43 +13:00
parent 1914b91bdb
commit 744be1a712
4 changed files with 24 additions and 6 deletions
@@ -48,12 +48,17 @@ namespace Newtonsoft.Json.Tests.Serialization
public void EnsureContractsShared() public void EnsureContractsShared()
{ {
CamelCasePropertyNamesContractResolver resolver1 = new CamelCasePropertyNamesContractResolver(); CamelCasePropertyNamesContractResolver resolver1 = new CamelCasePropertyNamesContractResolver();
var contract1 = resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); var contract1 = (JsonObjectContract)resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests));
CamelCasePropertyNamesContractResolver resolver2 = new CamelCasePropertyNamesContractResolver(); CamelCasePropertyNamesContractResolver resolver2 = new CamelCasePropertyNamesContractResolver();
var contract2 = resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); var contract2 = (JsonObjectContract)resolver2.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests));
Assert.IsTrue(ReferenceEquals(contract1, contract2)); Assert.IsTrue(ReferenceEquals(contract1, contract2));
var nt1 = resolver1.GetNameTable();
var nt2 = resolver2.GetNameTable();
Assert.IsTrue(ReferenceEquals(nt1, nt2));
} }
[Test] [Test]
+1 -1
View File
@@ -951,7 +951,7 @@ namespace Newtonsoft.Json
DefaultContractResolver resolver = _contractResolver as DefaultContractResolver; DefaultContractResolver resolver = _contractResolver as DefaultContractResolver;
if (resolver != null) if (resolver != null)
{ {
textReader.NameTable = resolver.NameTable; textReader.NameTable = resolver.GetNameTable();
} }
} }
} }
@@ -68,6 +68,7 @@ namespace Newtonsoft.Json.Serialization
public class CamelCasePropertyNamesContractResolver : DefaultContractResolver public class CamelCasePropertyNamesContractResolver : DefaultContractResolver
{ {
private static readonly object TypeContractCacheLock = new object(); private static readonly object TypeContractCacheLock = new object();
private static readonly PropertyNameTable NameTable = new PropertyNameTable();
private static Dictionary<ResolverContractKey, JsonContract> _contractCache; private static Dictionary<ResolverContractKey, JsonContract> _contractCache;
/// <summary> /// <summary>
@@ -117,5 +118,10 @@ namespace Newtonsoft.Json.Serialization
return contract; return contract;
} }
internal override PropertyNameTable GetNameTable()
{
return NameTable;
}
} }
} }
@@ -85,7 +85,7 @@ namespace Newtonsoft.Json.Serialization
}; };
private readonly object _typeContractCacheLock = new object(); private readonly object _typeContractCacheLock = new object();
internal readonly PropertyNameTable NameTable = new PropertyNameTable(); private readonly PropertyNameTable _nameTable = new PropertyNameTable();
private Dictionary<Type, JsonContract> _contractCache; private Dictionary<Type, JsonContract> _contractCache;
@@ -1239,6 +1239,8 @@ namespace Newtonsoft.Json.Serialization
throw new JsonSerializationException("Null collection of serializable members returned."); throw new JsonSerializationException("Null collection of serializable members returned.");
} }
PropertyNameTable nameTable = GetNameTable();
JsonPropertyCollection properties = new JsonPropertyCollection(type); JsonPropertyCollection properties = new JsonPropertyCollection(type);
foreach (MemberInfo member in members) foreach (MemberInfo member in members)
@@ -1248,9 +1250,9 @@ namespace Newtonsoft.Json.Serialization
if (property != null) if (property != null)
{ {
// nametable is not thread-safe for multiple writers // nametable is not thread-safe for multiple writers
lock (NameTable) lock (nameTable)
{ {
property.PropertyName = NameTable.Add(property.PropertyName); property.PropertyName = nameTable.Add(property.PropertyName);
} }
properties.AddProperty(property); properties.AddProperty(property);
@@ -1261,6 +1263,11 @@ namespace Newtonsoft.Json.Serialization
return orderedProperties; return orderedProperties;
} }
internal virtual PropertyNameTable GetNameTable()
{
return _nameTable;
}
/// <summary> /// <summary>
/// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member. /// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member.
/// </summary> /// </summary>