diff --git a/Src/Directory.Build.props b/Src/Directory.Build.props index a570e7e9..3df27f22 100644 --- a/Src/Directory.Build.props +++ b/Src/Directory.Build.props @@ -6,7 +6,7 @@ 2.9.2 4.3.0 1.0.0-beta2-19367-01 - 3.3.0-beta1-final + 3.3.1-beta3-final 15.7.2 4.8.1 3.11.0 diff --git a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs index bd45657e..7390503a 100644 --- a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs +++ b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs @@ -71,7 +71,7 @@ namespace Newtonsoft.Json.Converters if (value.GetType().FullName == BinaryTypeName) { EnsureReflectionObject(value.GetType()); - Debug.Assert(_reflectionObject != null); + MiscellaneousUtils.Assert(_reflectionObject != null); return (byte[])_reflectionObject.GetValue(value, BinaryToArrayName)!; } @@ -142,7 +142,7 @@ namespace Newtonsoft.Json.Converters if (t.FullName == BinaryTypeName) { EnsureReflectionObject(t); - Debug.Assert(_reflectionObject != null); + MiscellaneousUtils.Assert(_reflectionObject != null); return _reflectionObject.Creator!(data); } diff --git a/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs b/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs index f7a90fb8..4670dba4 100644 --- a/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs +++ b/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs @@ -60,7 +60,7 @@ namespace Newtonsoft.Json.Converters } EnsureReflectionObject(value.GetType()); - Debug.Assert(_reflectionObject != null); + MiscellaneousUtils.Assert(_reflectionObject != null); DefaultContractResolver? resolver = serializer.ContractResolver as DefaultContractResolver; @@ -117,7 +117,7 @@ namespace Newtonsoft.Json.Converters public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { EnsureReflectionObject(objectType); - Debug.Assert(_reflectionObject != null); + MiscellaneousUtils.Assert(_reflectionObject != null); object entityKeyMember = _reflectionObject.Creator!(); diff --git a/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs b/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs index 49977b5e..d9c8ee23 100644 --- a/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs @@ -57,7 +57,7 @@ namespace Newtonsoft.Json.Converters public string? DateTimeFormat { get => _dateTimeFormat ?? string.Empty; - set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value; + set => _dateTimeFormat = (StringUtils.IsNullOrEmpty(value)) ? null : value; } /// @@ -162,7 +162,7 @@ namespace Newtonsoft.Json.Converters string? dateText = reader.Value?.ToString(); - if (string.IsNullOrEmpty(dateText) && nullable) + if (StringUtils.IsNullOrEmpty(dateText) && nullable) { return null; } @@ -170,7 +170,7 @@ namespace Newtonsoft.Json.Converters #if HAVE_DATE_TIME_OFFSET if (t == typeof(DateTimeOffset)) { - if (!string.IsNullOrEmpty(_dateTimeFormat)) + if (!StringUtils.IsNullOrEmpty(_dateTimeFormat)) { return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); } @@ -181,7 +181,7 @@ namespace Newtonsoft.Json.Converters } #endif - if (!string.IsNullOrEmpty(_dateTimeFormat)) + if (!StringUtils.IsNullOrEmpty(_dateTimeFormat)) { return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles); } diff --git a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs index 550c02d9..9ba6b632 100644 --- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs +++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs @@ -230,7 +230,7 @@ namespace Newtonsoft.Json.Converters { string? enumText = reader.Value?.ToString(); - if (string.IsNullOrEmpty(enumText) && isNullable) + if (StringUtils.IsNullOrEmpty(enumText) && isNullable) { return null; } diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index c3205ece..2abb677a 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -882,9 +882,9 @@ namespace Newtonsoft.Json.Converters private bool HasImplicitNamespaceAttribute(string namespaceUri) { - if (!string.IsNullOrEmpty(namespaceUri) && namespaceUri != ParentNode?.NamespaceUri) + if (!StringUtils.IsNullOrEmpty(namespaceUri) && namespaceUri != ParentNode?.NamespaceUri) { - if (string.IsNullOrEmpty(GetPrefixOfNamespace(namespaceUri))) + if (StringUtils.IsNullOrEmpty(GetPrefixOfNamespace(namespaceUri))) { bool namespaceDeclared = false; @@ -892,7 +892,7 @@ namespace Newtonsoft.Json.Converters { foreach (XAttribute attribute in Element.Attributes()) { - if (attribute.Name.LocalName == "xmlns" && string.IsNullOrEmpty(attribute.Name.NamespaceName) && attribute.Value == namespaceUri) + if (attribute.Name.LocalName == "xmlns" && StringUtils.IsNullOrEmpty(attribute.Name.NamespaceName) && attribute.Value == namespaceUri) { namespaceDeclared = true; } @@ -1072,7 +1072,7 @@ namespace Newtonsoft.Json.Converters ? null : manager.LookupPrefix(node.NamespaceUri); - if (!string.IsNullOrEmpty(prefix)) + if (!StringUtils.IsNullOrEmpty(prefix)) { return prefix + ":" + XmlConvert.DecodeName(node.LocalName); } @@ -1405,17 +1405,17 @@ namespace Newtonsoft.Json.Converters writer.WritePropertyName(GetPropertyName(node, manager)); writer.WriteStartObject(); - if (!string.IsNullOrEmpty(declaration.Version)) + if (!StringUtils.IsNullOrEmpty(declaration.Version)) { writer.WritePropertyName("@version"); writer.WriteValue(declaration.Version); } - if (!string.IsNullOrEmpty(declaration.Encoding)) + if (!StringUtils.IsNullOrEmpty(declaration.Encoding)) { writer.WritePropertyName("@encoding"); writer.WriteValue(declaration.Encoding); } - if (!string.IsNullOrEmpty(declaration.Standalone)) + if (!StringUtils.IsNullOrEmpty(declaration.Standalone)) { writer.WritePropertyName("@standalone"); writer.WriteValue(declaration.Standalone); @@ -1428,22 +1428,22 @@ namespace Newtonsoft.Json.Converters writer.WritePropertyName(GetPropertyName(node, manager)); writer.WriteStartObject(); - if (!string.IsNullOrEmpty(documentType.Name)) + if (!StringUtils.IsNullOrEmpty(documentType.Name)) { writer.WritePropertyName("@name"); writer.WriteValue(documentType.Name); } - if (!string.IsNullOrEmpty(documentType.Public)) + if (!StringUtils.IsNullOrEmpty(documentType.Public)) { writer.WritePropertyName("@public"); writer.WriteValue(documentType.Public); } - if (!string.IsNullOrEmpty(documentType.System)) + if (!StringUtils.IsNullOrEmpty(documentType.System)) { writer.WritePropertyName("@system"); writer.WriteValue(documentType.System); } - if (!string.IsNullOrEmpty(documentType.InternalSubset)) + if (!StringUtils.IsNullOrEmpty(documentType.InternalSubset)) { writer.WritePropertyName("@internalSubset"); writer.WriteValue(documentType.InternalSubset); @@ -1537,7 +1537,7 @@ namespace Newtonsoft.Json.Converters throw JsonSerializationException.Create(reader, "Unexpected type when converting XML: " + objectType); } - if (!string.IsNullOrEmpty(DeserializeRootElementName)) + if (!StringUtils.IsNullOrEmpty(DeserializeRootElementName)) { ReadElement(reader, document, rootNode, DeserializeRootElementName, manager); } @@ -1586,7 +1586,7 @@ namespace Newtonsoft.Json.Converters return; default: // processing instructions and the xml declaration start with ? - if (!string.IsNullOrEmpty(propertyName) && propertyName[0] == '?') + if (!StringUtils.IsNullOrEmpty(propertyName) && propertyName[0] == '?') { CreateInstruction(reader, document, currentNode, propertyName); return; @@ -1616,7 +1616,7 @@ namespace Newtonsoft.Json.Converters private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { - if (string.IsNullOrEmpty(propertyName)) + if (StringUtils.IsNullOrEmpty(propertyName)) { throw JsonSerializationException.Create(reader, "XmlNodeConverter cannot convert JSON with an empty property name to XML."); } @@ -1685,7 +1685,7 @@ namespace Newtonsoft.Json.Converters string encodedName = XmlConvert.EncodeName(nameValue.Key); string? attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); - IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value) : document.CreateAttribute(encodedName, nameValue.Value); + IXmlNode attribute = (!StringUtils.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value) : document.CreateAttribute(encodedName, nameValue.Value); element.SetAttributeNode(attribute); } @@ -1733,7 +1733,7 @@ namespace Newtonsoft.Json.Converters string encodedName = XmlConvert.EncodeName(attributeName); string? attributeValue = ConvertTokenToXmlValue(reader); - IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) + IXmlNode attribute = (!StringUtils.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix), attributeValue) : document.CreateAttribute(encodedName, attributeValue); @@ -1876,7 +1876,7 @@ namespace Newtonsoft.Json.Converters case JsonToken.PropertyName: string attributeName = reader.Value!.ToString(); - if (!string.IsNullOrEmpty(attributeName)) + if (!StringUtils.IsNullOrEmpty(attributeName)) { char firstChar = attributeName[0]; string? attributeValue; @@ -2058,9 +2058,9 @@ namespace Newtonsoft.Json.Converters private IXmlElement CreateElement(string elementName, IXmlDocument document, string? elementPrefix, XmlNamespaceManager manager) { string encodeName = EncodeSpecialCharacters ? XmlConvert.EncodeLocalName(elementName) : XmlConvert.EncodeName(elementName); - string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); + string ns = StringUtils.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); - IXmlElement element = (!string.IsNullOrEmpty(ns)) ? document.CreateElement(encodeName, ns) : document.CreateElement(encodeName); + IXmlElement element = (!StringUtils.IsNullOrEmpty(ns)) ? document.CreateElement(encodeName, ns) : document.CreateElement(encodeName); return element; } @@ -2092,7 +2092,7 @@ namespace Newtonsoft.Json.Converters if (count == 1 && WriteArrayAttribute) { MiscellaneousUtils.GetQualifiedNameParts(propertyName, out string? elementPrefix, out string localName); - string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); + string ns = StringUtils.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); foreach (IXmlNode childNode in currentNode.ChildNodes) { diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index ba19d3d3..736ee0df 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -445,7 +445,7 @@ namespace Newtonsoft.Json internal int? ReadInt32String(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; @@ -656,7 +656,7 @@ namespace Newtonsoft.Json internal double? ReadDoubleString(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; @@ -715,7 +715,7 @@ namespace Newtonsoft.Json internal bool? ReadBooleanString(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; @@ -786,7 +786,7 @@ namespace Newtonsoft.Json internal decimal? ReadDecimalString(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; @@ -840,7 +840,7 @@ namespace Newtonsoft.Json internal DateTime? ReadDateTimeString(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; @@ -895,7 +895,7 @@ namespace Newtonsoft.Json internal DateTimeOffset? ReadDateTimeOffsetString(string? s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { SetToken(JsonToken.Null, null, false); return null; diff --git a/Src/Newtonsoft.Json/JsonTextReader.Async.cs b/Src/Newtonsoft.Json/JsonTextReader.Async.cs index 443c3055..7963f5b9 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.Async.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.Async.cs @@ -111,7 +111,7 @@ namespace Newtonsoft.Json private async Task ParsePostValueAsync(bool ignoreComments, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -196,7 +196,7 @@ namespace Newtonsoft.Json private async Task ReadFromFinishedAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (await EnsureCharsAsync(0, false, cancellationToken).ConfigureAwait(false)) { @@ -227,7 +227,7 @@ namespace Newtonsoft.Json private async Task ReadDataAsync(bool append, int charsRequired, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (_isEndOfFile) { @@ -251,7 +251,7 @@ namespace Newtonsoft.Json private async Task ParseValueAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -382,7 +382,7 @@ namespace Newtonsoft.Json private async Task ReadStringIntoBufferAsync(char quote, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int charPos = _charPos; int initialPosition = _charPos; @@ -600,7 +600,7 @@ namespace Newtonsoft.Json private async Task ParseObjectAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -659,7 +659,7 @@ namespace Newtonsoft.Json private async Task ParseCommentAsync(bool setToken, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // should have already parsed / character before reaching this method _charPos++; @@ -757,7 +757,7 @@ namespace Newtonsoft.Json private async Task EatWhitespaceAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -815,7 +815,7 @@ namespace Newtonsoft.Json private async Task MatchValueWithTrailingSeparatorAsync(string value, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // will match value and then move to the next character, checking that it is a separator character if (!await MatchValueAsync(value, cancellationToken).ConfigureAwait(false)) @@ -860,7 +860,7 @@ namespace Newtonsoft.Json private async Task ParseConstructorAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (await MatchValueWithTrailingSeparatorAsync("new", cancellationToken).ConfigureAwait(false)) { @@ -960,7 +960,7 @@ namespace Newtonsoft.Json private async Task ParseNumberAsync(ReadType readType, CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); ShiftBufferIfNeeded(); @@ -979,7 +979,7 @@ namespace Newtonsoft.Json private async Task ParsePropertyAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); char firstChar = _chars[_charPos]; char quoteChar; @@ -1033,7 +1033,7 @@ namespace Newtonsoft.Json private async Task ReadNumberIntoBufferAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int charPos = _charPos; @@ -1069,7 +1069,7 @@ namespace Newtonsoft.Json private async Task ParseUnquotedPropertyAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int initialPosition = _charPos; @@ -1120,7 +1120,7 @@ namespace Newtonsoft.Json private async Task HandleNullAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false)) { @@ -1140,7 +1140,7 @@ namespace Newtonsoft.Json private async Task ReadFinishedAsync(CancellationToken cancellationToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (await EnsureCharsAsync(0, false, cancellationToken).ConfigureAwait(false)) { @@ -1167,7 +1167,7 @@ namespace Newtonsoft.Json private async Task ReadStringValueAsync(ReadType readType, CancellationToken cancellationToken) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -1303,7 +1303,7 @@ namespace Newtonsoft.Json private async Task ReadNumberValueAsync(ReadType readType, CancellationToken cancellationToken) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -1430,7 +1430,7 @@ namespace Newtonsoft.Json internal async Task DoReadAsBooleanAsync(CancellationToken cancellationToken) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -1566,7 +1566,7 @@ namespace Newtonsoft.Json internal async Task DoReadAsBytesAsync(CancellationToken cancellationToken) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); bool isWrapped = false; diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index b95cb821..0ef5b0e5 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -138,7 +138,7 @@ namespace Newtonsoft.Json private void SetNewLine(bool hasNextChar) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (hasNextChar && _chars[_charPos] == StringUtils.LineFeed) { @@ -252,7 +252,7 @@ namespace Newtonsoft.Json private void ShiftBufferIfNeeded() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // once in the last 10% of the buffer, or buffer is already very large then // shift the remaining content to the start to avoid unnecessarily increasing @@ -280,7 +280,7 @@ namespace Newtonsoft.Json private void PrepareBufferForReadData(bool append, int charsRequired) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // char buffer is full if (_charsUsed + charsRequired >= _chars.Length - 1) @@ -345,7 +345,7 @@ namespace Newtonsoft.Json } PrepareBufferForReadData(append, charsRequired); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int attemptCharReadCount = _chars.Length - _charsUsed - 1; @@ -414,7 +414,7 @@ namespace Newtonsoft.Json public override bool Read() { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -497,7 +497,7 @@ namespace Newtonsoft.Json public override byte[]? ReadAsBytes() { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); bool isWrapped = false; @@ -603,7 +603,7 @@ namespace Newtonsoft.Json private object? ReadStringValue(ReadType readType) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -770,7 +770,7 @@ namespace Newtonsoft.Json public override bool? ReadAsBoolean() { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -908,7 +908,7 @@ namespace Newtonsoft.Json private object? ReadNumberValue(ReadType readType) { EnsureBuffer(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (_currentState) { @@ -1062,7 +1062,7 @@ namespace Newtonsoft.Json private void HandleNull() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (EnsureChars(1, true)) { @@ -1084,7 +1084,7 @@ namespace Newtonsoft.Json private void ReadFinished() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (EnsureChars(0, false)) { @@ -1135,7 +1135,7 @@ namespace Newtonsoft.Json private void ReadStringIntoBuffer(char quote) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int charPos = _charPos; int initialPosition = _charPos; @@ -1290,7 +1290,7 @@ namespace Newtonsoft.Json private void FinishReadStringIntoBuffer(int charPos, int initialPosition, int lastWritePosition) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (initialPosition == lastWritePosition) { @@ -1313,7 +1313,7 @@ namespace Newtonsoft.Json private void WriteCharToBuffer(char writeChar, int lastWritePosition, int writeToPosition) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (writeToPosition > lastWritePosition) { @@ -1325,7 +1325,7 @@ namespace Newtonsoft.Json private char ConvertUnicode(bool enoughChars) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (enoughChars) { @@ -1353,7 +1353,7 @@ namespace Newtonsoft.Json private void ReadNumberIntoBuffer() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int charPos = _charPos; @@ -1439,7 +1439,7 @@ namespace Newtonsoft.Json private bool ParsePostValue(bool ignoreComments) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -1521,7 +1521,7 @@ namespace Newtonsoft.Json private bool ParseObject() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -1577,7 +1577,7 @@ namespace Newtonsoft.Json private bool ParseProperty() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); char firstChar = _chars[_charPos]; char quoteChar; @@ -1640,7 +1640,7 @@ namespace Newtonsoft.Json private void ParseUnquotedProperty() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); int initialPosition = _charPos; @@ -1673,7 +1673,7 @@ namespace Newtonsoft.Json private bool ReadUnquotedPropertyReportIfDone(char currentChar, int initialPosition) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (ValidIdentifierChar(currentChar)) { @@ -1692,7 +1692,7 @@ namespace Newtonsoft.Json private bool ParseValue() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -1834,7 +1834,7 @@ namespace Newtonsoft.Json private void EatWhitespace() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); while (true) { @@ -1877,7 +1877,7 @@ namespace Newtonsoft.Json private void ParseConstructor() { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (MatchValueWithTrailingSeparator("new")) { @@ -1963,7 +1963,7 @@ namespace Newtonsoft.Json private void ParseNumber(ReadType readType) { ShiftBufferIfNeeded(); - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); char firstChar = _chars[_charPos]; int initialPosition = _charPos; @@ -1975,7 +1975,7 @@ namespace Newtonsoft.Json private void ParseReadNumber(ReadType readType, char firstChar, int initialPosition) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // set state to PostValue now so that if there is an error parsing the number then the reader can continue SetPostValueState(true); @@ -2257,7 +2257,7 @@ namespace Newtonsoft.Json private void ParseComment(bool setToken) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // should have already parsed / character before reaching this method _charPos++; @@ -2364,7 +2364,7 @@ namespace Newtonsoft.Json private bool MatchValue(bool enoughChars, string value) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); if (!enoughChars) { @@ -2388,7 +2388,7 @@ namespace Newtonsoft.Json private bool MatchValueWithTrailingSeparator(string value) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); // will match value and then move to the next character, checking that it is a separator character bool match = MatchValue(value); @@ -2408,7 +2408,7 @@ namespace Newtonsoft.Json private bool IsSeparator(char c) { - Debug.Assert(_chars != null); + MiscellaneousUtils.Assert(_chars != null); switch (c) { diff --git a/Src/Newtonsoft.Json/JsonTextWriter.Async.cs b/Src/Newtonsoft.Json/JsonTextWriter.Async.cs index 89ae0ab7..c1c763e9 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.Async.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.Async.cs @@ -165,7 +165,7 @@ namespace Newtonsoft.Json int currentIndentCount = Top * _indentation; int newLineLen = SetIndentChars(); - Debug.Assert(_indentChars != null); + MiscellaneousUtils.Assert(_indentChars != null); if (currentIndentCount <= IndentCharBufferSize) { @@ -177,7 +177,7 @@ namespace Newtonsoft.Json private async Task WriteIndentAsync(int currentIndentCount, int newLineLen, CancellationToken cancellationToken) { - Debug.Assert(_indentChars != null); + MiscellaneousUtils.Assert(_indentChars != null); await _writer.WriteAsync(_indentChars, 0, newLineLen + Math.Min(currentIndentCount, IndentCharBufferSize), cancellationToken).ConfigureAwait(false); @@ -657,7 +657,7 @@ namespace Newtonsoft.Json await InternalWriteValueAsync(JsonToken.Date, cancellationToken).ConfigureAwait(false); value = DateTimeUtils.EnsureDateTime(value, DateTimeZoneHandling); - if (string.IsNullOrEmpty(DateFormatString)) + if (StringUtils.IsNullOrEmpty(DateFormatString)) { int length = WriteValueToBuffer(value); @@ -706,7 +706,7 @@ namespace Newtonsoft.Json { await InternalWriteValueAsync(JsonToken.Date, cancellationToken).ConfigureAwait(false); - if (string.IsNullOrEmpty(DateFormatString)) + if (StringUtils.IsNullOrEmpty(DateFormatString)) { int length = WriteValueToBuffer(value); diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 2bb21ee8..b08dfb48 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -633,7 +633,7 @@ namespace Newtonsoft.Json InternalWriteValue(JsonToken.Date); value = DateTimeUtils.EnsureDateTime(value, DateTimeZoneHandling); - if (string.IsNullOrEmpty(DateFormatString)) + if (StringUtils.IsNullOrEmpty(DateFormatString)) { int length = WriteValueToBuffer(value); @@ -650,7 +650,7 @@ namespace Newtonsoft.Json private int WriteValueToBuffer(DateTime value) { EnsureWriteBuffer(); - Debug.Assert(_writeBuffer != null); + MiscellaneousUtils.Assert(_writeBuffer != null); int pos = 0; _writeBuffer[pos++] = _quoteChar; @@ -688,7 +688,7 @@ namespace Newtonsoft.Json { InternalWriteValue(JsonToken.Date); - if (string.IsNullOrEmpty(DateFormatString)) + if (StringUtils.IsNullOrEmpty(DateFormatString)) { int length = WriteValueToBuffer(value); @@ -705,7 +705,7 @@ namespace Newtonsoft.Json private int WriteValueToBuffer(DateTimeOffset value) { EnsureWriteBuffer(); - Debug.Assert(_writeBuffer != null); + MiscellaneousUtils.Assert(_writeBuffer != null); int pos = 0; _writeBuffer[pos++] = _quoteChar; @@ -842,7 +842,7 @@ namespace Newtonsoft.Json } EnsureWriteBuffer(); - Debug.Assert(_writeBuffer != null); + MiscellaneousUtils.Assert(_writeBuffer != null); int totalLength = MathUtils.IntLength(value); @@ -894,7 +894,7 @@ namespace Newtonsoft.Json private int WriteNumberToBuffer(uint value, bool negative) { EnsureWriteBuffer(); - Debug.Assert(_writeBuffer != null); + MiscellaneousUtils.Assert(_writeBuffer != null); int totalLength = MathUtils.IntLength(value); diff --git a/Src/Newtonsoft.Json/Linq/JContainer.Async.cs b/Src/Newtonsoft.Json/Linq/JContainer.Async.cs index 455ed8ff..beb67afd 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.Async.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.Async.cs @@ -72,7 +72,7 @@ namespace Newtonsoft.Json.Linq parent = parent.Parent; } - Debug.Assert(parent != null); + MiscellaneousUtils.Assert(parent != null); switch (reader.TokenType) { diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 81833940..4b30e44e 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -765,7 +765,7 @@ namespace Newtonsoft.Json.Linq parent = parent.Parent; } - Debug.Assert(parent != null); + MiscellaneousUtils.Assert(parent != null); switch (r.TokenType) { diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index 5e2b998c..3805bd60 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -122,7 +122,7 @@ namespace Newtonsoft.Json.Linq throw new IndexOutOfRangeException(); } - Debug.Assert(_token != null); + MiscellaneousUtils.Assert(_token != null); return _token; } set diff --git a/Src/Newtonsoft.Json/Linq/JTokenReader.cs b/Src/Newtonsoft.Json/Linq/JTokenReader.cs index aad1150c..03480570 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenReader.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenReader.cs @@ -315,9 +315,9 @@ namespace Newtonsoft.Json.Linq _initialPath = _root.Path; } - if (!string.IsNullOrEmpty(_initialPath)) + if (!StringUtils.IsNullOrEmpty(_initialPath)) { - if (string.IsNullOrEmpty(path)) + if (StringUtils.IsNullOrEmpty(path)) { return _initialPath; } diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index ff089c55..d5aa1eff 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -356,7 +356,7 @@ namespace Newtonsoft.Json.Linq } byte[]? bytesA = objA as byte[]; - Debug.Assert(bytesA != null); + MiscellaneousUtils.Assert(bytesA != null); return MiscellaneousUtils.ByteArrayCompare(bytesA!, bytesB); case JTokenType.Guid: diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs b/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs index 69eee36c..f7bf1d5f 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/QueryExpression.cs @@ -294,8 +294,8 @@ namespace Newtonsoft.Json.Linq.JsonPath internal static bool EqualsWithStrictMatch(JValue value, JValue queryValue) { - Debug.Assert(value != null); - Debug.Assert(queryValue != null); + MiscellaneousUtils.Assert(value != null); + MiscellaneousUtils.Assert(queryValue != null); // Handle comparing an integer with a float // e.g. Comparing 1 and 1.0 diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs index c87ffd6f..445d3a6f 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs @@ -221,7 +221,7 @@ namespace Newtonsoft.Json.Schema } string location = token.Path.Replace(".", "/").Replace("[", "/").Replace("]", string.Empty); - if (!string.IsNullOrEmpty(location)) + if (!StringUtils.IsNullOrEmpty(location)) { location = "/" + location; } diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index e0b3e406..7cdab611 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -175,7 +175,7 @@ namespace Newtonsoft.Json.Schema { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (!string.IsNullOrEmpty(containerAttribute?.Title)) + if (!StringUtils.IsNullOrEmpty(containerAttribute?.Title)) { return containerAttribute.Title; } @@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Schema { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (!string.IsNullOrEmpty(containerAttribute?.Description)) + if (!StringUtils.IsNullOrEmpty(containerAttribute?.Description)) { return containerAttribute.Description; } @@ -204,7 +204,7 @@ namespace Newtonsoft.Json.Schema { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute(type); - if (!string.IsNullOrEmpty(containerAttribute?.Id)) + if (!StringUtils.IsNullOrEmpty(containerAttribute?.Id)) { return containerAttribute.Id; } @@ -232,7 +232,7 @@ namespace Newtonsoft.Json.Schema string resolvedId = GetTypeId(type, false); string explicitId = GetTypeId(type, true); - if (!string.IsNullOrEmpty(resolvedId)) + if (!StringUtils.IsNullOrEmpty(resolvedId)) { JsonSchema resolvedSchema = _resolver.GetSchema(resolvedId); if (resolvedSchema != null) diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index e60030d7..a2ce9dac 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -273,7 +273,7 @@ namespace Newtonsoft.Json.Serialization { if (_genericWrapperCreator == null) { - Debug.Assert(_genericCollectionDefinitionType != null); + MiscellaneousUtils.Assert(_genericCollectionDefinitionType != null); _genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(CollectionItemType); diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index 1979c599..183a5a72 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -307,7 +307,7 @@ namespace Newtonsoft.Json.Serialization internal void WritePropertyName(JsonWriter writer) { string? propertyName = PropertyName; - Debug.Assert(propertyName != null); + MiscellaneousUtils.Assert(propertyName != null); if (_skipPropertyNameEscape) { diff --git a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs index d631a83f..8b3d201c 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs @@ -73,7 +73,7 @@ namespace Newtonsoft.Json.Serialization /// The property to add to the collection. public void AddProperty(JsonProperty property) { - Debug.Assert(property.PropertyName != null); + MiscellaneousUtils.Assert(property.PropertyName != null); if (Contains(property.PropertyName)) { diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 5cb82550..97229a1b 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -351,7 +351,7 @@ namespace Newtonsoft.Json.Serialization private static bool CoerceEmptyStringToNull(Type? objectType, JsonContract? contract, string s) { - return string.IsNullOrEmpty(s) && objectType != null && objectType != typeof(string) && objectType != typeof(object) && contract != null && contract.IsNullable; + return StringUtils.IsNullOrEmpty(s) && objectType != null && objectType != typeof(string) && objectType != typeof(object) && contract != null && contract.IsNullable; } internal string GetExpectedDescription(JsonContract contract) @@ -464,8 +464,8 @@ namespace Newtonsoft.Json.Serialization return CreateJObject(reader); } - Debug.Assert(resolvedObjectType != null); - Debug.Assert(contract != null); + MiscellaneousUtils.Assert(resolvedObjectType != null); + MiscellaneousUtils.Assert(contract != null); switch (contract.ContractType) { @@ -840,8 +840,8 @@ namespace Newtonsoft.Json.Serialization return CreateJToken(reader, contract); } - Debug.Assert(objectType != null); - Debug.Assert(contract != null); + MiscellaneousUtils.Assert(objectType != null); + MiscellaneousUtils.Assert(contract != null); JsonArrayContract arrayContract = EnsureArrayContract(reader, objectType, contract); @@ -936,7 +936,7 @@ namespace Newtonsoft.Json.Serialization return value; } - Debug.Assert(contract != null); + MiscellaneousUtils.Assert(contract != null); Type? valueType = ReflectionUtils.GetObjectType(value); // type of value and type of target don't match diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index fc0ded0b..2a21a4f7 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -159,7 +159,7 @@ namespace Newtonsoft.Json.Serialization return; } - Debug.Assert(valueContract != null); + MiscellaneousUtils.Assert(valueContract != null); JsonConverter? converter = member?.Converter ?? @@ -252,7 +252,7 @@ namespace Newtonsoft.Json.Serialization return false; } - Debug.Assert(valueContract != null); + MiscellaneousUtils.Assert(valueContract != null); if (valueContract.ContractType == JsonContractType.Primitive || valueContract.ContractType == JsonContractType.String) { @@ -304,7 +304,7 @@ namespace Newtonsoft.Json.Serialization return true; } - Debug.Assert(contract != null); + MiscellaneousUtils.Assert(contract != null); if (contract.ContractType == JsonContractType.Primitive || contract.ContractType == JsonContractType.String) { diff --git a/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs b/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs index ebcd7b05..80ff463a 100644 --- a/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs @@ -54,13 +54,13 @@ namespace Newtonsoft.Json.Utilities // previous frameworks. public static Task FromCanceled(this CancellationToken cancellationToken) { - Debug.Assert(cancellationToken.IsCancellationRequested); + MiscellaneousUtils.Assert(cancellationToken.IsCancellationRequested); return new Task(() => {}, cancellationToken); } public static Task FromCanceled(this CancellationToken cancellationToken) { - Debug.Assert(cancellationToken.IsCancellationRequested); + MiscellaneousUtils.Assert(cancellationToken.IsCancellationRequested); #pragma warning disable CS8653 // A default expression introduces a null value for a type parameter. return new Task(() => default, cancellationToken); #pragma warning restore CS8653 // A default expression introduces a null value for a type parameter. @@ -73,25 +73,25 @@ namespace Newtonsoft.Json.Utilities public static Task WriteAsync(this TextWriter writer, char value, CancellationToken cancellationToken) { - Debug.Assert(writer != null); + MiscellaneousUtils.Assert(writer != null); return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value); } public static Task WriteAsync(this TextWriter writer, string? value, CancellationToken cancellationToken) { - Debug.Assert(writer != null); + MiscellaneousUtils.Assert(writer != null); return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value); } public static Task WriteAsync(this TextWriter writer, char[] value, int start, int count, CancellationToken cancellationToken) { - Debug.Assert(writer != null); + MiscellaneousUtils.Assert(writer != null); return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value, start, count); } public static Task ReadAsync(this TextReader reader, char[] buffer, int index, int count, CancellationToken cancellationToken) { - Debug.Assert(reader != null); + MiscellaneousUtils.Assert(reader != null); return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : reader.ReadAsync(buffer, index, count); } diff --git a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs index c1cd79e8..0b4b3545 100644 --- a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs @@ -364,7 +364,7 @@ namespace Newtonsoft.Json.Utilities } } - if (!string.IsNullOrEmpty(dateFormatString)) + if (!StringUtils.IsNullOrEmpty(dateFormatString)) { if (TryParseDateTimeExact(s.ToString(), dateTimeZoneHandling, dateFormatString, culture, out dt)) { @@ -400,7 +400,7 @@ namespace Newtonsoft.Json.Utilities } } - if (!string.IsNullOrEmpty(dateFormatString)) + if (!StringUtils.IsNullOrEmpty(dateFormatString)) { if (TryParseDateTimeExact(s, dateTimeZoneHandling, dateFormatString, culture, out dt)) { @@ -437,7 +437,7 @@ namespace Newtonsoft.Json.Utilities } } - if (!string.IsNullOrEmpty(dateFormatString)) + if (!StringUtils.IsNullOrEmpty(dateFormatString)) { if (TryParseDateTimeOffsetExact(s.ToString(), dateFormatString, culture, out dt)) { @@ -475,7 +475,7 @@ namespace Newtonsoft.Json.Utilities } } - if (!string.IsNullOrEmpty(dateFormatString)) + if (!StringUtils.IsNullOrEmpty(dateFormatString)) { if (TryParseDateTimeOffsetExact(s, dateFormatString, culture, out dt)) { @@ -620,7 +620,7 @@ namespace Newtonsoft.Json.Utilities #region Write internal static void WriteDateTimeString(TextWriter writer, DateTime value, DateFormatHandling format, string? formatString, CultureInfo culture) { - if (string.IsNullOrEmpty(formatString)) + if (StringUtils.IsNullOrEmpty(formatString)) { char[] chars = new char[64]; int pos = WriteDateTimeString(chars, 0, value, null, value.Kind, format); @@ -753,7 +753,7 @@ namespace Newtonsoft.Json.Utilities #if HAVE_DATE_TIME_OFFSET internal static void WriteDateTimeOffsetString(TextWriter writer, DateTimeOffset value, DateFormatHandling format, string? formatString, CultureInfo culture) { - if (string.IsNullOrEmpty(formatString)) + if (StringUtils.IsNullOrEmpty(formatString)) { char[] chars = new char[64]; int pos = WriteDateTimeString(chars, 0, (format == DateFormatHandling.IsoDateFormat) ? value.DateTime : value.UtcDateTime, value.Offset, DateTimeKind.Local, format); diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index 19a7a322..1d2ed07e 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -80,7 +80,7 @@ namespace Newtonsoft.Json.Utilities { get { - Debug.Assert(_genericDictionary != null); + MiscellaneousUtils.Assert(_genericDictionary != null); return _genericDictionary; } } diff --git a/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs b/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs index 0daa1c1d..acb3e3a3 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs @@ -117,8 +117,8 @@ namespace Newtonsoft.Json.Utilities { #if !HAVE_REFLECTION_BINDER Init(); - Debug.Assert(_getMemberCall != null); - Debug.Assert(_getCSharpArgumentInfoArray != null); + MiscellaneousUtils.Assert(_getMemberCall != null); + MiscellaneousUtils.Assert(_getCSharpArgumentInfoArray != null); return (CallSiteBinder)_getMemberCall(null, 0, name, context, _getCSharpArgumentInfoArray)!; #else return Binder.GetMember( @@ -130,8 +130,8 @@ namespace Newtonsoft.Json.Utilities { #if !HAVE_REFLECTION_BINDER Init(); - Debug.Assert(_setMemberCall != null); - Debug.Assert(_setCSharpArgumentInfoArray != null); + MiscellaneousUtils.Assert(_setMemberCall != null); + MiscellaneousUtils.Assert(_setCSharpArgumentInfoArray != null); return (CallSiteBinder)_setMemberCall(null, 0, name, context, _setCSharpArgumentInfoArray)!; #else return Binder.SetMember( diff --git a/Src/Newtonsoft.Json/Utilities/FSharpUtils.cs b/Src/Newtonsoft.Json/Utilities/FSharpUtils.cs index 718975b9..cb816660 100644 --- a/Src/Newtonsoft.Json/Utilities/FSharpUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/FSharpUtils.cs @@ -94,7 +94,7 @@ namespace Newtonsoft.Json.Utilities { get { - Debug.Assert(_instance != null); + MiscellaneousUtils.Assert(_instance != null); return _instance; } } diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 80fb1b65..fbcacf97 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -153,7 +153,7 @@ namespace Newtonsoft.Json.Utilities writer.Write(delimiter); } - if (!string.IsNullOrEmpty(s)) + if (!StringUtils.IsNullOrEmpty(s)) { int lastWritePosition = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); if (lastWritePosition == -1) @@ -266,7 +266,7 @@ namespace Newtonsoft.Json.Utilities // copy it over when creating new buffer if (isEscapedUnicodeText) { - Debug.Assert(writeBuffer != null, "Write buffer should never be null because it is set when the escaped unicode text is encountered."); + MiscellaneousUtils.Assert(writeBuffer != null, "Write buffer should never be null because it is set when the escaped unicode text is encountered."); Array.Copy(writeBuffer, newBuffer, UnicodeTextLength); } @@ -293,7 +293,7 @@ namespace Newtonsoft.Json.Utilities } } - Debug.Assert(lastWritePosition != 0); + MiscellaneousUtils.Assert(lastWritePosition != 0); length = s.Length - lastWritePosition; if (length > 0) { @@ -374,7 +374,7 @@ namespace Newtonsoft.Json.Utilities return WriteEscapedJavaScriptStringWithDelimitersAsync(writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); } - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { return cancellationToken.CancelIfRequestedAsync() ?? AsyncUtils.CompletedTask; } @@ -391,7 +391,7 @@ namespace Newtonsoft.Json.Utilities return WriteEscapedJavaScriptStringWithDelimitersAsync(task, writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); } - if (!string.IsNullOrEmpty(s)) + if (!StringUtils.IsNullOrEmpty(s)) { task = WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); if (task.IsCompletedSucessfully()) @@ -409,7 +409,7 @@ namespace Newtonsoft.Json.Utilities { await task.ConfigureAwait(false); - if (!string.IsNullOrEmpty(s)) + if (!StringUtils.IsNullOrEmpty(s)) { await WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken).ConfigureAwait(false); } diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index fe93b82c..e1dba08a 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -377,7 +377,7 @@ namespace Newtonsoft.Json.Utilities.LinqBridge Func empty) { CheckNotNull(source, "source"); - Debug.Assert(empty != null); + MiscellaneousUtils.Assert(empty != null); var list = source as IList; // optimized case for lists if (list != null) @@ -1364,7 +1364,7 @@ namespace Newtonsoft.Json.Utilities.LinqBridge Func lesser) { CheckNotNull(source, "source"); - Debug.Assert(lesser != null); + MiscellaneousUtils.Assert(lesser != null); return source.Aggregate((a, item) => lesser(a, item) ? a : item); } @@ -1378,7 +1378,7 @@ namespace Newtonsoft.Json.Utilities.LinqBridge TSource? seed, Func lesser) where TSource : struct { CheckNotNull(source, "source"); - Debug.Assert(lesser != null); + MiscellaneousUtils.Assert(lesser != null); return source.Aggregate(seed, (a, item) => lesser(a, item) ? a : item); // == MinMaxImpl(Repeat(null, 1).Concat(source), lesser); @@ -1442,7 +1442,7 @@ namespace Newtonsoft.Json.Utilities.LinqBridge private static IEnumerable Renumerable(this IEnumerator e) { - Debug.Assert(e != null); + MiscellaneousUtils.Assert(e != null); do { diff --git a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs index 70a1583b..137ec445 100644 --- a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs @@ -31,6 +31,8 @@ using System.Reflection; using System.Text; using System.Globalization; using System.Text.RegularExpressions; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace Newtonsoft.Json.Utilities { @@ -38,6 +40,12 @@ namespace Newtonsoft.Json.Utilities internal static class MiscellaneousUtils { + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool condition, string? message = null) + { + Debug.Assert(condition, message); + } + public static bool ValueEquals(object? objA, object? objB) { if (objA == objB) diff --git a/Src/Newtonsoft.Json/Utilities/NullableAttributes.cs b/Src/Newtonsoft.Json/Utilities/NullableAttributes.cs index ce153f2c..fa1f55e9 100644 --- a/Src/Newtonsoft.Json/Utilities/NullableAttributes.cs +++ b/Src/Newtonsoft.Json/Utilities/NullableAttributes.cs @@ -52,4 +52,23 @@ namespace System.Diagnostics.CodeAnalysis [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] internal sealed class AllowNullAttribute : Attribute { } + + /// + /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value. + /// + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] + internal class DoesNotReturnIfAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to + /// the associated parameter matches this value. + /// + public DoesNotReturnIfAttribute(bool parameterValue) => this.ParameterValue = parameterValue; + + /// Gets the condition parameter value. + public bool ParameterValue { get; } + } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index 8544a3f5..f07ffc4c 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -28,6 +28,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Globalization; +using System.Diagnostics.CodeAnalysis; #if !HAVE_LINQ using Newtonsoft.Json.Utilities.LinqBridge; #else @@ -45,6 +46,11 @@ namespace Newtonsoft.Json.Utilities public const char LineFeed = '\n'; public const char Tab = '\t'; + public static bool IsNullOrEmpty([NotNullWhen(false)] string? value) + { + return string.IsNullOrEmpty(value); + } + public static string FormatWith(this string format, IFormatProvider provider, object? arg0) { return format.FormatWith(provider, new object?[] { arg0 }); @@ -148,7 +154,7 @@ namespace Newtonsoft.Json.Utilities public static string ToCamelCase(string s) { - if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0])) + if (StringUtils.IsNullOrEmpty(s) || !char.IsUpper(s[0])) { return s; } @@ -207,7 +213,7 @@ namespace Newtonsoft.Json.Utilities public static string ToSnakeCase(string s) { - if (string.IsNullOrEmpty(s)) + if (StringUtils.IsNullOrEmpty(s)) { return s; }