From dbbe944b19fcae59fd014ae87e6441ff6430c951 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Thu, 17 Dec 2015 18:27:12 +0000 Subject: [PATCH] Move reflection calls until after branches that may mean they're unused A handful of cases where reflection calls are made prior to branches that might lead to the results not being used. --- .../Converters/BinaryConverter.cs | 8 ++++---- .../Converters/JavaScriptDateTimeConverter.cs | 9 +++------ .../Converters/KeyValuePairConverter.cs | 16 +++++++--------- .../Converters/StringEnumConverter.cs | 6 +++--- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs index 593f53c1..34b878b8 100644 --- a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs +++ b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs @@ -99,10 +99,6 @@ namespace Newtonsoft.Json.Converters /// The object value. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - Type t = (ReflectionUtils.IsNullableType(objectType)) - ? Nullable.GetUnderlyingType(objectType) - : objectType; - if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullable(objectType)) @@ -131,6 +127,10 @@ namespace Newtonsoft.Json.Converters throw JsonSerializationException.Create(reader, "Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } + Type t = (ReflectionUtils.IsNullableType(objectType)) + ? Nullable.GetUnderlyingType(objectType) + : objectType; + #if !NET20 if (t.AssignableToTypeName(BinaryTypeName)) { diff --git a/Src/Newtonsoft.Json/Converters/JavaScriptDateTimeConverter.cs b/Src/Newtonsoft.Json/Converters/JavaScriptDateTimeConverter.cs index 96846728..0f0e7ff4 100644 --- a/Src/Newtonsoft.Json/Converters/JavaScriptDateTimeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/JavaScriptDateTimeConverter.cs @@ -78,12 +78,6 @@ namespace Newtonsoft.Json.Converters /// The object value. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { -#if !NET20 - Type t = (ReflectionUtils.IsNullableType(objectType)) - ? Nullable.GetUnderlyingType(objectType) - : objectType; -#endif - if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullable(objectType)) @@ -118,6 +112,9 @@ namespace Newtonsoft.Json.Converters } #if !NET20 + Type t = (ReflectionUtils.IsNullableType(objectType)) + ? Nullable.GetUnderlyingType(objectType) + : objectType; if (t == typeof(DateTimeOffset)) { return new DateTimeOffset(d); diff --git a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs index 16c6b72b..7528ef86 100644 --- a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs +++ b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs @@ -80,17 +80,9 @@ namespace Newtonsoft.Json.Converters /// The object value. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - bool isNullable = ReflectionUtils.IsNullableType(objectType); - - Type t = (isNullable) - ? Nullable.GetUnderlyingType(objectType) - : objectType; - - ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t); - if (reader.TokenType == JsonToken.Null) { - if (!isNullable) + if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair."); } @@ -103,6 +95,12 @@ namespace Newtonsoft.Json.Converters ReadAndAssert(reader); + Type t = ReflectionUtils.IsNullableType(objectType) + ? Nullable.GetUnderlyingType(objectType) + : objectType; + + ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t); + while (reader.TokenType == JsonToken.PropertyName) { string propertyName = reader.Value.ToString(); diff --git a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs index b6cfc963..1a44938e 100644 --- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs +++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs @@ -106,9 +106,6 @@ namespace Newtonsoft.Json.Converters /// The object value. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - bool isNullable = ReflectionUtils.IsNullableType(objectType); - Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType; - if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) @@ -119,6 +116,9 @@ namespace Newtonsoft.Json.Converters return null; } + bool isNullable = ReflectionUtils.IsNullableType(objectType); + Type t = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType; + try { if (reader.TokenType == JsonToken.String)