diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
index a58743f0..9c720f8a 100644
--- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs
+++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs
@@ -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
diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
index 6b47ef33..cfec4fc8 100644
--- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
@@ -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
diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs
index b3244b3d..4255a42e 100644
--- a/Src/Newtonsoft.Json/JsonReader.cs
+++ b/Src/Newtonsoft.Json/JsonReader.cs
@@ -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);
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs
index e6a780c6..53c86652 100644
--- a/Src/Newtonsoft.Json/JsonWriter.cs
+++ b/Src/Newtonsoft.Json/JsonWriter.cs
@@ -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);
}
}
diff --git a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs
index fa1ebedb..c5234774 100644
--- a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs
+++ b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs
@@ -89,7 +89,7 @@ namespace Newtonsoft.Json.Linq
/// The new 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;
}
diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs
index 54bb8939..d94e4a48 100644
--- a/Src/Newtonsoft.Json/Linq/JToken.cs
+++ b/Src/Newtonsoft.Json/Linq/JToken.cs
@@ -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));
diff --git a/Src/Newtonsoft.Json/Linq/JTokenReader.cs b/Src/Newtonsoft.Json/Linq/JTokenReader.cs
index 20ea19f8..b245b5a9 100644
--- a/Src/Newtonsoft.Json/Linq/JTokenReader.cs
+++ b/Src/Newtonsoft.Json/Linq/JTokenReader.cs
@@ -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));
diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs
index b41a1abc..2af9d763 100644
--- a/Src/Newtonsoft.Json/Linq/JValue.cs
+++ b/Src/Newtonsoft.Json/Linq/JValue.cs
@@ -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);
}
diff --git a/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs
index b0f6d01f..34766002 100644
--- a/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs
+++ b/Src/Newtonsoft.Json/Serialization/DefaultReferenceResolver.cs
@@ -35,19 +35,18 @@ namespace Newtonsoft.Json.Serialization
private BidirectionalDictionary 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;
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
index 2bd45965..f41f28e0 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
@@ -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
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
index 7bc937e1..b947a567 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
@@ -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;
}
diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs
index 2503a3cd..06b86df0 100644
--- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs
@@ -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
{
diff --git a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs
index 6afc9ac6..52f8ce6c 100644
--- a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs
+++ b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs
@@ -52,9 +52,10 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(list, nameof(list));
- if (list is ICollection)
+ ICollection collection = list as ICollection;
+ if (collection != null)
{
- _genericCollection = (ICollection)list;
+ _genericCollection = collection;
}
else
{
diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs
index de771a59..bd5c0003 100644
--- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs
@@ -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
diff --git a/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs
index 99b93846..32581bcf 100644
--- a/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs
+++ b/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs
@@ -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);
diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
index 286bbe1e..71e162d0 100644
--- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs
@@ -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().ToArray();
+ object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
+ Attribute[] attributes = array.Cast().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);
}