Replace is-and-cast with as-and-nullcheck
Gives slightly better performance, so applying across the board adds up.
This commit is contained in:
@@ -203,9 +203,10 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
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;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -311,9 +311,8 @@ namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
get
|
||||
{
|
||||
XmlNode node = (_node is XmlAttribute)
|
||||
? ((XmlAttribute)_node).OwnerElement
|
||||
: _node.ParentNode;
|
||||
XmlAttribute attribute = _node as XmlAttribute;
|
||||
XmlNode node = attribute != null ? attribute.OwnerElement : _node.ParentNode;
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
@@ -727,42 +726,55 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new XObjectWrapper(node);
|
||||
return new XDocumentTypeWrapper(type);
|
||||
}
|
||||
|
||||
return new XObjectWrapper(node);
|
||||
}
|
||||
|
||||
public override IXmlNode AppendChild(IXmlNode newChild)
|
||||
@@ -1034,15 +1046,17 @@ namespace Newtonsoft.Json.Converters
|
||||
private IXmlNode WrapXml(object value)
|
||||
{
|
||||
#if !NET20
|
||||
if (value is XObject)
|
||||
XObject xObject = value as XObject;
|
||||
if (xObject != null)
|
||||
{
|
||||
return XContainerWrapper.WrapNode((XObject)value);
|
||||
return XContainerWrapper.WrapNode(xObject);
|
||||
}
|
||||
#endif
|
||||
#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
|
||||
|
||||
|
||||
@@ -483,17 +483,15 @@ namespace Newtonsoft.Json
|
||||
if (Value != null)
|
||||
{
|
||||
string s;
|
||||
if (Value is IFormattable)
|
||||
IFormattable formattable = Value as IFormattable;
|
||||
if (formattable != null)
|
||||
{
|
||||
s = ((IFormattable)Value).ToString(null, Culture);
|
||||
}
|
||||
else if (Value is Uri)
|
||||
{
|
||||
s = ((Uri)Value).OriginalString;
|
||||
s = formattable.ToString(null, Culture);
|
||||
}
|
||||
else
|
||||
{
|
||||
s = Value.ToString();
|
||||
Uri uri = Value as Uri;
|
||||
s = uri != null ? uri.OriginalString : Value.ToString();
|
||||
}
|
||||
|
||||
SetToken(JsonToken.String, s, false);
|
||||
|
||||
@@ -1576,28 +1576,25 @@ namespace Newtonsoft.Json
|
||||
#endif
|
||||
default:
|
||||
#if !PORTABLE
|
||||
if (value is IConvertible)
|
||||
IConvertible convertible = value as IConvertible;
|
||||
if (convertible != null)
|
||||
{
|
||||
// the value is a non-standard IConvertible
|
||||
// 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
|
||||
PrimitiveTypeCode resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
|
||||
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);
|
||||
break;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
throw CreateUnsupportedTypeException(writer, value);
|
||||
}
|
||||
throw CreateUnsupportedTypeException(writer, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="value">The new value.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -520,9 +520,11 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
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));
|
||||
}
|
||||
@@ -634,10 +636,13 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
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));
|
||||
}
|
||||
#endif
|
||||
@@ -1237,10 +1242,13 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
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 (v.Value is BigInteger)
|
||||
{
|
||||
@@ -1328,9 +1336,10 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
#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)));
|
||||
@@ -1349,9 +1358,10 @@ namespace Newtonsoft.Json.Linq
|
||||
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));
|
||||
@@ -1380,9 +1390,10 @@ namespace Newtonsoft.Json.Linq
|
||||
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));
|
||||
|
||||
@@ -227,14 +227,8 @@ namespace Newtonsoft.Json.Linq
|
||||
break;
|
||||
case JTokenType.Uri:
|
||||
object v = ((JValue)token).Value;
|
||||
if (v is Uri)
|
||||
{
|
||||
SetToken(JsonToken.String, ((Uri)v).OriginalString);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetToken(JsonToken.String, SafeToString(v));
|
||||
}
|
||||
Uri uri = v as Uri;
|
||||
SetToken(JsonToken.String, uri != null ? uri.OriginalString : SafeToString(v));
|
||||
break;
|
||||
case JTokenType.TimeSpan:
|
||||
SetToken(JsonToken.String, SafeToString(((JValue)token).Value));
|
||||
|
||||
@@ -1004,7 +1004,8 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -1055,7 +1056,8 @@ namespace Newtonsoft.Json.Linq
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -35,19 +35,18 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private BidirectionalDictionary<string, object> GetMappings(object context)
|
||||
{
|
||||
JsonSerializerInternalBase internalSerializer;
|
||||
|
||||
if (context is JsonSerializerInternalBase)
|
||||
JsonSerializerInternalBase internalSerializer = context as JsonSerializerInternalBase;
|
||||
if (internalSerializer == null)
|
||||
{
|
||||
internalSerializer = (JsonSerializerInternalBase)context;
|
||||
}
|
||||
else if (context is JsonSerializerProxy)
|
||||
{
|
||||
internalSerializer = ((JsonSerializerProxy)context).GetInternalSerializer();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonException("The DefaultReferenceResolver can only be used internally.");
|
||||
JsonSerializerProxy proxy = context as JsonSerializerProxy;
|
||||
if (proxy != null)
|
||||
{
|
||||
internalSerializer = proxy.GetInternalSerializer();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonException("The DefaultReferenceResolver can only be used internally.");
|
||||
}
|
||||
}
|
||||
|
||||
return internalSerializer.DefaultReferenceMappings;
|
||||
|
||||
@@ -1320,7 +1320,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
|
||||
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;
|
||||
do
|
||||
|
||||
@@ -403,9 +403,10 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
#endif
|
||||
|
||||
if (value is Type)
|
||||
type = value as Type;
|
||||
if (type != null)
|
||||
{
|
||||
s = ((Type)value).AssemblyQualifiedName;
|
||||
s = type.AssemblyQualifiedName;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -268,9 +268,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
|
||||
object v = currentArray[0];
|
||||
if (v is IList)
|
||||
IList list = v as IList;
|
||||
if (list != null)
|
||||
{
|
||||
currentArray = (IList)v;
|
||||
currentArray = list;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -52,9 +52,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
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
|
||||
{
|
||||
|
||||
@@ -318,10 +318,13 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
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)
|
||||
{
|
||||
return new BigInteger((float)value);
|
||||
@@ -350,9 +353,11 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
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()));
|
||||
@@ -487,9 +492,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -587,9 +593,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
#endif
|
||||
#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;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -139,9 +139,9 @@ namespace Newtonsoft.Json.Utilities
|
||||
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))
|
||||
{
|
||||
callExpression = EnsureCastExpression(callExpression, type);
|
||||
|
||||
@@ -762,11 +762,11 @@ namespace Newtonsoft.Json.Utilities
|
||||
// http://hyperthink.net/blog/getcustomattributes-gotcha/
|
||||
// ICustomAttributeProvider doesn't do inheritance
|
||||
|
||||
if (provider is Type)
|
||||
Type t = provider as Type;
|
||||
if (t != null)
|
||||
{
|
||||
Type t = (Type)provider;
|
||||
object[] a = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
|
||||
Attribute[] attributes = a.Cast<Attribute>().ToArray();
|
||||
object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
|
||||
Attribute[] attributes = array.Cast<Attribute>().ToArray();
|
||||
|
||||
#if (NET20 || NET35)
|
||||
// ye olde .NET GetCustomAttributes doesn't respect the inherit argument
|
||||
@@ -779,29 +779,29 @@ namespace Newtonsoft.Json.Utilities
|
||||
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);
|
||||
}
|
||||
|
||||
if (provider is MemberInfo)
|
||||
MemberInfo mi = provider as MemberInfo;
|
||||
if (mi != null)
|
||||
{
|
||||
MemberInfo m = (MemberInfo)provider;
|
||||
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
|
||||
return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit);
|
||||
}
|
||||
|
||||
#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);
|
||||
}
|
||||
#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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user