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