diff --git a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs index 74df13b5..620c0deb 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs @@ -48,12 +48,17 @@ namespace Newtonsoft.Json.Tests.Serialization public void EnsureContractsShared() { CamelCasePropertyNamesContractResolver resolver1 = new CamelCasePropertyNamesContractResolver(); - var contract1 = resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); + var contract1 = (JsonObjectContract)resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); CamelCasePropertyNamesContractResolver resolver2 = new CamelCasePropertyNamesContractResolver(); - var contract2 = resolver1.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); + var contract2 = (JsonObjectContract)resolver2.ResolveContract(typeof(CamelCasePropertyNamesContractResolverTests)); Assert.IsTrue(ReferenceEquals(contract1, contract2)); + + var nt1 = resolver1.GetNameTable(); + var nt2 = resolver2.GetNameTable(); + + Assert.IsTrue(ReferenceEquals(nt1, nt2)); } [Test] diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 27927f87..bf44e23d 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -951,7 +951,7 @@ namespace Newtonsoft.Json DefaultContractResolver resolver = _contractResolver as DefaultContractResolver; if (resolver != null) { - textReader.NameTable = resolver.NameTable; + textReader.NameTable = resolver.GetNameTable(); } } } diff --git a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs index 8a7088e4..d093f622 100644 --- a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs @@ -68,6 +68,7 @@ namespace Newtonsoft.Json.Serialization public class CamelCasePropertyNamesContractResolver : DefaultContractResolver { private static readonly object TypeContractCacheLock = new object(); + private static readonly PropertyNameTable NameTable = new PropertyNameTable(); private static Dictionary _contractCache; /// @@ -117,5 +118,10 @@ namespace Newtonsoft.Json.Serialization return contract; } + + internal override PropertyNameTable GetNameTable() + { + return NameTable; + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 351d5198..6d33d155 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -85,7 +85,7 @@ namespace Newtonsoft.Json.Serialization }; private readonly object _typeContractCacheLock = new object(); - internal readonly PropertyNameTable NameTable = new PropertyNameTable(); + private readonly PropertyNameTable _nameTable = new PropertyNameTable(); private Dictionary _contractCache; @@ -1239,6 +1239,8 @@ namespace Newtonsoft.Json.Serialization throw new JsonSerializationException("Null collection of serializable members returned."); } + PropertyNameTable nameTable = GetNameTable(); + JsonPropertyCollection properties = new JsonPropertyCollection(type); foreach (MemberInfo member in members) @@ -1248,9 +1250,9 @@ namespace Newtonsoft.Json.Serialization if (property != null) { // 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); @@ -1261,6 +1263,11 @@ namespace Newtonsoft.Json.Serialization return orderedProperties; } + internal virtual PropertyNameTable GetNameTable() + { + return _nameTable; + } + /// /// Creates the used by the serializer to get and set values from a member. ///