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