Replace is-and-cast with as-and-nullcheck

Gives slightly better performance, so applying across the board
adds up.
This commit is contained in:
Jon Hanna
2017-01-05 01:52:04 +00:00
parent 306c3a812b
commit a95665b481
16 changed files with 136 additions and 109 deletions
+3 -2
View File
@@ -203,9 +203,10 @@ namespace Newtonsoft.Json.Bson
{ {
if (_parent != null) if (_parent != null)
{ {
if (_parent is BsonObject) BsonObject bo = _parent as BsonObject;
if (bo != null)
{ {
((BsonObject)_parent).Add(_propertyName, token); bo.Add(_propertyName, token);
_propertyName = null; _propertyName = null;
} }
else else
@@ -311,9 +311,8 @@ namespace Newtonsoft.Json.Converters
{ {
get get
{ {
XmlNode node = (_node is XmlAttribute) XmlAttribute attribute = _node as XmlAttribute;
? ((XmlAttribute)_node).OwnerElement XmlNode node = attribute != null ? attribute.OwnerElement : _node.ParentNode;
: _node.ParentNode;
if (node == null) if (node == null)
{ {
@@ -727,42 +726,55 @@ namespace Newtonsoft.Json.Converters
internal static IXmlNode WrapNode(XObject node) internal static IXmlNode WrapNode(XObject node)
{ {
if (node is XDocument) XDocument document = node as XDocument;
if (document != null)
{ {
return new XDocumentWrapper((XDocument)node); return new XDocumentWrapper(document);
} }
else if (node is XElement)
XElement element = node as XElement;
if (element != null)
{ {
return new XElementWrapper((XElement)node); return new XElementWrapper(element);
} }
else if (node is XContainer)
XContainer container = node as XContainer;
if (container != null)
{ {
return new XContainerWrapper((XContainer)node); return new XContainerWrapper(container);
} }
else if (node is XProcessingInstruction)
XProcessingInstruction pi = node as XProcessingInstruction;
if (pi != null)
{ {
return new XProcessingInstructionWrapper((XProcessingInstruction)node); return new XProcessingInstructionWrapper(pi);
} }
else if (node is XText)
XText text = node as XText;
if (text != null)
{ {
return new XTextWrapper((XText)node); return new XTextWrapper(text);
} }
else if (node is XComment)
XComment comment = node as XComment;
if (comment != null)
{ {
return new XCommentWrapper((XComment)node); return new XCommentWrapper(comment);
} }
else if (node is XAttribute)
XAttribute attribute = node as XAttribute;
if (attribute != null)
{ {
return new XAttributeWrapper((XAttribute)node); return new XAttributeWrapper(attribute);
} }
else if (node is XDocumentType)
XDocumentType type = node as XDocumentType;
if (type != null)
{ {
return new XDocumentTypeWrapper((XDocumentType)node); return new XDocumentTypeWrapper(type);
}
else
{
return new XObjectWrapper(node);
} }
return new XObjectWrapper(node);
} }
public override IXmlNode AppendChild(IXmlNode newChild) public override IXmlNode AppendChild(IXmlNode newChild)
@@ -1034,15 +1046,17 @@ namespace Newtonsoft.Json.Converters
private IXmlNode WrapXml(object value) private IXmlNode WrapXml(object value)
{ {
#if !NET20 #if !NET20
if (value is XObject) XObject xObject = value as XObject;
if (xObject != null)
{ {
return XContainerWrapper.WrapNode((XObject)value); return XContainerWrapper.WrapNode(xObject);
} }
#endif #endif
#if !(DOTNET || PORTABLE) #if !(DOTNET || PORTABLE)
if (value is XmlNode) XmlNode node = value as XmlNode;
if (node != null)
{ {
return XmlNodeWrapper.WrapNode((XmlNode)value); return XmlNodeWrapper.WrapNode(node);
} }
#endif #endif
+5 -7
View File
@@ -483,17 +483,15 @@ namespace Newtonsoft.Json
if (Value != null) if (Value != null)
{ {
string s; string s;
if (Value is IFormattable) IFormattable formattable = Value as IFormattable;
if (formattable != null)
{ {
s = ((IFormattable)Value).ToString(null, Culture); s = formattable.ToString(null, Culture);
}
else if (Value is Uri)
{
s = ((Uri)Value).OriginalString;
} }
else else
{ {
s = Value.ToString(); Uri uri = Value as Uri;
s = uri != null ? uri.OriginalString : Value.ToString();
} }
SetToken(JsonToken.String, s, false); SetToken(JsonToken.String, s, false);
+5 -8
View File
@@ -1576,28 +1576,25 @@ namespace Newtonsoft.Json
#endif #endif
default: default:
#if !PORTABLE #if !PORTABLE
if (value is IConvertible) IConvertible convertible = value as IConvertible;
if (convertible != null)
{ {
// the value is a non-standard IConvertible // the value is a non-standard IConvertible
// convert to the underlying value and retry // convert to the underlying value and retry
IConvertible convertable = (IConvertible)value;
TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable); TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertible);
// if convertible has an underlying typecode of Object then attempt to convert it to a string // if convertible has an underlying typecode of Object then attempt to convert it to a string
PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode; PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
Type resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type; Type resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof(string) : typeInformation.Type;
object convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture); object convertedValue = convertible.ToType(resolvedType, CultureInfo.InvariantCulture);
WriteValue(writer, resolvedTypeCode, convertedValue); WriteValue(writer, resolvedTypeCode, convertedValue);
break; break;
} }
else
#endif #endif
{ throw CreateUnsupportedTypeException(writer, value);
throw CreateUnsupportedTypeException(writer, value);
}
} }
} }
@@ -89,7 +89,7 @@ namespace Newtonsoft.Json.Linq
/// <param name="value">The new value.</param> /// <param name="value">The new value.</param>
public override void SetValue(object component, object value) public override void SetValue(object component, object value)
{ {
JToken token = (value is JToken) ? (JToken)value : new JValue(value); JToken token = value as JToken ?? new JValue(value);
CastInstance(component)[Name] = token; CastInstance(component)[Name] = token;
} }
+23 -12
View File
@@ -520,9 +520,11 @@ namespace Newtonsoft.Json.Linq
{ {
return (DateTimeOffset)v.Value; return (DateTimeOffset)v.Value;
} }
if (v.Value is string)
string s = v.Value as string;
if (s != null)
{ {
return DateTimeOffset.Parse((string)v.Value, CultureInfo.InvariantCulture); return DateTimeOffset.Parse(s, CultureInfo.InvariantCulture);
} }
return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture)); return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture));
} }
@@ -634,10 +636,13 @@ namespace Newtonsoft.Json.Linq
{ {
return (DateTimeOffset?)v.Value; return (DateTimeOffset?)v.Value;
} }
if (v.Value is string)
string s = v.Value as string;
if (s != null)
{ {
return DateTimeOffset.Parse((string)v.Value, CultureInfo.InvariantCulture); return DateTimeOffset.Parse(s, CultureInfo.InvariantCulture);
} }
return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture)); return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture));
} }
#endif #endif
@@ -1237,10 +1242,13 @@ namespace Newtonsoft.Json.Linq
{ {
return null; return null;
} }
if (v.Value is byte[])
byte[] bytes = v.Value as byte[];
if (bytes != null)
{ {
return Convert.ToBase64String((byte[])v.Value); return Convert.ToBase64String(bytes);
} }
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_1 #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_1
if (v.Value is BigInteger) if (v.Value is BigInteger)
{ {
@@ -1328,9 +1336,10 @@ namespace Newtonsoft.Json.Linq
} }
#endif #endif
if (v.Value is byte[]) byte[] bytes = v.Value as byte[];
if (bytes != null)
{ {
return (byte[])v.Value; return bytes;
} }
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
@@ -1349,9 +1358,10 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
} }
if (v.Value is byte[]) byte[] bytes = v.Value as byte[];
if (bytes != null)
{ {
return new Guid((byte[])v.Value); return new Guid(bytes);
} }
return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
@@ -1380,9 +1390,10 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
if (v.Value is byte[]) byte[] bytes = v.Value as byte[];
if (bytes != null)
{ {
return new Guid((byte[])v.Value); return new Guid(bytes);
} }
return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
+2 -8
View File
@@ -227,14 +227,8 @@ namespace Newtonsoft.Json.Linq
break; break;
case JTokenType.Uri: case JTokenType.Uri:
object v = ((JValue)token).Value; object v = ((JValue)token).Value;
if (v is Uri) Uri uri = v as Uri;
{ SetToken(JsonToken.String, uri != null ? uri.OriginalString : SafeToString(v));
SetToken(JsonToken.String, ((Uri)v).OriginalString);
}
else
{
SetToken(JsonToken.String, SafeToString(v));
}
break; break;
case JTokenType.TimeSpan: case JTokenType.TimeSpan:
SetToken(JsonToken.String, SafeToString(((JValue)token).Value)); SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
+4 -2
View File
@@ -1004,7 +1004,8 @@ namespace Newtonsoft.Json.Linq
public override bool TryBinaryOperation(JValue instance, BinaryOperationBinder binder, object arg, out object result) public override bool TryBinaryOperation(JValue instance, BinaryOperationBinder binder, object arg, out object result)
{ {
object compareValue = (arg is JValue) ? ((JValue)arg).Value : arg; JValue value = arg as JValue;
object compareValue = value != null ? value.Value : arg;
switch (binder.Operation) switch (binder.Operation)
{ {
@@ -1055,7 +1056,8 @@ namespace Newtonsoft.Json.Linq
return 1; return 1;
} }
object otherValue = (obj is JValue) ? ((JValue)obj).Value : obj; JValue value = obj as JValue;
object otherValue = value != null ? value.Value : obj;
return Compare(_valueType, _value, otherValue); return Compare(_valueType, _value, otherValue);
} }
@@ -35,19 +35,18 @@ namespace Newtonsoft.Json.Serialization
private BidirectionalDictionary<string, object> GetMappings(object context) private BidirectionalDictionary<string, object> GetMappings(object context)
{ {
JsonSerializerInternalBase internalSerializer; JsonSerializerInternalBase internalSerializer = context as JsonSerializerInternalBase;
if (internalSerializer == null)
if (context is JsonSerializerInternalBase)
{ {
internalSerializer = (JsonSerializerInternalBase)context; JsonSerializerProxy proxy = context as JsonSerializerProxy;
} if (proxy != null)
else if (context is JsonSerializerProxy) {
{ internalSerializer = proxy.GetInternalSerializer();
internalSerializer = ((JsonSerializerProxy)context).GetInternalSerializer(); }
} else
else {
{ throw new JsonException("The DefaultReferenceResolver can only be used internally.");
throw new JsonException("The DefaultReferenceResolver can only be used internally."); }
} }
return internalSerializer.DefaultReferenceMappings; return internalSerializer.DefaultReferenceMappings;
@@ -1320,7 +1320,8 @@ namespace Newtonsoft.Json.Serialization
} }
JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty); JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty);
PrimitiveTypeCode keyTypeCode = (contract.KeyContract is JsonPrimitiveContract) ? ((JsonPrimitiveContract)contract.KeyContract).TypeCode : PrimitiveTypeCode.Empty; JsonPrimitiveContract keyContract = contract.KeyContract as JsonPrimitiveContract;
PrimitiveTypeCode keyTypeCode = (keyContract != null) ? keyContract.TypeCode : PrimitiveTypeCode.Empty;
bool finished = false; bool finished = false;
do do
@@ -403,9 +403,10 @@ namespace Newtonsoft.Json.Serialization
} }
#endif #endif
if (value is Type) type = value as Type;
if (type != null)
{ {
s = ((Type)value).AssemblyQualifiedName; s = type.AssemblyQualifiedName;
return true; return true;
} }
@@ -268,9 +268,10 @@ namespace Newtonsoft.Json.Utilities
} }
object v = currentArray[0]; object v = currentArray[0];
if (v is IList) IList list = v as IList;
if (list != null)
{ {
currentArray = (IList)v; currentArray = list;
} }
else else
{ {
@@ -52,9 +52,10 @@ namespace Newtonsoft.Json.Utilities
{ {
ValidationUtils.ArgumentNotNull(list, nameof(list)); ValidationUtils.ArgumentNotNull(list, nameof(list));
if (list is ICollection<T>) ICollection<T> collection = list as ICollection<T>;
if (collection != null)
{ {
_genericCollection = (ICollection<T>)list; _genericCollection = collection;
} }
else else
{ {
+15 -8
View File
@@ -318,10 +318,13 @@ namespace Newtonsoft.Json.Utilities
{ {
return (BigInteger)value; return (BigInteger)value;
} }
if (value is string)
string s = value as string;
if (s != null)
{ {
return BigInteger.Parse((string)value, CultureInfo.InvariantCulture); return BigInteger.Parse(s, CultureInfo.InvariantCulture);
} }
if (value is float) if (value is float)
{ {
return new BigInteger((float)value); return new BigInteger((float)value);
@@ -350,9 +353,11 @@ namespace Newtonsoft.Json.Utilities
{ {
return new BigInteger((ulong)value); return new BigInteger((ulong)value);
} }
if (value is byte[])
byte[] bytes = value as byte[];
if (bytes != null)
{ {
return new BigInteger((byte[])value); return new BigInteger(bytes);
} }
throw new InvalidCastException("Cannot convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); throw new InvalidCastException("Cannot convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
@@ -487,9 +492,10 @@ namespace Newtonsoft.Json.Utilities
} }
#endif #endif
if (initialValue is byte[] && targetType == typeof(Guid)) byte[] bytes = initialValue as byte[];
if (bytes != null && targetType == typeof(Guid))
{ {
value = new Guid((byte[])initialValue); value = new Guid(bytes);
return ConvertResult.Success; return ConvertResult.Success;
} }
@@ -587,9 +593,10 @@ namespace Newtonsoft.Json.Utilities
} }
#endif #endif
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
if (initialValue is INullable) INullable nullable = initialValue as INullable;
if (nullable != null)
{ {
value = EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType); value = EnsureTypeAssignable(ToValue(nullable), initialType, targetType);
return ConvertResult.Success; return ConvertResult.Success;
} }
#endif #endif
@@ -139,9 +139,9 @@ namespace Newtonsoft.Json.Utilities
callExpression = Expression.Call(readParameter, (MethodInfo)method, argsExpression); callExpression = Expression.Call(readParameter, (MethodInfo)method, argsExpression);
} }
if (method is MethodInfo) MethodInfo m = method as MethodInfo;
if (m != null)
{ {
MethodInfo m = (MethodInfo)method;
if (m.ReturnType != typeof(void)) if (m.ReturnType != typeof(void))
{ {
callExpression = EnsureCastExpression(callExpression, type); callExpression = EnsureCastExpression(callExpression, type);
@@ -762,11 +762,11 @@ namespace Newtonsoft.Json.Utilities
// http://hyperthink.net/blog/getcustomattributes-gotcha/ // http://hyperthink.net/blog/getcustomattributes-gotcha/
// ICustomAttributeProvider doesn't do inheritance // ICustomAttributeProvider doesn't do inheritance
if (provider is Type) Type t = provider as Type;
if (t != null)
{ {
Type t = (Type)provider; object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
object[] a = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); Attribute[] attributes = array.Cast<Attribute>().ToArray();
Attribute[] attributes = a.Cast<Attribute>().ToArray();
#if (NET20 || NET35) #if (NET20 || NET35)
// ye olde .NET GetCustomAttributes doesn't respect the inherit argument // ye olde .NET GetCustomAttributes doesn't respect the inherit argument
@@ -779,29 +779,29 @@ namespace Newtonsoft.Json.Utilities
return attributes; return attributes;
} }
if (provider is Assembly) Assembly a = provider as Assembly;
if (a != null)
{ {
Assembly a = (Assembly)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a); return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);
} }
if (provider is MemberInfo) MemberInfo mi = provider as MemberInfo;
if (mi != null)
{ {
MemberInfo m = (MemberInfo)provider; return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit);
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
} }
#if !PORTABLE40 #if !PORTABLE40
if (provider is Module) Module m = provider as Module;
if (m != null)
{ {
Module m = (Module)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit); return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
} }
#endif #endif
if (provider is ParameterInfo) ParameterInfo p = provider as ParameterInfo;
if (p != null)
{ {
ParameterInfo p = (ParameterInfo)provider;
return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit); return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
} }