-Refactor

This commit is contained in:
James Newton-King
2017-10-23 12:24:52 +13:00
parent 473a7721bd
commit 3154a6de68
23 changed files with 233 additions and 296 deletions
@@ -40,20 +40,4 @@ namespace Newtonsoft.Json.Linq
/// </summary> /// </summary>
Load = 1 Load = 1
} }
/// <summary>
/// Specifies how line information is handled when loading JSON.
/// </summary>
public enum LineInfoHandling
{
/// <summary>
/// Ignore line information.
/// </summary>
Ignore = 0,
/// <summary>
/// Load line information.
/// </summary>
Load = 1
}
} }
+3 -4
View File
@@ -171,8 +171,7 @@ namespace Newtonsoft.Json.Linq
{ {
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
JToken token = value as JToken; if (!(value is JToken token))
if (token == null)
{ {
throw new ArgumentException("Source value must be a JToken."); throw new ArgumentException("Source value must be a JToken.");
} }
@@ -275,9 +274,9 @@ namespace Newtonsoft.Json.Linq
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T))); throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
} }
if (value.Value is U) if (value.Value is U u)
{ {
return (U)value.Value; return u;
} }
Type targetType = typeof(U); Type targetType = typeof(U);
+1 -2
View File
@@ -90,8 +90,7 @@ namespace Newtonsoft.Json.Linq
internal override bool DeepEquals(JToken node) internal override bool DeepEquals(JToken node)
{ {
JArray t = node as JArray; return (node is JArray t && ContentsEqual(t));
return (t != null && ContentsEqual(t));
} }
internal override JToken CloneToken() internal override JToken CloneToken()
+2 -4
View File
@@ -52,8 +52,7 @@ namespace Newtonsoft.Json.Linq
internal override void MergeItem(object content, JsonMergeSettings settings) internal override void MergeItem(object content, JsonMergeSettings settings)
{ {
JConstructor c = content as JConstructor; if (!(content is JConstructor c))
if (c == null)
{ {
return; return;
} }
@@ -140,8 +139,7 @@ namespace Newtonsoft.Json.Linq
internal override bool DeepEquals(JToken node) internal override bool DeepEquals(JToken node)
{ {
JConstructor c = node as JConstructor; return (node is JConstructor c && _name == c.Name && ContentsEqual(c));
return (c != null && _name == c.Name && ContentsEqual(c));
} }
internal override JToken CloneToken() internal override JToken CloneToken()
+5 -10
View File
@@ -304,8 +304,7 @@ namespace Newtonsoft.Json.Linq
foreach (JToken o in ChildrenTokens) foreach (JToken o in ChildrenTokens)
{ {
yield return o; yield return o;
JContainer c = o as JContainer; if (o is JContainer c)
if (c != null)
{ {
foreach (JToken d in c.Descendants()) foreach (JToken d in c.Descendants())
{ {
@@ -597,8 +596,7 @@ namespace Newtonsoft.Json.Linq
internal static bool IsTokenUnchanged(JToken currentValue, JToken newValue) internal static bool IsTokenUnchanged(JToken currentValue, JToken newValue)
{ {
JValue v1 = currentValue as JValue; if (currentValue is JValue v1)
if (v1 != null)
{ {
// null will get turned into a JValue of type null // null will get turned into a JValue of type null
if (v1.Type == JTokenType.Null && newValue == null) if (v1.Type == JTokenType.Null && newValue == null)
@@ -668,8 +666,7 @@ namespace Newtonsoft.Json.Linq
internal static JToken CreateFromContent(object content) internal static JToken CreateFromContent(object content)
{ {
JToken token = content as JToken; if (content is JToken token)
if (token != null)
{ {
return token; return token;
} }
@@ -944,8 +941,7 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
JToken token = value as JToken; if (value is JToken token)
if (token != null)
{ {
return token; return token;
} }
@@ -1148,8 +1144,7 @@ namespace Newtonsoft.Json.Linq
{ {
JToken sourceItem = target[i]; JToken sourceItem = target[i];
JContainer existingContainer = sourceItem as JContainer; if (sourceItem is JContainer existingContainer)
if (existingContainer != null)
{ {
existingContainer.Merge(targetItem, settings); existingContainer.Merge(targetItem, settings);
} }
+2 -2
View File
@@ -113,9 +113,9 @@ namespace Newtonsoft.Json.Linq
/// </returns> /// </returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj is JEnumerable<T>) if (obj is JEnumerable<T> enumerable)
{ {
return Equals((JEnumerable<T>)obj); return Equals(enumerable);
} }
return false; return false;
+7 -15
View File
@@ -115,8 +115,7 @@ namespace Newtonsoft.Json.Linq
internal override bool DeepEquals(JToken node) internal override bool DeepEquals(JToken node)
{ {
JObject t = node as JObject; if (!(node is JObject t))
if (t == null)
{ {
return false; return false;
} }
@@ -169,8 +168,7 @@ namespace Newtonsoft.Json.Linq
internal override void MergeItem(object content, JsonMergeSettings settings) internal override void MergeItem(object content, JsonMergeSettings settings)
{ {
JObject o = content as JObject; if (!(content is JObject o))
if (o == null)
{ {
return; return;
} }
@@ -185,8 +183,7 @@ namespace Newtonsoft.Json.Linq
} }
else if (contentItem.Value != null) else if (contentItem.Value != null)
{ {
JContainer existingContainer = existingProperty.Value as JContainer; if (!(existingProperty.Value is JContainer existingContainer) || existingContainer.Type != contentItem.Value.Type)
if (existingContainer == null || existingContainer.Type != contentItem.Value.Type)
{ {
if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge) if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
{ {
@@ -257,8 +254,7 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
JToken property; _properties.TryGetValue(name, out JToken property);
_properties.TryGetValue(name, out property);
return (JProperty)property; return (JProperty)property;
} }
@@ -281,8 +277,7 @@ namespace Newtonsoft.Json.Linq
{ {
ValidationUtils.ArgumentNotNull(key, nameof(key)); ValidationUtils.ArgumentNotNull(key, nameof(key));
string propertyName = key as string; if (!(key is string propertyName))
if (propertyName == null)
{ {
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
} }
@@ -293,8 +288,7 @@ namespace Newtonsoft.Json.Linq
{ {
ValidationUtils.ArgumentNotNull(key, nameof(key)); ValidationUtils.ArgumentNotNull(key, nameof(key));
string propertyName = key as string; if (!(key is string propertyName))
if (propertyName == null)
{ {
throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
} }
@@ -794,10 +788,8 @@ namespace Newtonsoft.Json.Linq
public override bool TrySetMember(JObject instance, SetMemberBinder binder, object value) public override bool TrySetMember(JObject instance, SetMemberBinder binder, object value)
{ {
JToken v = value as JToken;
// this can throw an error if value isn't a valid for a JValue // this can throw an error if value isn't a valid for a JValue
if (v == null) if (!(value is JToken v))
{ {
v = new JValue(value); v = new JValue(value);
} }
+2 -3
View File
@@ -263,8 +263,7 @@ namespace Newtonsoft.Json.Linq
internal override bool DeepEquals(JToken node) internal override bool DeepEquals(JToken node)
{ {
JProperty t = node as JProperty; return (node is JProperty t && _name == t.Name && ContentsEqual(t));
return (t != null && _name == t.Name && ContentsEqual(t));
} }
internal override JToken CloneToken() internal override JToken CloneToken()
@@ -338,7 +337,7 @@ namespace Newtonsoft.Json.Linq
internal override int GetDeepHashCode() internal override int GetDeepHashCode()
{ {
return _name.GetHashCode() ^ ((Value != null) ? Value.GetDeepHashCode() : 0); return _name.GetHashCode() ^ (Value?.GetDeepHashCode() ?? 0);
} }
/// <summary> /// <summary>
@@ -98,8 +98,7 @@ namespace Newtonsoft.Json.Linq
} }
string key = GetKeyForItem(item); string key = GetKeyForItem(item);
JToken value; return _dictionary.TryGetValue(key, out _);
return _dictionary.TryGetValue(key, out value);
} }
private void EnsureDictionary() private void EnsureDictionary()
@@ -258,8 +257,7 @@ namespace Newtonsoft.Json.Linq
foreach (KeyValuePair<string, JToken> keyAndProperty in d1) foreach (KeyValuePair<string, JToken> keyAndProperty in d1)
{ {
JToken secondValue; if (!d2.TryGetValue(keyAndProperty.Key, out JToken secondValue))
if (!d2.TryGetValue(keyAndProperty.Key, out secondValue))
{ {
return false; return false;
} }
+89 -106
View File
@@ -34,7 +34,6 @@ using System.IO;
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
using System.Numerics; using System.Numerics;
#endif #endif
using System.Text;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
@@ -449,9 +448,9 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentNullException(nameof(value)); throw new ArgumentNullException(nameof(value));
} }
if (value is JProperty) if (value is JProperty property)
{ {
value = ((JProperty)value).Value; value = property.Value;
} }
JValue v = value as JValue; JValue v = value as JValue;
@@ -491,9 +490,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return Convert.ToBoolean((int)(BigInteger)v.Value); return Convert.ToBoolean((int)integer);
} }
#endif #endif
@@ -514,16 +513,16 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
} }
if (v.Value is DateTimeOffset) if (v.Value is DateTimeOffset offset)
{ {
return (DateTimeOffset)v.Value; return offset;
} }
string s = v.Value as string; if (v.Value is string s)
if (s != null)
{ {
return DateTimeOffset.Parse(s, 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
@@ -547,9 +546,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return Convert.ToBoolean((int)(BigInteger)v.Value); return Convert.ToBoolean((int)integer);
} }
#endif #endif
@@ -570,9 +569,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (long)(BigInteger)v.Value; return (long)integer;
} }
#endif #endif
@@ -598,9 +597,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (v.Value is DateTimeOffset) if (v.Value is DateTimeOffset offset)
{ {
return ((DateTimeOffset)v.Value).DateTime; return offset.DateTime;
} }
#endif #endif
@@ -630,13 +629,12 @@ namespace Newtonsoft.Json.Linq
{ {
return null; return null;
} }
if (v.Value is DateTimeOffset) if (v.Value is DateTimeOffset offset)
{ {
return (DateTimeOffset?)v.Value; return offset;
} }
string s = v.Value as string; if (v.Value is string s)
if (s != null)
{ {
return DateTimeOffset.Parse(s, CultureInfo.InvariantCulture); return DateTimeOffset.Parse(s, CultureInfo.InvariantCulture);
} }
@@ -664,9 +662,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (decimal?)(BigInteger)v.Value; return (decimal?)integer;
} }
#endif #endif
@@ -692,9 +690,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (double?)(BigInteger)v.Value; return (double?)integer;
} }
#endif #endif
@@ -720,9 +718,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (char?)(BigInteger)v.Value; return (char?)integer;
} }
#endif #endif
@@ -743,9 +741,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (int)(BigInteger)v.Value; return (int)integer;
} }
#endif #endif
@@ -766,9 +764,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (short)(BigInteger)v.Value; return (short)integer;
} }
#endif #endif
@@ -790,9 +788,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (ushort)(BigInteger)v.Value; return (ushort)integer;
} }
#endif #endif
@@ -814,9 +812,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (char)(BigInteger)v.Value; return (char)integer;
} }
#endif #endif
@@ -837,9 +835,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (byte)(BigInteger)v.Value; return (byte)integer;
} }
#endif #endif
@@ -861,9 +859,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (sbyte)(BigInteger)v.Value; return (sbyte)integer;
} }
#endif #endif
@@ -889,9 +887,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (int?)(BigInteger)v.Value; return (int?)integer;
} }
#endif #endif
@@ -917,9 +915,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (short?)(BigInteger)v.Value; return (short?)integer;
} }
#endif #endif
@@ -946,9 +944,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (ushort?)(BigInteger)v.Value; return (ushort?)integer;
} }
#endif #endif
@@ -974,9 +972,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (byte?)(BigInteger)v.Value; return (byte?)integer;
} }
#endif #endif
@@ -1003,9 +1001,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (sbyte?)(BigInteger)v.Value; return (sbyte?)integer;
} }
#endif #endif
@@ -1026,9 +1024,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (v.Value is DateTimeOffset) if (v.Value is DateTimeOffset offset)
{ {
return ((DateTimeOffset)v.Value).DateTime; return offset.DateTime;
} }
#endif #endif
@@ -1054,9 +1052,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (long?)(BigInteger)v.Value; return (long?)integer;
} }
#endif #endif
@@ -1082,9 +1080,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (float?)(BigInteger)v.Value; return (float?)integer;
} }
#endif #endif
@@ -1105,9 +1103,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (decimal)(BigInteger)v.Value; return (decimal)integer;
} }
#endif #endif
@@ -1134,9 +1132,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (uint?)(BigInteger)v.Value; return (uint?)integer;
} }
#endif #endif
@@ -1163,9 +1161,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (ulong?)(BigInteger)v.Value; return (ulong?)integer;
} }
#endif #endif
@@ -1186,9 +1184,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (double)(BigInteger)v.Value; return (double)integer;
} }
#endif #endif
@@ -1209,9 +1207,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (float)(BigInteger)v.Value; return (float)integer;
} }
#endif #endif
@@ -1241,16 +1239,15 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
byte[] bytes = v.Value as byte[]; if (v.Value is byte[] bytes)
if (bytes != null)
{ {
return Convert.ToBase64String(bytes); return Convert.ToBase64String(bytes);
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return ((BigInteger)v.Value).ToString(CultureInfo.InvariantCulture); return integer.ToString(CultureInfo.InvariantCulture);
} }
#endif #endif
@@ -1272,9 +1269,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (uint)(BigInteger)v.Value; return (uint)integer;
} }
#endif #endif
@@ -1296,9 +1293,9 @@ namespace Newtonsoft.Json.Linq
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return (ulong)(BigInteger)v.Value; return (ulong)integer;
} }
#endif #endif
@@ -1328,14 +1325,13 @@ namespace Newtonsoft.Json.Linq
return Convert.FromBase64String(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return Convert.FromBase64String(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (v.Value is BigInteger) if (v.Value is BigInteger integer)
{ {
return ((BigInteger)v.Value).ToByteArray(); return integer.ToByteArray();
} }
#endif #endif
byte[] bytes = v.Value as byte[]; if (v.Value is byte[] bytes)
if (bytes != null)
{ {
return bytes; return bytes;
} }
@@ -1356,13 +1352,12 @@ 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)));
} }
byte[] bytes = v.Value as byte[]; if (v.Value is byte[] bytes)
if (bytes != null)
{ {
return new Guid(bytes); 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) ? guid : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
/// <summary> /// <summary>
@@ -1388,13 +1383,12 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
byte[] bytes = v.Value as byte[]; if (v.Value is byte[] bytes)
if (bytes != null)
{ {
return new Guid(bytes); 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) ? guid : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
/// <summary> /// <summary>
@@ -1410,7 +1404,7 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
} }
return (v.Value is TimeSpan) ? (TimeSpan)v.Value : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return (v.Value is TimeSpan span) ? span : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
/// <summary> /// <summary>
@@ -1436,7 +1430,7 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
return (v.Value is TimeSpan) ? (TimeSpan)v.Value : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return (v.Value is TimeSpan span) ? span : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
/// <summary> /// <summary>
@@ -1462,7 +1456,7 @@ namespace Newtonsoft.Json.Linq
return null; return null;
} }
return (v.Value is Uri) ? (Uri)v.Value : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); return (v.Value is Uri uri) ? uri : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
@@ -1939,8 +1933,7 @@ namespace Newtonsoft.Json.Linq
{ {
if (JsonConvert.DefaultSettings == null) if (JsonConvert.DefaultSettings == null)
{ {
bool isEnum; PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(objectType, out bool isEnum);
PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(objectType, out isEnum);
if (isEnum) if (isEnum)
{ {
@@ -2419,8 +2412,7 @@ namespace Newtonsoft.Json.Linq
} }
else else
{ {
object[] annotations = _annotations as object[]; if (!(_annotations is object[] annotations))
if (annotations == null)
{ {
_annotations = new[] { _annotations, annotation }; _annotations = new[] { _annotations, annotation };
} }
@@ -2450,8 +2442,7 @@ namespace Newtonsoft.Json.Linq
{ {
if (_annotations != null) if (_annotations != null)
{ {
object[] annotations = _annotations as object[]; if (!(_annotations is object[] annotations))
if (annotations == null)
{ {
return (_annotations as T); return (_annotations as T);
} }
@@ -2463,8 +2454,7 @@ namespace Newtonsoft.Json.Linq
break; break;
} }
T local = annotation as T; if (annotation is T local)
if (local != null)
{ {
return local; return local;
} }
@@ -2488,8 +2478,7 @@ namespace Newtonsoft.Json.Linq
if (_annotations != null) if (_annotations != null)
{ {
object[] annotations = _annotations as object[]; if (!(_annotations is object[] annotations))
if (annotations == null)
{ {
if (type.IsInstanceOfType(_annotations)) if (type.IsInstanceOfType(_annotations))
{ {
@@ -2529,8 +2518,7 @@ namespace Newtonsoft.Json.Linq
yield break; yield break;
} }
object[] annotations = _annotations as object[]; if (_annotations is object[] annotations)
if (annotations != null)
{ {
for (int i = 0; i < annotations.Length; i++) for (int i = 0; i < annotations.Length; i++)
{ {
@@ -2540,8 +2528,7 @@ namespace Newtonsoft.Json.Linq
break; break;
} }
T casted = o as T; if (o is T casted)
if (casted != null)
{ {
yield return casted; yield return casted;
} }
@@ -2549,8 +2536,7 @@ namespace Newtonsoft.Json.Linq
yield break; yield break;
} }
T annotation = _annotations as T; if (!(_annotations is T annotation))
if (annotation == null)
{ {
yield break; yield break;
} }
@@ -2575,8 +2561,7 @@ namespace Newtonsoft.Json.Linq
yield break; yield break;
} }
object[] annotations = _annotations as object[]; if (_annotations is object[] annotations)
if (annotations != null)
{ {
for (int i = 0; i < annotations.Length; i++) for (int i = 0; i < annotations.Length; i++)
{ {
@@ -2610,8 +2595,7 @@ namespace Newtonsoft.Json.Linq
{ {
if (_annotations != null) if (_annotations != null)
{ {
object[] annotations = _annotations as object[]; if (!(_annotations is object[] annotations))
if (annotations == null)
{ {
if (_annotations is T) if (_annotations is T)
{ {
@@ -2666,8 +2650,7 @@ namespace Newtonsoft.Json.Linq
if (_annotations != null) if (_annotations != null)
{ {
object[] annotations = _annotations as object[]; if (!(_annotations is object[] annotations))
if (annotations == null)
{ {
if (type.IsInstanceOfType(_annotations)) if (type.IsInstanceOfType(_annotations))
{ {
+1 -2
View File
@@ -76,8 +76,7 @@ namespace Newtonsoft.Json.Linq
return false; return false;
} }
JContainer container = _current as JContainer; if (_current is JContainer container && _parent != container)
if (container != null && _parent != container)
{ {
return ReadInto(container); return ReadInto(container);
} }
+2 -4
View File
@@ -379,7 +379,7 @@ namespace Newtonsoft.Json.Linq
public override void WriteValue(char value) public override void WriteValue(char value)
{ {
base.WriteValue(value); base.WriteValue(value);
string s = null; string s;
#if HAVE_CHAR_TO_STRING_WITH_CULTURE #if HAVE_CHAR_TO_STRING_WITH_CULTURE
s = value.ToString(CultureInfo.InvariantCulture); s = value.ToString(CultureInfo.InvariantCulture);
#else #else
@@ -485,10 +485,8 @@ namespace Newtonsoft.Json.Linq
internal override void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments) internal override void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{ {
JTokenReader tokenReader = reader as JTokenReader;
// cloning the token rather than reading then writing it doesn't lose some type information, e.g. Guid, byte[], etc // cloning the token rather than reading then writing it doesn't lose some type information, e.g. Guid, byte[], etc
if (tokenReader != null && writeChildren && writeDateConstructorAsDate && writeComments) if (reader is JTokenReader tokenReader && writeChildren && writeDateConstructorAsDate && writeComments)
{ {
if (tokenReader.TokenType == JsonToken.None) if (tokenReader.TokenType == JsonToken.None)
{ {
+16 -16
View File
@@ -69,43 +69,43 @@ namespace Newtonsoft.Json.Linq
case JTokenType.Undefined: case JTokenType.Undefined:
return writer.WriteUndefinedAsync(cancellationToken); return writer.WriteUndefinedAsync(cancellationToken);
case JTokenType.Integer: case JTokenType.Integer:
if (_value is int) if (_value is int i)
{ {
return writer.WriteValueAsync((int)_value, cancellationToken); return writer.WriteValueAsync(i, cancellationToken);
} }
if (_value is long) if (_value is long l)
{ {
return writer.WriteValueAsync((long)_value, cancellationToken); return writer.WriteValueAsync(l, cancellationToken);
} }
if (_value is ulong) if (_value is ulong ul)
{ {
return writer.WriteValueAsync((ulong)_value, cancellationToken); return writer.WriteValueAsync(ul, cancellationToken);
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (_value is BigInteger) if (_value is BigInteger integer)
{ {
return writer.WriteValueAsync((BigInteger)_value, cancellationToken); return writer.WriteValueAsync(integer, cancellationToken);
} }
#endif #endif
return writer.WriteValueAsync(Convert.ToInt64(_value, CultureInfo.InvariantCulture), cancellationToken); return writer.WriteValueAsync(Convert.ToInt64(_value, CultureInfo.InvariantCulture), cancellationToken);
case JTokenType.Float: case JTokenType.Float:
if (_value is decimal) if (_value is decimal dec)
{ {
return writer.WriteValueAsync((decimal)_value, cancellationToken); return writer.WriteValueAsync(dec, cancellationToken);
} }
if (_value is double) if (_value is double d)
{ {
return writer.WriteValueAsync((double)_value, cancellationToken); return writer.WriteValueAsync(d, cancellationToken);
} }
if (_value is float) if (_value is float f)
{ {
return writer.WriteValueAsync((float)_value, cancellationToken); return writer.WriteValueAsync(f, cancellationToken);
} }
return writer.WriteValueAsync(Convert.ToDouble(_value, CultureInfo.InvariantCulture), cancellationToken); return writer.WriteValueAsync(Convert.ToDouble(_value, CultureInfo.InvariantCulture), cancellationToken);
@@ -114,9 +114,9 @@ namespace Newtonsoft.Json.Linq
case JTokenType.Boolean: case JTokenType.Boolean:
return writer.WriteValueAsync(Convert.ToBoolean(_value, CultureInfo.InvariantCulture), cancellationToken); return writer.WriteValueAsync(Convert.ToBoolean(_value, CultureInfo.InvariantCulture), cancellationToken);
case JTokenType.Date: case JTokenType.Date:
if (_value is DateTimeOffset) if (_value is DateTimeOffset offset)
{ {
return writer.WriteValueAsync((DateTimeOffset)_value, cancellationToken); return writer.WriteValueAsync(offset, cancellationToken);
} }
return writer.WriteValueAsync(Convert.ToDateTime(_value, CultureInfo.InvariantCulture), cancellationToken); return writer.WriteValueAsync(Convert.ToDateTime(_value, CultureInfo.InvariantCulture), cancellationToken);
+52 -60
View File
@@ -195,8 +195,7 @@ namespace Newtonsoft.Json.Linq
internal override bool DeepEquals(JToken node) internal override bool DeepEquals(JToken node)
{ {
JValue other = node as JValue; if (!(node is JValue other))
if (other == null)
{ {
return false; return false;
} }
@@ -228,10 +227,9 @@ namespace Newtonsoft.Json.Linq
// converting a fractional number to a BigInteger will lose the fraction // converting a fractional number to a BigInteger will lose the fraction
// check for fraction if result is two numbers are equal // check for fraction if result is two numbers are equal
if (i2 is decimal) if (i2 is decimal d1)
{ {
decimal d = (decimal)i2; return (0m).CompareTo(Math.Abs(d1 - Math.Truncate(d1)));
return (0m).CompareTo(Math.Abs(d - Math.Truncate(d)));
} }
else if (i2 is double || i2 is float) else if (i2 is double || i2 is float)
{ {
@@ -261,14 +259,15 @@ namespace Newtonsoft.Json.Linq
switch (valueType) switch (valueType)
{ {
case JTokenType.Integer: case JTokenType.Integer:
{
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (objA is BigInteger) if (objA is BigInteger integerA)
{ {
return CompareBigInteger((BigInteger)objA, objB); return CompareBigInteger(integerA, objB);
} }
if (objB is BigInteger) if (objB is BigInteger integerB)
{ {
return -CompareBigInteger((BigInteger)objB, objA); return -CompareBigInteger(integerB, objA);
} }
#endif #endif
if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
@@ -283,15 +282,17 @@ namespace Newtonsoft.Json.Linq
{ {
return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)); return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture));
} }
}
case JTokenType.Float: case JTokenType.Float:
{
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (objA is BigInteger) if (objA is BigInteger integerA)
{ {
return CompareBigInteger((BigInteger)objA, objB); return CompareBigInteger(integerA, objB);
} }
if (objB is BigInteger) if (objB is BigInteger integerB)
{ {
return -CompareBigInteger((BigInteger)objB, objA); return -CompareBigInteger(integerB, objA);
} }
#endif #endif
if (objA is ulong || objB is ulong || objA is decimal || objB is decimal) if (objA is ulong || objB is ulong || objA is decimal || objB is decimal)
@@ -299,6 +300,7 @@ namespace Newtonsoft.Json.Linq
return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture)); return Convert.ToDecimal(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(objB, CultureInfo.InvariantCulture));
} }
return CompareFloat(objA, objB); return CompareFloat(objA, objB);
}
case JTokenType.Comment: case JTokenType.Comment:
case JTokenType.String: case JTokenType.String:
case JTokenType.Raw: case JTokenType.Raw:
@@ -313,54 +315,48 @@ namespace Newtonsoft.Json.Linq
return b1.CompareTo(b2); return b1.CompareTo(b2);
case JTokenType.Date: case JTokenType.Date:
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (objA is DateTime) if (objA is DateTime dateA)
{ {
#else
DateTime dateA = (DateTime)objA;
#endif #endif
DateTime date1 = (DateTime)objA; DateTime dateB;
DateTime date2;
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (objB is DateTimeOffset) if (objB is DateTimeOffset offsetB)
{ {
date2 = ((DateTimeOffset)objB).DateTime; dateB = offsetB.DateTime;
} }
else else
#endif #endif
{ {
date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); dateB = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
} }
return date1.CompareTo(date2); return dateA.CompareTo(dateB);
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
} }
else else
{ {
DateTimeOffset date1 = (DateTimeOffset)objA; DateTimeOffset offsetA = (DateTimeOffset)objA;
DateTimeOffset date2; if (!(objB is DateTimeOffset offsetB))
if (objB is DateTimeOffset)
{ {
date2 = (DateTimeOffset)objB; offsetB = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
}
else
{
date2 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
} }
return date1.CompareTo(date2); return offsetA.CompareTo(offsetB);
} }
#endif #endif
case JTokenType.Bytes: case JTokenType.Bytes:
byte[] bytes2 = objB as byte[]; if (!(objB is byte[] bytesB))
if (bytes2 == null)
{ {
throw new ArgumentException("Object must be of type byte[]."); throw new ArgumentException("Object must be of type byte[].");
} }
byte[] bytes1 = objA as byte[]; byte[] bytesA = objA as byte[];
Debug.Assert(bytes1 != null); Debug.Assert(bytesA != null);
return MiscellaneousUtils.ByteArrayCompare(bytes1, bytes2); return MiscellaneousUtils.ByteArrayCompare(bytesA, bytesB);
case JTokenType.Guid: case JTokenType.Guid:
if (!(objB is Guid)) if (!(objB is Guid))
{ {
@@ -747,22 +743,22 @@ namespace Newtonsoft.Json.Linq
writer.WriteUndefined(); writer.WriteUndefined();
return; return;
case JTokenType.Integer: case JTokenType.Integer:
if (_value is int) if (_value is int i)
{ {
writer.WriteValue((int)_value); writer.WriteValue(i);
} }
else if (_value is long) else if (_value is long l)
{ {
writer.WriteValue((long)_value); writer.WriteValue(l);
} }
else if (_value is ulong) else if (_value is ulong ul)
{ {
writer.WriteValue((ulong)_value); writer.WriteValue(ul);
} }
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
else if (_value is BigInteger) else if (_value is BigInteger integer)
{ {
writer.WriteValue((BigInteger)_value); writer.WriteValue(integer);
} }
#endif #endif
else else
@@ -771,17 +767,17 @@ namespace Newtonsoft.Json.Linq
} }
return; return;
case JTokenType.Float: case JTokenType.Float:
if (_value is decimal) if (_value is decimal dec)
{ {
writer.WriteValue((decimal)_value); writer.WriteValue(dec);
} }
else if (_value is double) else if (_value is double d)
{ {
writer.WriteValue((double)_value); writer.WriteValue(d);
} }
else if (_value is float) else if (_value is float f)
{ {
writer.WriteValue((float)_value); writer.WriteValue(f);
} }
else else
{ {
@@ -796,9 +792,9 @@ namespace Newtonsoft.Json.Linq
return; return;
case JTokenType.Date: case JTokenType.Date:
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (_value is DateTimeOffset) if (_value is DateTimeOffset offset)
{ {
writer.WriteValue((DateTimeOffset)_value); writer.WriteValue(offset);
} }
else else
#endif #endif
@@ -936,8 +932,7 @@ namespace Newtonsoft.Json.Linq
return string.Empty; return string.Empty;
} }
IFormattable formattable = _value as IFormattable; if (_value is IFormattable formattable)
if (formattable != null)
{ {
return formattable.ToString(format, formatProvider); return formattable.ToString(format, formatProvider);
} }
@@ -984,8 +979,7 @@ 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)
{ {
JValue value = arg as JValue; object compareValue = arg is JValue value ? value.Value : arg;
object compareValue = value != null ? value.Value : arg;
switch (binder.Operation) switch (binder.Operation)
{ {
@@ -1095,14 +1089,12 @@ namespace Newtonsoft.Json.Linq
return TypeCode.Empty; return TypeCode.Empty;
} }
IConvertible convertable = _value as IConvertible; if (_value is IConvertible convertable)
if (convertable == null)
{ {
return TypeCode.Object; return convertable.GetTypeCode();
} }
return convertable.GetTypeCode(); return TypeCode.Object;
} }
bool IConvertible.ToBoolean(IFormatProvider provider) bool IConvertible.ToBoolean(IFormatProvider provider)
@@ -20,8 +20,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
foreach (JToken t in current) foreach (JToken t in current)
{ {
JArray a = t as JArray; if (t is JArray a)
if (a != null)
{ {
// set defaults for null arguments // set defaults for null arguments
int stepCount = Step ?? 1; int stepCount = Step ?? 1;
@@ -12,8 +12,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
foreach (JToken t in current) foreach (JToken t in current)
{ {
JObject o = t as JObject; if (t is JObject o)
if (o != null)
{ {
if (Name != null) if (Name != null)
{ {
@@ -17,8 +17,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
foreach (JToken t in current) foreach (JToken t in current)
{ {
JObject o = t as JObject; if (t is JObject o)
if (o != null)
{ {
foreach (string name in Names) foreach (string name in Names)
{ {
+11 -15
View File
@@ -406,7 +406,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
_currentIndex++; _currentIndex++;
QueryExpression expression = ParseExpression(scan); QueryExpression expression = ParseExpression();
_currentIndex++; _currentIndex++;
EnsureLength("Path ended with open indexer."); EnsureLength("Path ended with open indexer.");
@@ -433,7 +433,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
} }
} }
private bool TryParseExpression(bool scan, out List<PathFilter> expressionPath) private bool TryParseExpression(out List<PathFilter> expressionPath)
{ {
if (_expression[_currentIndex] == '$') if (_expression[_currentIndex] == '$')
{ {
@@ -465,12 +465,11 @@ namespace Newtonsoft.Json.Linq.JsonPath
return new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]); return new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
} }
private object ParseSide(bool scan) private object ParseSide()
{ {
EatWhitespace(); EatWhitespace();
List<PathFilter> expressionPath; if (TryParseExpression(out var expressionPath))
if (TryParseExpression(scan, out expressionPath))
{ {
EatWhitespace(); EatWhitespace();
EnsureLength("Path ended with open query."); EnsureLength("Path ended with open query.");
@@ -478,8 +477,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
return expressionPath; return expressionPath;
} }
object value; if (TryParseValue(out var value))
if (TryParseValue(out value))
{ {
EatWhitespace(); EatWhitespace();
EnsureLength("Path ended with open query."); EnsureLength("Path ended with open query.");
@@ -490,14 +488,14 @@ namespace Newtonsoft.Json.Linq.JsonPath
throw CreateUnexpectedCharacterException(); throw CreateUnexpectedCharacterException();
} }
private QueryExpression ParseExpression(bool scan) private QueryExpression ParseExpression()
{ {
QueryExpression rootExpression = null; QueryExpression rootExpression = null;
CompositeExpression parentExpression = null; CompositeExpression parentExpression = null;
while (_currentIndex < _expression.Length) while (_currentIndex < _expression.Length)
{ {
object left = ParseSide(scan); object left = ParseSide();
object right = null; object right = null;
QueryOperator op; QueryOperator op;
@@ -511,7 +509,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
op = ParseOperator(); op = ParseOperator();
right = ParseSide(scan); right = ParseSide();
} }
BooleanQueryExpression booleanExpression = new BooleanQueryExpression BooleanQueryExpression booleanExpression = new BooleanQueryExpression
@@ -603,17 +601,15 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
string numberText = sb.ToString(); string numberText = sb.ToString();
if (numberText.IndexOfAny(new char[] { '.', 'E', 'e' }) != -1) if (numberText.IndexOfAny(new[] { '.', 'E', 'e' }) != -1)
{ {
double d; bool result = double.TryParse(numberText, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var d);
bool result = double.TryParse(numberText, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out d);
value = d; value = d;
return result; return result;
} }
else else
{ {
long l; bool result = long.TryParse(numberText, NumberStyles.Integer, CultureInfo.InvariantCulture, out var l);
bool result = long.TryParse(numberText, NumberStyles.Integer, CultureInfo.InvariantCulture, out l);
value = l; value = l;
return result; return result;
} }
@@ -10,10 +10,8 @@ namespace Newtonsoft.Json.Linq.JsonPath
protected static JToken GetTokenIndex(JToken t, bool errorWhenNoMatch, int index) protected static JToken GetTokenIndex(JToken t, bool errorWhenNoMatch, int index)
{ {
JArray a = t as JArray;
JConstructor c = t as JConstructor;
if (a != null) if (t is JArray a)
{ {
if (a.Count <= index) if (a.Count <= index)
{ {
@@ -27,7 +25,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
return a[index]; return a[index];
} }
else if (c != null) else if (t is JConstructor c)
{ {
if (c.Count <= index) if (c.Count <= index)
{ {
@@ -76,14 +76,12 @@ namespace Newtonsoft.Json.Linq.JsonPath
private IEnumerable<JToken> GetResult(JToken root, JToken t, object o) private IEnumerable<JToken> GetResult(JToken root, JToken t, object o)
{ {
JToken resultToken = o as JToken; if (o is JToken resultToken)
if (resultToken != null)
{ {
return new[] { resultToken }; return new[] { resultToken };
} }
List<PathFilter> pathFilters = o as List<PathFilter>; if (o is List<PathFilter> pathFilters)
if (pathFilters != null)
{ {
return JPath.Evaluate(pathFilters, root, t, false); return JPath.Evaluate(pathFilters, root, t, false);
} }
@@ -124,10 +122,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
private bool MatchTokens(JToken leftResult, JToken rightResult) private bool MatchTokens(JToken leftResult, JToken rightResult)
{ {
JValue leftValue = leftResult as JValue; if (leftResult is JValue leftValue && rightResult is JValue rightValue)
JValue rightValue = rightResult as JValue;
if (leftValue != null && rightValue != null)
{ {
switch (Operator) switch (Operator)
{ {
@@ -209,9 +204,9 @@ namespace Newtonsoft.Json.Linq.JsonPath
using (StringWriter writer = StringUtils.CreateStringWriter(64)) using (StringWriter writer = StringUtils.CreateStringWriter(64))
{ {
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (value.Value is DateTimeOffset) if (value.Value is DateTimeOffset offset)
{ {
DateTimeUtils.WriteDateTimeOffsetString(writer, (DateTimeOffset)value.Value, DateFormatHandling.IsoDateFormat, null, CultureInfo.InvariantCulture); DateTimeUtils.WriteDateTimeOffsetString(writer, offset, DateFormatHandling.IsoDateFormat, null, CultureInfo.InvariantCulture);
} }
else else
#endif #endif
@@ -16,22 +16,22 @@ namespace Newtonsoft.Json.Linq.JsonPath
} }
JToken value = c; JToken value = c;
JContainer container = c as JContainer;
while (true) while (true)
{ {
JContainer container = value as JContainer;
value = GetNextScanValue(c, container, value); value = GetNextScanValue(c, container, value);
if (value == null) if (value == null)
{ {
break; break;
} }
JProperty e = value as JProperty; if (value is JProperty property)
if (e != null)
{ {
if (e.Name == Name) if (property.Name == Name)
{ {
yield return e.Value; yield return property.Value;
} }
} }
else else
@@ -41,8 +41,6 @@ namespace Newtonsoft.Json.Linq.JsonPath
yield return value; yield return value;
} }
} }
container = value as JContainer;
} }
} }
} }
@@ -11,29 +11,28 @@ namespace Newtonsoft.Json.Linq.JsonPath
foreach (JToken c in current) foreach (JToken c in current)
{ {
JToken value = c; JToken value = c;
JContainer container = c as JContainer;
while (true) while (true)
{ {
JContainer container = value as JContainer;
value = GetNextScanValue(c, container, value); value = GetNextScanValue(c, container, value);
if (value == null) if (value == null)
{ {
break; break;
} }
JProperty e = value as JProperty; if (value is JProperty property)
if (e != null)
{ {
foreach (string name in Names) foreach (string name in Names)
{ {
if (e.Name == name) if (property.Name == name)
{ {
yield return e.Value; yield return property.Value;
} }
} }
} }
container = value as JContainer;
} }
} }
} }
@@ -0,0 +1,18 @@
namespace Newtonsoft.Json.Linq
{
/// <summary>
/// Specifies how line information is handled when loading JSON.
/// </summary>
public enum LineInfoHandling
{
/// <summary>
/// Ignore line information.
/// </summary>
Ignore = 0,
/// <summary>
/// Load line information.
/// </summary>
Load = 1
}
}