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

Gives slightly better performance, so applying across the board
adds up.
This commit is contained in:
Jon Hanna
2017-01-05 01:52:04 +00:00
parent 306c3a812b
commit a95665b481
16 changed files with 136 additions and 109 deletions
+23 -12
View File
@@ -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));