-Performance

This commit is contained in:
James Newton-King
2013-04-01 23:51:40 +13:00
parent 33a5604707
commit cb7ac1dfb4
7 changed files with 22 additions and 24 deletions
+10 -10
View File
@@ -140,19 +140,19 @@ namespace Newtonsoft.Json.Tests
[Test] [Test]
public void Deserialize() public void Deserialize()
{ {
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractSerializer, XmlText); //BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractSerializer, XmlText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.BinaryFormatter, MiscellaneousUtils.HexToBytes(BinaryFormatterHex)); //BenchmarkDeserializeMethod<TestClass>(SerializeMethod.BinaryFormatter, MiscellaneousUtils.HexToBytes(BinaryFormatterHex));
DeserializeTests<TestClass>(JsonText); DeserializeTests<TestClass>(JsonText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText); //BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetWithIsoConverter, JsonIsoText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex)); //BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex));
} }
public void DeserializeTests<T>(string json) public void DeserializeTests<T>(string json)
{ {
BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json); //BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json);
BenchmarkDeserializeMethod<T>(SerializeMethod.DataContractJsonSerializer, json); //BenchmarkDeserializeMethod<T>(SerializeMethod.DataContractJsonSerializer, json);
BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNet, json); BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNet, json);
BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNetManual, json); //BenchmarkDeserializeMethod<T>(SerializeMethod.JsonNetManual, json);
} }
[Test] [Test]
@@ -729,9 +729,9 @@ If attributes are not mentioned, default values are used in each case.
Type type = typeof (T); Type type = typeof (T);
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; //serializer.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; //serializer.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
if (isoDateTimeConverter) if (isoDateTimeConverter)
serializer.Converters.Add(new IsoDateTimeConverter()); serializer.Converters.Add(new IsoDateTimeConverter());
@@ -65,7 +65,6 @@ namespace Newtonsoft.Json.Serialization
internal bool ShouldCreateWrapper { get; private set; } internal bool ShouldCreateWrapper { get; private set; }
internal bool CanDeserialize { get; private set; } internal bool CanDeserialize { get; private set; }
internal bool IsReadOnlyOrFixedSize { get; private set; }
internal ConstructorInfo ParametrizedConstructor { get; private set; } internal ConstructorInfo ParametrizedConstructor { get; private set; }
/// <summary> /// <summary>
@@ -73,6 +73,7 @@ namespace Newtonsoft.Json.Serialization
internal Type NonNullableUnderlyingType; internal Type NonNullableUnderlyingType;
internal ReadType InternalReadType; internal ReadType InternalReadType;
internal JsonContractType ContractType; internal JsonContractType ContractType;
internal bool IsReadOnlyOrFixedSize;
/// <summary> /// <summary>
/// Gets the underlying type for the contract. /// Gets the underlying type for the contract.
@@ -68,7 +68,6 @@ namespace Newtonsoft.Json.Serialization
private Func<object> _genericTemporaryDictionaryCreator; private Func<object> _genericTemporaryDictionaryCreator;
internal bool ShouldCreateWrapper { get; private set; } internal bool ShouldCreateWrapper { get; private set; }
internal bool IsReadOnlyOrFixedSize { get; private set; }
internal ConstructorInfo ParametrizedConstructor { get; private set; } internal ConstructorInfo ParametrizedConstructor { get; private set; }
/// <summary> /// <summary>
@@ -48,6 +48,7 @@ namespace Newtonsoft.Json.Serialization
ContractType = JsonContractType.Primitive; ContractType = JsonContractType.Primitive;
TypeCode = ConvertUtils.GetTypeCode(underlyingType); TypeCode = ConvertUtils.GetTypeCode(underlyingType);
IsReadOnlyOrFixedSize = true;
} }
} }
} }
@@ -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 // attempt to convert value's type to target's type
if (valueType != targetType) if (valueType != targetType)
{ {
if (value == null && contract.IsNullable)
return null;
try try
{ {
if (value == null && contract.IsNullable)
return null;
if (contract.IsConvertable) if (contract.IsConvertable)
{ {
JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract; 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; return true;
} }
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
ObjectCreationHandling objectCreationHandling = ObjectCreationHandling objectCreationHandling =
property.ObjectCreationHandling.GetValueOrDefault(Serializer.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) && (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject)
&& property.Readable) && property.Readable)
{ {
currentValue = property.ValueProvider.GetValue(target); currentValue = property.ValueProvider.GetValue(target);
gottenCurrentValue = true; gottenCurrentValue = true;
useExistingValue = (currentValue != null useExistingValue = (currentValue != null && !property.PropertyContract.IsReadOnlyOrFixedSize && !property.PropertyType.IsValueType());
&& !property.PropertyType.IsArray
&& !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof (ReadOnlyCollection<>))
&& !property.PropertyType.IsValueType());
} }
if (!property.Writable && !useExistingValue) 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; return true;
} }
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
if (currentValue == null) if (currentValue == null)
{ {
propertyContract = property.PropertyContract; propertyContract = property.PropertyContract;
@@ -85,8 +85,9 @@ namespace Newtonsoft.Json.Serialization
private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) 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); bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
if (includeTypeDetails) if (includeTypeDetails)
{ {