-Minor refactor

This commit is contained in:
James Newton-King
2017-10-14 12:18:10 +13:00
parent 5829826cbb
commit 8dd5eff2ee
6 changed files with 123 additions and 293 deletions
@@ -157,10 +157,7 @@ namespace Newtonsoft.Json.Converters
return _element.GetPrefixOfNamespace(namespaceUri); return _element.GetPrefixOfNamespace(namespaceUri);
} }
public bool IsEmpty public bool IsEmpty => _element.IsEmpty;
{
get { return _element.IsEmpty; }
}
} }
internal class XmlDeclarationWrapper : XmlNodeWrapper, IXmlDeclaration internal class XmlDeclarationWrapper : XmlNodeWrapper, IXmlDeclaration
@@ -173,21 +170,18 @@ namespace Newtonsoft.Json.Converters
_declaration = declaration; _declaration = declaration;
} }
public string Version public string Version => _declaration.Version;
{
get { return _declaration.Version; }
}
public string Encoding public string Encoding
{ {
get { return _declaration.Encoding; } get => _declaration.Encoding;
set { _declaration.Encoding = value; } set => _declaration.Encoding = value;
} }
public string Standalone public string Standalone
{ {
get { return _declaration.Standalone; } get => _declaration.Standalone;
set { _declaration.Standalone = value; } set => _declaration.Standalone = value;
} }
} }
@@ -202,30 +196,15 @@ namespace Newtonsoft.Json.Converters
_documentType = documentType; _documentType = documentType;
} }
public string Name public string Name => _documentType.Name;
{
get { return _documentType.Name; }
}
public string System public string System => _documentType.SystemId;
{
get { return _documentType.SystemId; }
}
public string Public public string Public => _documentType.PublicId;
{
get { return _documentType.PublicId; }
}
public string InternalSubset public string InternalSubset => _documentType.InternalSubset;
{
get { return _documentType.InternalSubset; }
}
public override string LocalName public override string LocalName => "DOCTYPE";
{
get { return "DOCTYPE"; }
}
} }
#endif #endif
@@ -240,20 +219,11 @@ namespace Newtonsoft.Json.Converters
_node = node; _node = node;
} }
public object WrappedNode public object WrappedNode => _node;
{
get { return _node; }
}
public XmlNodeType NodeType public XmlNodeType NodeType => _node.NodeType;
{
get { return _node.NodeType; }
}
public virtual string LocalName public virtual string LocalName => _node.LocalName;
{
get { return _node.LocalName; }
}
public List<IXmlNode> ChildNodes public List<IXmlNode> ChildNodes
{ {
@@ -281,10 +251,7 @@ namespace Newtonsoft.Json.Converters
} }
} }
protected virtual bool HasChildNodes protected virtual bool HasChildNodes => _node.HasChildNodes;
{
get { return _node.HasChildNodes; }
}
internal static IXmlNode WrapNode(XmlNode node) internal static IXmlNode WrapNode(XmlNode node)
{ {
@@ -333,8 +300,7 @@ namespace Newtonsoft.Json.Converters
{ {
get get
{ {
XmlElement element = _node as XmlElement; if (_node is XmlElement element)
if (element != null)
{ {
return element.HasAttributes; return element.HasAttributes;
} }
@@ -347,8 +313,7 @@ namespace Newtonsoft.Json.Converters
{ {
get get
{ {
XmlAttribute attribute = _node as XmlAttribute; XmlNode node = _node is XmlAttribute attribute ? attribute.OwnerElement : _node.ParentNode;
XmlNode node = attribute != null ? attribute.OwnerElement : _node.ParentNode;
if (node == null) if (node == null)
{ {
@@ -361,8 +326,8 @@ namespace Newtonsoft.Json.Converters
public string Value public string Value
{ {
get { return _node.Value; } get => _node.Value;
set { _node.Value = value; } set => _node.Value = value;
} }
public IXmlNode AppendChild(IXmlNode newChild) public IXmlNode AppendChild(IXmlNode newChild)
@@ -375,10 +340,7 @@ namespace Newtonsoft.Json.Converters
return newChild; return newChild;
} }
public string NamespaceUri public string NamespaceUri => _node.NamespaceURI;
{
get { return _node.NamespaceURI; }
}
} }
#endif #endif
#endregion #endregion
@@ -452,26 +414,20 @@ namespace Newtonsoft.Json.Converters
Declaration = declaration; Declaration = declaration;
} }
public override XmlNodeType NodeType public override XmlNodeType NodeType => XmlNodeType.XmlDeclaration;
{
get { return XmlNodeType.XmlDeclaration; }
}
public string Version public string Version => Declaration.Version;
{
get { return Declaration.Version; }
}
public string Encoding public string Encoding
{ {
get { return Declaration.Encoding; } get => Declaration.Encoding;
set { Declaration.Encoding = value; } set => Declaration.Encoding = value;
} }
public string Standalone public string Standalone
{ {
get { return Declaration.Standalone; } get => Declaration.Standalone;
set { Declaration.Standalone = value; } set => Declaration.Standalone = value;
} }
} }
@@ -485,38 +441,20 @@ namespace Newtonsoft.Json.Converters
_documentType = documentType; _documentType = documentType;
} }
public string Name public string Name => _documentType.Name;
{
get { return _documentType.Name; }
}
public string System public string System => _documentType.SystemId;
{
get { return _documentType.SystemId; }
}
public string Public public string Public => _documentType.PublicId;
{
get { return _documentType.PublicId; }
}
public string InternalSubset public string InternalSubset => _documentType.InternalSubset;
{
get { return _documentType.InternalSubset; }
}
public override string LocalName public override string LocalName => "DOCTYPE";
{
get { return "DOCTYPE"; }
}
} }
internal class XDocumentWrapper : XContainerWrapper, IXmlDocument internal class XDocumentWrapper : XContainerWrapper, IXmlDocument
{ {
private XDocument Document private XDocument Document => (XDocument)WrappedNode;
{
get { return (XDocument)WrappedNode; }
}
public XDocumentWrapper(XDocument document) public XDocumentWrapper(XDocument document)
: base(document) : base(document)
@@ -627,8 +565,7 @@ namespace Newtonsoft.Json.Converters
public override IXmlNode AppendChild(IXmlNode newChild) public override IXmlNode AppendChild(IXmlNode newChild)
{ {
XDeclarationWrapper declarationWrapper = newChild as XDeclarationWrapper; if (newChild is XDeclarationWrapper declarationWrapper)
if (declarationWrapper != null)
{ {
Document.Declaration = declarationWrapper.Declaration; Document.Declaration = declarationWrapper.Declaration;
return declarationWrapper; return declarationWrapper;
@@ -642,10 +579,7 @@ namespace Newtonsoft.Json.Converters
internal class XTextWrapper : XObjectWrapper internal class XTextWrapper : XObjectWrapper
{ {
private XText Text private XText Text => (XText)WrappedNode;
{
get { return (XText)WrappedNode; }
}
public XTextWrapper(XText text) public XTextWrapper(XText text)
: base(text) : base(text)
@@ -654,8 +588,8 @@ namespace Newtonsoft.Json.Converters
public override string Value public override string Value
{ {
get { return Text.Value; } get => Text.Value;
set { Text.Value = value; } set => Text.Value = value;
} }
public override IXmlNode ParentNode public override IXmlNode ParentNode
@@ -674,10 +608,7 @@ namespace Newtonsoft.Json.Converters
internal class XCommentWrapper : XObjectWrapper internal class XCommentWrapper : XObjectWrapper
{ {
private XComment Text private XComment Text => (XComment)WrappedNode;
{
get { return (XComment)WrappedNode; }
}
public XCommentWrapper(XComment text) public XCommentWrapper(XComment text)
: base(text) : base(text)
@@ -686,8 +617,8 @@ namespace Newtonsoft.Json.Converters
public override string Value public override string Value
{ {
get { return Text.Value; } get => Text.Value;
set { Text.Value = value; } set => Text.Value = value;
} }
public override IXmlNode ParentNode public override IXmlNode ParentNode
@@ -706,25 +637,19 @@ namespace Newtonsoft.Json.Converters
internal class XProcessingInstructionWrapper : XObjectWrapper internal class XProcessingInstructionWrapper : XObjectWrapper
{ {
private XProcessingInstruction ProcessingInstruction private XProcessingInstruction ProcessingInstruction => (XProcessingInstruction)WrappedNode;
{
get { return (XProcessingInstruction)WrappedNode; }
}
public XProcessingInstructionWrapper(XProcessingInstruction processingInstruction) public XProcessingInstructionWrapper(XProcessingInstruction processingInstruction)
: base(processingInstruction) : base(processingInstruction)
{ {
} }
public override string LocalName public override string LocalName => ProcessingInstruction.Target;
{
get { return ProcessingInstruction.Target; }
}
public override string Value public override string Value
{ {
get { return ProcessingInstruction.Data; } get => ProcessingInstruction.Data;
set { ProcessingInstruction.Data = value; } set => ProcessingInstruction.Data = value;
} }
} }
@@ -732,10 +657,7 @@ namespace Newtonsoft.Json.Converters
{ {
private List<IXmlNode> _childNodes; private List<IXmlNode> _childNodes;
private XContainer Container private XContainer Container => (XContainer)WrappedNode;
{
get { return (XContainer)WrappedNode; }
}
public XContainerWrapper(XContainer container) public XContainerWrapper(XContainer container)
: base(container) : base(container)
@@ -768,15 +690,7 @@ namespace Newtonsoft.Json.Converters
} }
} }
protected virtual bool HasChildNodes protected virtual bool HasChildNodes => Container.LastNode != null;
{
get
{
// use last node for performance
// container linked list starts with lastnode
return Container.LastNode != null;
}
}
public override IXmlNode ParentNode public override IXmlNode ParentNode
{ {
@@ -793,50 +707,42 @@ namespace Newtonsoft.Json.Converters
internal static IXmlNode WrapNode(XObject node) internal static IXmlNode WrapNode(XObject node)
{ {
XDocument document = node as XDocument; if (node is XDocument document)
if (document != null)
{ {
return new XDocumentWrapper(document); return new XDocumentWrapper(document);
} }
XElement element = node as XElement; if (node is XElement element)
if (element != null)
{ {
return new XElementWrapper(element); return new XElementWrapper(element);
} }
XContainer container = node as XContainer; if (node is XContainer container)
if (container != null)
{ {
return new XContainerWrapper(container); return new XContainerWrapper(container);
} }
XProcessingInstruction pi = node as XProcessingInstruction; if (node is XProcessingInstruction pi)
if (pi != null)
{ {
return new XProcessingInstructionWrapper(pi); return new XProcessingInstructionWrapper(pi);
} }
XText text = node as XText; if (node is XText text)
if (text != null)
{ {
return new XTextWrapper(text); return new XTextWrapper(text);
} }
XComment comment = node as XComment; if (node is XComment comment)
if (comment != null)
{ {
return new XCommentWrapper(comment); return new XCommentWrapper(comment);
} }
XAttribute attribute = node as XAttribute; if (node is XAttribute attribute)
if (attribute != null)
{ {
return new XAttributeWrapper(attribute); return new XAttributeWrapper(attribute);
} }
XDocumentType type = node as XDocumentType; if (node is XDocumentType type)
if (type != null)
{ {
return new XDocumentTypeWrapper(type); return new XDocumentTypeWrapper(type);
} }
@@ -862,40 +768,22 @@ namespace Newtonsoft.Json.Converters
_xmlObject = xmlObject; _xmlObject = xmlObject;
} }
public object WrappedNode public object WrappedNode => _xmlObject;
{
get { return _xmlObject; }
}
public virtual XmlNodeType NodeType public virtual XmlNodeType NodeType => _xmlObject.NodeType;
{
get { return _xmlObject.NodeType; }
}
public virtual string LocalName public virtual string LocalName => null;
{
get { return null; }
}
public virtual List<IXmlNode> ChildNodes public virtual List<IXmlNode> ChildNodes => XmlNodeConverter.EmptyChildNodes;
{
get { return XmlNodeConverter.EmptyChildNodes; }
}
public virtual List<IXmlNode> Attributes public virtual List<IXmlNode> Attributes => XmlNodeConverter.EmptyChildNodes;
{
get { return XmlNodeConverter.EmptyChildNodes; }
}
public virtual IXmlNode ParentNode public virtual IXmlNode ParentNode => null;
{
get { return null; }
}
public virtual string Value public virtual string Value
{ {
get { return null; } get => null;
set { throw new InvalidOperationException(); } set => throw new InvalidOperationException();
} }
public virtual IXmlNode AppendChild(IXmlNode newChild) public virtual IXmlNode AppendChild(IXmlNode newChild)
@@ -903,18 +791,12 @@ namespace Newtonsoft.Json.Converters
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
public virtual string NamespaceUri public virtual string NamespaceUri => null;
{
get { return null; }
}
} }
internal class XAttributeWrapper : XObjectWrapper internal class XAttributeWrapper : XObjectWrapper
{ {
private XAttribute Attribute private XAttribute Attribute => (XAttribute)WrappedNode;
{
get { return (XAttribute)WrappedNode; }
}
public XAttributeWrapper(XAttribute attribute) public XAttributeWrapper(XAttribute attribute)
: base(attribute) : base(attribute)
@@ -923,19 +805,13 @@ namespace Newtonsoft.Json.Converters
public override string Value public override string Value
{ {
get { return Attribute.Value; } get => Attribute.Value;
set { Attribute.Value = value; } set => Attribute.Value = value;
} }
public override string LocalName public override string LocalName => Attribute.Name.LocalName;
{
get { return Attribute.Name.LocalName; }
}
public override string NamespaceUri public override string NamespaceUri => Attribute.Name.NamespaceName;
{
get { return Attribute.Name.NamespaceName; }
}
public override IXmlNode ParentNode public override IXmlNode ParentNode
{ {
@@ -955,10 +831,7 @@ namespace Newtonsoft.Json.Converters
{ {
private List<IXmlNode> _attributes; private List<IXmlNode> _attributes;
private XElement Element private XElement Element => (XElement)WrappedNode;
{
get { return (XElement)WrappedNode; }
}
public XElementWrapper(XElement element) public XElementWrapper(XElement element)
: base(element) : base(element)
@@ -1044,29 +917,20 @@ namespace Newtonsoft.Json.Converters
public override string Value public override string Value
{ {
get { return Element.Value; } get => Element.Value;
set { Element.Value = value; } set => Element.Value = value;
} }
public override string LocalName public override string LocalName => Element.Name.LocalName;
{
get { return Element.Name.LocalName; }
}
public override string NamespaceUri public override string NamespaceUri => Element.Name.NamespaceName;
{
get { return Element.Name.NamespaceName; }
}
public string GetPrefixOfNamespace(string namespaceUri) public string GetPrefixOfNamespace(string namespaceUri)
{ {
return Element.GetPrefixOfNamespace(namespaceUri); return Element.GetPrefixOfNamespace(namespaceUri);
} }
public bool IsEmpty public bool IsEmpty => Element.IsEmpty;
{
get { return Element.IsEmpty; }
}
} }
#endif #endif
#endregion #endregion
@@ -1141,15 +1005,13 @@ namespace Newtonsoft.Json.Converters
private IXmlNode WrapXml(object value) private IXmlNode WrapXml(object value)
{ {
#if HAVE_XLINQ #if HAVE_XLINQ
XObject xObject = value as XObject; if (value is XObject xObject)
if (xObject != null)
{ {
return XContainerWrapper.WrapNode(xObject); return XContainerWrapper.WrapNode(xObject);
} }
#endif #endif
#if HAVE_XML_DOCUMENT #if HAVE_XML_DOCUMENT
XmlNode node = value as XmlNode; if (value is XmlNode node)
if (node != null)
{ {
return XmlNodeWrapper.WrapNode(node); return XmlNodeWrapper.WrapNode(node);
} }
@@ -1329,15 +1191,13 @@ namespace Newtonsoft.Json.Converters
} }
else else
{ {
object value; if (!nodesGroupedByName.TryGetValue(currentNodeName, out object value))
if (!nodesGroupedByName.TryGetValue(currentNodeName, out value))
{ {
nodesGroupedByName.Add(currentNodeName, childNode); nodesGroupedByName.Add(currentNodeName, childNode);
} }
else else
{ {
List<IXmlNode> nodes = value as List<IXmlNode>; if (!(value is List<IXmlNode> nodes))
if (nodes == null)
{ {
nodes = new List<IXmlNode> {(IXmlNode)value}; nodes = new List<IXmlNode> {(IXmlNode)value};
nodesGroupedByName[currentNodeName] = nodes; nodesGroupedByName[currentNodeName] = nodes;
@@ -1358,8 +1218,7 @@ namespace Newtonsoft.Json.Converters
// write multiple names together in an array // write multiple names together in an array
foreach (KeyValuePair<string, object> nodeNameGroup in nodesGroupedByName) foreach (KeyValuePair<string, object> nodeNameGroup in nodesGroupedByName)
{ {
List<IXmlNode> nodes = nodeNameGroup.Value as List<IXmlNode>; if (nodeNameGroup.Value is List<IXmlNode> nodes)
if (nodes != null)
{ {
WriteGroupedNodes(writer, manager, writePropertyName, nodes, nodeNameGroup.Key); WriteGroupedNodes(writer, manager, writePropertyName, nodes, nodeNameGroup.Key);
} }
@@ -1376,16 +1235,7 @@ namespace Newtonsoft.Json.Converters
private void WriteGroupedNodes(JsonWriter writer, XmlNamespaceManager manager, bool writePropertyName, List<IXmlNode> groupedNodes, string elementNames) private void WriteGroupedNodes(JsonWriter writer, XmlNamespaceManager manager, bool writePropertyName, List<IXmlNode> groupedNodes, string elementNames)
{ {
bool writeArray; bool writeArray = groupedNodes.Count != 1 || IsArray(groupedNodes[0]);
if (groupedNodes.Count == 1)
{
writeArray = IsArray(groupedNodes[0]);
}
else
{
writeArray = true;
}
if (!writeArray) if (!writeArray)
{ {
+1 -1
View File
@@ -66,7 +66,7 @@ namespace Newtonsoft.Json
case JsonContainerType.Constructor: case JsonContainerType.Constructor:
return MathUtils.IntLength((ulong)Position) + 2; return MathUtils.IntLength((ulong)Position) + 2;
default: default:
throw new ArgumentOutOfRangeException("Type"); throw new ArgumentOutOfRangeException(nameof(Type));
} }
} }
+21 -36
View File
@@ -447,8 +447,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
int i; if (int.TryParse(s, NumberStyles.Integer, Culture, out int i))
if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
{ {
SetToken(JsonToken.Integer, i, false); SetToken(JsonToken.Integer, i, false);
return i; return i;
@@ -483,8 +482,7 @@ namespace Newtonsoft.Json
if (Value != null) if (Value != null)
{ {
string s; string s;
IFormattable formattable = Value as IFormattable; if (Value is IFormattable formattable)
if (formattable != null)
{ {
s = formattable.ToString(null, Culture); s = formattable.ToString(null, Culture);
} }
@@ -535,12 +533,11 @@ namespace Newtonsoft.Json
byte[] data; byte[] data;
Guid g;
if (s.Length == 0) if (s.Length == 0)
{ {
data = CollectionUtils.ArrayEmpty<byte>(); data = CollectionUtils.ArrayEmpty<byte>();
} }
else if (ConvertUtils.TryConvertGuid(s, out g)) else if (ConvertUtils.TryConvertGuid(s, out Guid g))
{ {
data = g.ToByteArray(); data = g.ToByteArray();
} }
@@ -630,9 +627,9 @@ namespace Newtonsoft.Json
{ {
double d; double d;
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (Value is BigInteger) if (Value is BigInteger value)
{ {
d = (double)(BigInteger)Value; d = (double)value;
} }
else else
#endif #endif
@@ -659,8 +656,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
double d; if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, Culture, out double d))
if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, Culture, out d))
{ {
SetToken(JsonToken.Float, d, false); SetToken(JsonToken.Float, d, false);
return d; return d;
@@ -690,9 +686,9 @@ namespace Newtonsoft.Json
case JsonToken.Float: case JsonToken.Float:
bool b; bool b;
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (Value is BigInteger) if (Value is BigInteger integer)
{ {
b = (BigInteger)Value != 0; b = integer != 0;
} }
else else
#endif #endif
@@ -720,8 +716,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
bool b; if (bool.TryParse(s, out bool b))
if (bool.TryParse(s, out b))
{ {
SetToken(JsonToken.Boolean, b, false); SetToken(JsonToken.Boolean, b, false);
return b; return b;
@@ -753,9 +748,9 @@ namespace Newtonsoft.Json
{ {
decimal d; decimal d;
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
if (Value is BigInteger) if (Value is BigInteger value)
{ {
d = (decimal)(BigInteger)Value; d = (decimal)value;
} }
else else
#endif #endif
@@ -782,8 +777,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
decimal d; if (decimal.TryParse(s, NumberStyles.Number, Culture, out decimal d))
if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
{ {
SetToken(JsonToken.Float, d, false); SetToken(JsonToken.Float, d, false);
return d; return d;
@@ -809,9 +803,9 @@ namespace Newtonsoft.Json
return null; return null;
case JsonToken.Date: case JsonToken.Date:
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (Value is DateTimeOffset) if (Value is DateTimeOffset offset)
{ {
SetToken(JsonToken.Date, ((DateTimeOffset)Value).DateTime, false); SetToken(JsonToken.Date, offset.DateTime, false);
} }
#endif #endif
@@ -832,8 +826,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
DateTime dt; if (DateTimeUtils.TryParseDateTime(s, DateTimeZoneHandling, _dateFormatString, Culture, out DateTime dt))
if (DateTimeUtils.TryParseDateTime(s, DateTimeZoneHandling, _dateFormatString, Culture, out dt))
{ {
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling); dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
SetToken(JsonToken.Date, dt, false); SetToken(JsonToken.Date, dt, false);
@@ -866,9 +859,9 @@ namespace Newtonsoft.Json
case JsonToken.EndArray: case JsonToken.EndArray:
return null; return null;
case JsonToken.Date: case JsonToken.Date:
if (Value is DateTime) if (Value is DateTime time)
{ {
SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value), false); SetToken(JsonToken.Date, new DateTimeOffset(time), false);
} }
return (DateTimeOffset)Value; return (DateTimeOffset)Value;
@@ -888,8 +881,7 @@ namespace Newtonsoft.Json
return null; return null;
} }
DateTimeOffset dt; if (DateTimeUtils.TryParseDateTimeOffset(s, _dateFormatString, Culture, out DateTimeOffset dt))
if (DateTimeUtils.TryParseDateTimeOffset(s, _dateFormatString, Culture, out dt))
{ {
SetToken(JsonToken.Date, dt, false); SetToken(JsonToken.Date, dt, false);
return dt; return dt;
@@ -1102,14 +1094,7 @@ namespace Newtonsoft.Json
private void SetFinished() private void SetFinished()
{ {
if (SupportMultipleContent) _currentState = SupportMultipleContent ? State.Start : State.Finished;
{
_currentState = State.Start;
}
else
{
_currentState = State.Finished;
}
} }
private JsonContainerType GetTypeForCloseToken(JsonToken token) private JsonContainerType GetTypeForCloseToken(JsonToken token)
@@ -1181,7 +1166,7 @@ namespace Newtonsoft.Json
return Read(); return Read();
} }
ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read; ReadType t = contract?.InternalReadType ?? ReadType.Read;
switch (t) switch (t)
{ {
@@ -1194,7 +1179,7 @@ namespace Newtonsoft.Json
bool result = ReadAndMoveToContent(); bool result = ReadAndMoveToContent();
if (TokenType == JsonToken.Undefined) if (TokenType == JsonToken.Undefined)
{ {
throw JsonReaderException.Create(this, "An undefined token is not a valid {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); throw JsonReaderException.Create(this, "An undefined token is not a valid {0}.".FormatWith(CultureInfo.InvariantCulture, contract?.UnderlyingType ?? typeof(long)));
} }
return result; return result;
case ReadType.ReadAsDecimal: case ReadType.ReadAsDecimal:
+3 -6
View File
@@ -950,11 +950,9 @@ namespace Newtonsoft.Json
previousDateFormatString = null; previousDateFormatString = null;
} }
JsonTextReader textReader = reader as JsonTextReader; if (reader is JsonTextReader textReader)
if (textReader != null)
{ {
DefaultContractResolver resolver = _contractResolver as DefaultContractResolver; if (_contractResolver is DefaultContractResolver resolver)
if (resolver != null)
{ {
textReader.NameTable = resolver.GetNameTable(); textReader.NameTable = resolver.GetNameTable();
} }
@@ -989,8 +987,7 @@ namespace Newtonsoft.Json
reader.DateFormatString = previousDateFormatString; reader.DateFormatString = previousDateFormatString;
} }
JsonTextReader textReader = reader as JsonTextReader; if (reader is JsonTextReader textReader)
if (textReader != null)
{ {
textReader.NameTable = null; textReader.NameTable = null;
} }
+13 -14
View File
@@ -283,11 +283,11 @@ namespace Newtonsoft.Json
} }
int levelsToComplete = CalculateLevelsToComplete(type); int levelsToComplete = CalculateLevelsToComplete(type);
Task t;
while (levelsToComplete-- > 0) while (levelsToComplete-- > 0)
{ {
JsonToken token = GetCloseTokenForType(Pop()); JsonToken token = GetCloseTokenForType(Pop());
Task t;
if (_currentState == State.Property) if (_currentState == State.Property)
{ {
t = WriteNullAsync(cancellationToken); t = WriteNullAsync(cancellationToken);
@@ -701,24 +701,24 @@ namespace Newtonsoft.Json
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
return return
#if HAVE_BIG_INTEGER #if HAVE_BIG_INTEGER
value is BigInteger ? WriteValueAsync((BigInteger)value, cancellationToken) : value is BigInteger integer ? WriteValueAsync(integer, cancellationToken) :
#endif #endif
WriteValueAsync(Convert.ToInt64(value, CultureInfo.InvariantCulture), cancellationToken); WriteValueAsync(Convert.ToInt64(value, CultureInfo.InvariantCulture), cancellationToken);
case JsonToken.Float: case JsonToken.Float:
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
if (value is decimal) if (value is decimal dec)
{ {
return WriteValueAsync((decimal)value, cancellationToken); return WriteValueAsync(dec, cancellationToken);
} }
if (value is double) if (value is double doub)
{ {
return WriteValueAsync((double)value, cancellationToken); return WriteValueAsync(doub, cancellationToken);
} }
if (value is float) if (value is float f)
{ {
return WriteValueAsync((float)value, cancellationToken); return WriteValueAsync(f, cancellationToken);
} }
return WriteValueAsync(Convert.ToDouble(value, CultureInfo.InvariantCulture), cancellationToken); return WriteValueAsync(Convert.ToDouble(value, CultureInfo.InvariantCulture), cancellationToken);
@@ -740,9 +740,9 @@ namespace Newtonsoft.Json
return WriteEndConstructorAsync(cancellationToken); return WriteEndConstructorAsync(cancellationToken);
case JsonToken.Date: case JsonToken.Date:
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
if (value is DateTimeOffset) if (value is DateTimeOffset offset)
{ {
return WriteValueAsync((DateTimeOffset)value, cancellationToken); return WriteValueAsync(offset, cancellationToken);
} }
return WriteValueAsync(Convert.ToDateTime(value, CultureInfo.InvariantCulture), cancellationToken); return WriteValueAsync(Convert.ToDateTime(value, CultureInfo.InvariantCulture), cancellationToken);
@@ -750,9 +750,9 @@ namespace Newtonsoft.Json
return WriteRawValueAsync(value?.ToString(), cancellationToken); return WriteRawValueAsync(value?.ToString(), cancellationToken);
case JsonToken.Bytes: case JsonToken.Bytes:
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
if (value is Guid) if (value is Guid guid)
{ {
return WriteValueAsync((Guid)value, cancellationToken); return WriteValueAsync(guid, cancellationToken);
} }
return WriteValueAsync((byte[])value, cancellationToken); return WriteValueAsync((byte[])value, cancellationToken);
@@ -1772,8 +1772,7 @@ namespace Newtonsoft.Json
#endif #endif
default: default:
#if HAVE_ICONVERTIBLE #if HAVE_ICONVERTIBLE
IConvertible convertable = value as IConvertible; if (value is IConvertible convertable)
if (convertable != null)
{ {
// the value is a non-standard IConvertible // the value is a non-standard IConvertible
// convert to the underlying value and retry // convert to the underlying value and retry
+6 -7
View File
@@ -142,7 +142,7 @@ namespace Newtonsoft.Json
{ {
get get
{ {
int depth = (_stack != null) ? _stack.Count : 0; int depth = _stack?.Count ?? 0;
if (Peek() != JsonContainerType.None) if (Peek() != JsonContainerType.None)
{ {
depth++; depth++;
@@ -601,9 +601,9 @@ namespace Newtonsoft.Json
case JsonToken.Date: case JsonToken.Date:
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
#if HAVE_DATE_TIME_OFFSET #if HAVE_DATE_TIME_OFFSET
if (value is DateTimeOffset) if (value is DateTimeOffset dt)
{ {
WriteValue((DateTimeOffset)value); WriteValue(dt);
} }
else else
#endif #endif
@@ -616,9 +616,9 @@ namespace Newtonsoft.Json
break; break;
case JsonToken.Bytes: case JsonToken.Bytes:
ValidationUtils.ArgumentNotNull(value, nameof(value)); ValidationUtils.ArgumentNotNull(value, nameof(value));
if (value is Guid) if (value is Guid guid)
{ {
WriteValue((Guid)value); WriteValue(guid);
} }
else else
{ {
@@ -1614,8 +1614,7 @@ namespace Newtonsoft.Json
#endif #endif
default: default:
#if HAVE_ICONVERTIBLE #if HAVE_ICONVERTIBLE
IConvertible convertible = value as IConvertible; if (value is IConvertible convertible)
if (convertible != null)
{ {
// the value is a non-standard IConvertible // the value is a non-standard IConvertible
// convert to the underlying value and retry // convert to the underlying value and retry