From cb7ac1dfb4f016661f09126747cbad558d148397 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 1 Apr 2013 23:51:40 +1300 Subject: [PATCH] -Performance --- Src/Newtonsoft.Json.Tests/PerformanceTests.cs | 20 +++++++++---------- .../Serialization/JsonArrayContract.cs | 1 - .../Serialization/JsonContract.cs | 1 + .../Serialization/JsonDictionaryContract.cs | 1 - .../Serialization/JsonPrimitiveContract.cs | 1 + .../JsonSerializerInternalReader.cs | 19 ++++++++---------- .../JsonSerializerInternalWriter.cs | 3 ++- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index 591f86f8..1e525635 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -140,19 +140,19 @@ namespace Newtonsoft.Json.Tests [Test] public void Deserialize() { - BenchmarkDeserializeMethod(SerializeMethod.DataContractSerializer, XmlText); - BenchmarkDeserializeMethod(SerializeMethod.BinaryFormatter, MiscellaneousUtils.HexToBytes(BinaryFormatterHex)); + //BenchmarkDeserializeMethod(SerializeMethod.DataContractSerializer, XmlText); + //BenchmarkDeserializeMethod(SerializeMethod.BinaryFormatter, MiscellaneousUtils.HexToBytes(BinaryFormatterHex)); DeserializeTests(JsonText); - BenchmarkDeserializeMethod(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText); - BenchmarkDeserializeMethod(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex)); + //BenchmarkDeserializeMethod(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText); + //BenchmarkDeserializeMethod(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex)); } public void DeserializeTests(string json) { - BenchmarkDeserializeMethod(SerializeMethod.JavaScriptSerializer, json); - BenchmarkDeserializeMethod(SerializeMethod.DataContractJsonSerializer, json); + //BenchmarkDeserializeMethod(SerializeMethod.JavaScriptSerializer, json); + //BenchmarkDeserializeMethod(SerializeMethod.DataContractJsonSerializer, json); BenchmarkDeserializeMethod(SerializeMethod.JsonNet, json); - BenchmarkDeserializeMethod(SerializeMethod.JsonNetManual, json); + //BenchmarkDeserializeMethod(SerializeMethod.JsonNetManual, json); } [Test] @@ -729,9 +729,9 @@ If attributes are not mentioned, default values are used in each case. Type type = typeof (T); JsonSerializer serializer = new JsonSerializer(); - serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; - serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; - serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + //serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; + //serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; + //serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; if (isoDateTimeConverter) serializer.Converters.Add(new IsoDateTimeConverter()); diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 12aaaac1..ed948493 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -65,7 +65,6 @@ namespace Newtonsoft.Json.Serialization internal bool ShouldCreateWrapper { get; private set; } internal bool CanDeserialize { get; private set; } - internal bool IsReadOnlyOrFixedSize { get; private set; } internal ConstructorInfo ParametrizedConstructor { get; private set; } /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index dccf6667..edb91e07 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -73,6 +73,7 @@ namespace Newtonsoft.Json.Serialization internal Type NonNullableUnderlyingType; internal ReadType InternalReadType; internal JsonContractType ContractType; + internal bool IsReadOnlyOrFixedSize; /// /// Gets the underlying type for the contract. diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index 3bde0bde..0ec48de6 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -68,7 +68,6 @@ namespace Newtonsoft.Json.Serialization private Func _genericTemporaryDictionaryCreator; internal bool ShouldCreateWrapper { get; private set; } - internal bool IsReadOnlyOrFixedSize { get; private set; } internal ConstructorInfo ParametrizedConstructor { get; private set; } /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs index 07222762..35c2973b 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs @@ -48,6 +48,7 @@ namespace Newtonsoft.Json.Serialization ContractType = JsonContractType.Primitive; TypeCode = ConvertUtils.GetTypeCode(underlyingType); + IsReadOnlyOrFixedSize = true; } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 20346993..5be873ed 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -686,11 +686,11 @@ To fix this error either change the JSON to a {1} or change the deserialized typ // attempt to convert value's type to target's type if (valueType != targetType) { + if (value == null && contract.IsNullable) + return null; + try { - if (value == null && contract.IsNullable) - return null; - if (contract.IsConvertable) { JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract; @@ -778,20 +778,20 @@ To fix this error either change the JSON to a {1} or change the deserialized typ return true; } + if (property.PropertyContract == null) + property.PropertyContract = GetContractSafe(property.PropertyType); + ObjectCreationHandling objectCreationHandling = property.ObjectCreationHandling.GetValueOrDefault(Serializer.ObjectCreationHandling); - if ((objectCreationHandling == ObjectCreationHandling.Auto || objectCreationHandling == ObjectCreationHandling.Reuse) + if ((objectCreationHandling != ObjectCreationHandling.Replace) && (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject) && property.Readable) { currentValue = property.ValueProvider.GetValue(target); gottenCurrentValue = true; - useExistingValue = (currentValue != null - && !property.PropertyType.IsArray - && !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof (ReadOnlyCollection<>)) - && !property.PropertyType.IsValueType()); + useExistingValue = (currentValue != null && !property.PropertyContract.IsReadOnlyOrFixedSize && !property.PropertyType.IsValueType()); } if (!property.Writable && !useExistingValue) @@ -816,9 +816,6 @@ To fix this error either change the JSON to a {1} or change the deserialized typ return true; } - if (property.PropertyContract == null) - property.PropertyContract = GetContractSafe(property.PropertyType); - if (currentValue == null) { propertyContract = property.PropertyContract; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index af5d6ffb..b1d6e8e0 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -85,8 +85,9 @@ namespace Newtonsoft.Json.Serialization private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) { - if (contract.UnderlyingType == typeof (byte[])) + if (contract.TypeCode == PrimitiveTypeCode.Bytes) { + // if type name handling is enabled then wrap the base64 byte string in an object with the type name bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty); if (includeTypeDetails) {