From ded58696dc5720bdd7674a3561233e61c4e4d667 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:33:54 +0500 Subject: [PATCH 01/20] shorter switch --- Src/Newtonsoft.Json/JsonWriter.cs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 5fd5efe40..468a97b82 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -162,28 +162,17 @@ public WriteState WriteState { get { - switch (_currentState) + return _currentState switch { - case State.Error: - return WriteState.Error; - case State.Closed: - return WriteState.Closed; - case State.Object: - case State.ObjectStart: - return WriteState.Object; - case State.Array: - case State.ArrayStart: - return WriteState.Array; - case State.Constructor: - case State.ConstructorStart: - return WriteState.Constructor; - case State.Property: - return WriteState.Property; - case State.Start: - return WriteState.Start; - default: - throw JsonWriterException.Create(this, "Invalid state: " + _currentState, null); - } + State.Error => WriteState.Error, + State.Closed => WriteState.Closed, + State.Object or State.ObjectStart => WriteState.Object, + State.Array or State.ArrayStart => WriteState.Array, + State.Constructor or State.ConstructorStart => WriteState.Constructor, + State.Property => WriteState.Property, + State.Start => WriteState.Start, + _ => throw JsonWriterException.Create(this, "Invalid state: " + _currentState, null) + }; } } From b52f822eb8ba8c1622d23152ad1d834d5f27fb61 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:37:48 +0500 Subject: [PATCH 02/20] using null-conditional operator instead of checking for null --- Src/Newtonsoft.Json/JsonWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 468a97b82..741caedf0 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -369,7 +369,7 @@ private JsonContainerType Pop() { JsonPosition oldPosition = _currentPosition; - if (_stack != null && _stack.Count > 0) + if (_stack?.Count > 0) { _currentPosition = _stack[_stack.Count - 1]; _stack.RemoveAt(_stack.Count - 1); From d1ccf7a257650c3c252cde4281c079ae105d5635 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:40:21 +0500 Subject: [PATCH 03/20] using List.Last() instead of explicit index specifying --- Src/Newtonsoft.Json/JsonWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 741caedf0..02f067395 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -371,8 +371,8 @@ private JsonContainerType Pop() if (_stack?.Count > 0) { - _currentPosition = _stack[_stack.Count - 1]; - _stack.RemoveAt(_stack.Count - 1); + _currentPosition = _stack.Last(); + _stack.Remove(_stack.Last()); } else { From fe4dc22b6b941cb0e6c7005724a9675fa07a74c6 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:43:26 +0500 Subject: [PATCH 04/20] shorter switch --- Src/Newtonsoft.Json/JsonWriter.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 02f067395..88221cd52 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -729,17 +729,13 @@ private void AutoCompleteAll() private JsonToken GetCloseTokenForType(JsonContainerType type) { - switch (type) + return type switch { - case JsonContainerType.Object: - return JsonToken.EndObject; - case JsonContainerType.Array: - return JsonToken.EndArray; - case JsonContainerType.Constructor: - return JsonToken.EndConstructor; - default: - throw JsonWriterException.Create(this, "No close token for type: " + type, null); - } + JsonContainerType.Object => JsonToken.EndObject, + JsonContainerType.Array => JsonToken.EndArray, + JsonContainerType.Constructor=> JsonToken.EndConstructor, + _ => throw JsonWriterException.Create(this, "No close token for type: " + type, null) + }; } private void AutoCompleteClose(JsonContainerType type) From b5c1554846eb4473d5b47a570db00d9e0243aa90 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:46:49 +0500 Subject: [PATCH 05/20] shorter switch --- Src/Newtonsoft.Json/JsonWriter.cs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 88221cd52..bb2ebe0da 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -733,7 +733,7 @@ private JsonToken GetCloseTokenForType(JsonContainerType type) { JsonContainerType.Object => JsonToken.EndObject, JsonContainerType.Array => JsonToken.EndArray, - JsonContainerType.Constructor=> JsonToken.EndConstructor, + JsonContainerType.Constructor => JsonToken.EndConstructor, _ => throw JsonWriterException.Create(this, "No close token for type: " + type, null) }; } @@ -800,23 +800,13 @@ private void UpdateCurrentState() { JsonContainerType currentLevelType = Peek(); - switch (currentLevelType) + _currentState = currentLevelType switch { - case JsonContainerType.Object: - _currentState = State.Object; - break; - case JsonContainerType.Array: - _currentState = State.Array; - break; - case JsonContainerType.Constructor: - _currentState = State.Array; - break; - case JsonContainerType.None: - _currentState = State.Start; - break; - default: - throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null); - } + JsonContainerType.Object => State.Object, + JsonContainerType.Array or JsonContainerType.Constructor => State.Array, + JsonContainerType.None => State.Start, + _ => throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null) + }; } /// From f9d4311095395a9099a20c37dd93d53bc4729de6 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 16:55:32 +0500 Subject: [PATCH 06/20] shorter switch --- Src/Newtonsoft.Json/JsonWriter.Async.cs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.Async.cs b/Src/Newtonsoft.Json/JsonWriter.Async.cs index ffb1af826..b5cd024df 100644 --- a/Src/Newtonsoft.Json/JsonWriter.Async.cs +++ b/Src/Newtonsoft.Json/JsonWriter.Async.cs @@ -270,22 +270,14 @@ public virtual Task WriteEndAsync(CancellationToken cancellationToken = default) internal Task WriteEndInternalAsync(CancellationToken cancellationToken) { JsonContainerType type = Peek(); - switch (type) - { - case JsonContainerType.Object: - return WriteEndObjectAsync(cancellationToken); - case JsonContainerType.Array: - return WriteEndArrayAsync(cancellationToken); - case JsonContainerType.Constructor: - return WriteEndConstructorAsync(cancellationToken); - default: - if (cancellationToken.IsCancellationRequested) - { - return cancellationToken.FromCanceled(); - } - - throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null); - } + return type switch + { + JsonContainerType.Object => WriteEndObjectAsync(cancellationToken), + JsonContainerType.Array => WriteEndArrayAsync(cancellationToken), + JsonContainerType.Constructor => WriteEndConstructorAsync(cancellationToken), + _ => cancellationToken.IsCancellationRequested ? cancellationToken.FromCanceled() + : throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null) + }; } internal Task InternalWriteEndAsync(JsonContainerType type, CancellationToken cancellationToken) From 1eb1bf6a30a750ac8dd14bf71630169dce644065 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 17:07:31 +0500 Subject: [PATCH 07/20] shorter switch --- Src/Newtonsoft.Json/JsonWriter.Async.cs | 54 ++++++++----------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.Async.cs b/Src/Newtonsoft.Json/JsonWriter.Async.cs index b5cd024df..b2f556b82 100644 --- a/Src/Newtonsoft.Json/JsonWriter.Async.cs +++ b/Src/Newtonsoft.Json/JsonWriter.Async.cs @@ -1644,43 +1644,23 @@ protected Task SetWriteStateAsync(JsonToken token, object value, CancellationTok return cancellationToken.FromCanceled(); } - switch (token) - { - case JsonToken.StartObject: - return InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken); - case JsonToken.StartArray: - return InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken); - case JsonToken.StartConstructor: - return InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken); - case JsonToken.PropertyName: - if (!(value is string s)) - { - throw new ArgumentException("A name is required when setting property name state.", nameof(value)); - } - - return InternalWritePropertyNameAsync(s, cancellationToken); - case JsonToken.Comment: - return InternalWriteCommentAsync(cancellationToken); - case JsonToken.Raw: - return AsyncUtils.CompletedTask; - case JsonToken.Integer: - case JsonToken.Float: - case JsonToken.String: - case JsonToken.Boolean: - case JsonToken.Date: - case JsonToken.Bytes: - case JsonToken.Null: - case JsonToken.Undefined: - return InternalWriteValueAsync(token, cancellationToken); - case JsonToken.EndObject: - return InternalWriteEndAsync(JsonContainerType.Object, cancellationToken); - case JsonToken.EndArray: - return InternalWriteEndAsync(JsonContainerType.Array, cancellationToken); - case JsonToken.EndConstructor: - return InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken); - default: - throw new ArgumentOutOfRangeException(nameof(token)); - } + return token switch + { + JsonToken.StartObject => InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken), + JsonToken.StartArray => InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken), + JsonToken.StartConstructor => InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken), + JsonToken.PropertyName => value is string s ? InternalWritePropertyNameAsync(s, cancellationToken) + : throw new ArgumentException("A name is required when setting property name state.", nameof(value)), + JsonToken.Comment => InternalWriteCommentAsync(cancellationToken), + JsonToken.Raw => AsyncUtils.CompletedTask, + JsonToken.Integer or JsonToken.Float or JsonToken.String + or JsonToken.Boolean or JsonToken.Date or JsonToken.Bytes + or JsonToken.Null or JsonToken.Undefined => InternalWriteValueAsync(token, cancellationToken), + JsonToken.EndObject => InternalWriteEndAsync(JsonContainerType.Object, cancellationToken), + JsonToken.EndArray => InternalWriteEndAsync(JsonContainerType.Array, cancellationToken), + JsonToken.EndConstructor => InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken), + _ => throw new ArgumentOutOfRangeException(nameof(token)) + }; } internal static Task WriteValueAsync(JsonWriter writer, PrimitiveTypeCode typeCode, object value, CancellationToken cancellationToken) From 3be823faa7b05ff6d17044f91c646bd7cb6dfda4 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 17:16:08 +0500 Subject: [PATCH 08/20] using null-conditional operator instead of checking for null --- Src/Newtonsoft.Json/JsonValidatingReader.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index 7b6de5902..ffe4c5e49 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -229,14 +229,14 @@ private IList CurrentMemberSchemas { if (!schema.PositionalItemsValidation) { - if (schema.Items != null && schema.Items.Count > 0) + if (schema.Items?.Count > 0) { schemas.Add(schema.Items[0]); } } else { - if (schema.Items != null && schema.Items.Count > 0) + if (schema.Items?.Count > 0) { if (schema.Items.Count > (_currentScope.ArrayItemCount - 1)) { From 3b94503a266ef2657d06af2d4a6ad20fbebee6e4 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Sun, 30 Apr 2023 17:18:37 +0500 Subject: [PATCH 09/20] shorter switch --- Src/Newtonsoft.Json/JsonValidatingReader.cs | 30 ++++++++------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index ffe4c5e49..50beb532d 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -354,25 +354,17 @@ private void ValidateNotDisallowed(JsonSchemaModel schema) private JsonSchemaType? GetCurrentNodeSchemaType() { - switch (_reader.TokenType) - { - case JsonToken.StartObject: - return JsonSchemaType.Object; - case JsonToken.StartArray: - return JsonSchemaType.Array; - case JsonToken.Integer: - return JsonSchemaType.Integer; - case JsonToken.Float: - return JsonSchemaType.Float; - case JsonToken.String: - return JsonSchemaType.String; - case JsonToken.Boolean: - return JsonSchemaType.Boolean; - case JsonToken.Null: - return JsonSchemaType.Null; - default: - return null; - } + return _reader.TokenType switch + { + JsonToken.StartObject => JsonSchemaType.Object, + JsonToken.StartArray => JsonSchemaType.Array, + JsonToken.Integer => JsonSchemaType.Integer, + JsonToken.Float => JsonSchemaType.Float, + JsonToken.String => JsonSchemaType.String, + JsonToken.Boolean => JsonSchemaType.Boolean, + JsonToken.Null => JsonSchemaType.Null, + _ => null + }; } /// From 11315e027609fca7992d31e7bf37c9d2c087b6f7 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:10:43 +0500 Subject: [PATCH 10/20] return to using List.RemoveAt(List.Count() - 1) instead of List.Remove(List.Last()) --- Src/Newtonsoft.Json/JsonWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index bb2ebe0da..3943d1f2e 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -371,8 +371,8 @@ private JsonContainerType Pop() if (_stack?.Count > 0) { - _currentPosition = _stack.Last(); - _stack.Remove(_stack.Last()); + _currentPosition = _stack[_stack.Count - 1]; + _stack.RemoveAt(_stack.Count - 1); } else { From 1a36ecb01879dae9ea936f79be1394dd681ddc71 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:26:52 +0500 Subject: [PATCH 11/20] shorter switch --- Src/Newtonsoft.Json/JsonConvert.cs | 69 +++++++++++------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 95019e2b7..25d57165f 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -463,57 +463,36 @@ public static string ToString(object? value) PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value.GetType()); - switch (typeCode) + return typeCode switch { - case PrimitiveTypeCode.String: - return ToString((string)value); - case PrimitiveTypeCode.Char: - return ToString((char)value); - case PrimitiveTypeCode.Boolean: - return ToString((bool)value); - case PrimitiveTypeCode.SByte: - return ToString((sbyte)value); - case PrimitiveTypeCode.Int16: - return ToString((short)value); - case PrimitiveTypeCode.UInt16: - return ToString((ushort)value); - case PrimitiveTypeCode.Int32: - return ToString((int)value); - case PrimitiveTypeCode.Byte: - return ToString((byte)value); - case PrimitiveTypeCode.UInt32: - return ToString((uint)value); - case PrimitiveTypeCode.Int64: - return ToString((long)value); - case PrimitiveTypeCode.UInt64: - return ToString((ulong)value); - case PrimitiveTypeCode.Single: - return ToString((float)value); - case PrimitiveTypeCode.Double: - return ToString((double)value); - case PrimitiveTypeCode.DateTime: - return ToString((DateTime)value); - case PrimitiveTypeCode.Decimal: - return ToString((decimal)value); + PrimitiveTypeCode.String => ToString((string)value), + PrimitiveTypeCode.Char => ToString((char)value), + PrimitiveTypeCode.Boolean => ToString((bool)value), + PrimitiveTypeCode.SByte => ToString((sbyte)value), + PrimitiveTypeCode.Int16 => ToString((short)value), + PrimitiveTypeCode.UInt16 => ToString((ushort)value), + PrimitiveTypeCode.Int32 => ToString((int)value), + PrimitiveTypeCode.Byte => ToString((byte)value), + PrimitiveTypeCode.UInt32 => ToString((uint)value), + PrimitiveTypeCode.Int64 => ToString((long)value), + PrimitiveTypeCode.UInt64 => ToString((ulong)value), + PrimitiveTypeCode.Single => ToString((float)value), + PrimitiveTypeCode.Double => ToString((double)value), + PrimitiveTypeCode.DateTime => ToString((DateTime)value), + PrimitiveTypeCode.Decimal => ToString((decimal)value), #if HAVE_DB_NULL_TYPE_CODE - case PrimitiveTypeCode.DBNull: - return Null; + PrimitiveTypeCode.DBNull => Null, #endif #if HAVE_DATE_TIME_OFFSET - case PrimitiveTypeCode.DateTimeOffset: - return ToString((DateTimeOffset)value); + PrimitiveTypeCode.DateTimeOffset => ToString((DateTimeOffset)value), #endif - case PrimitiveTypeCode.Guid: - return ToString((Guid)value); - case PrimitiveTypeCode.Uri: - return ToString((Uri)value); - case PrimitiveTypeCode.TimeSpan: - return ToString((TimeSpan)value); + PrimitiveTypeCode.Guid => ToString((Guid)value), + PrimitiveTypeCode.Uri => ToString((Uri)value), + PrimitiveTypeCode.TimeSpan => ToString((TimeSpan)value), #if HAVE_BIG_INTEGER - case PrimitiveTypeCode.BigInteger: - return ToStringInternal((BigInteger)value); + PrimitiveTypeCode.BigInteger => ToString((BigInteger)value), #endif - } + }; throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); } @@ -1080,6 +1059,6 @@ public static string SerializeXNode(XObject? node, Formatting formatting, bool o return (XDocument?)DeserializeObject(value, typeof(XDocument), converter); } #endif - #endregion +#endregion } } From c8702644918f5a71b87ef1e3b70afd8dfa1be7da Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:29:04 +0500 Subject: [PATCH 12/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonConvert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 25d57165f..510b979dd 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -532,7 +532,7 @@ public static string SerializeObject(object? value, Formatting formatting) [DebuggerStepThrough] public static string SerializeObject(object? value, params JsonConverter[] converters) { - JsonSerializerSettings? settings = (converters != null && converters.Length > 0) + JsonSerializerSettings? settings = (converters?.Length > 0) ? new JsonSerializerSettings { Converters = converters } : null; From fe1fc1fa95e1c200cc29b76b10e57c30d4c4591c Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:29:29 +0500 Subject: [PATCH 13/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonConvert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 510b979dd..5b6a24b3e 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -549,7 +549,7 @@ public static string SerializeObject(object? value, params JsonConverter[] conve [DebuggerStepThrough] public static string SerializeObject(object? value, Formatting formatting, params JsonConverter[] converters) { - JsonSerializerSettings? settings = (converters != null && converters.Length > 0) + JsonSerializerSettings? settings = (converters?.Length > 0) ? new JsonSerializerSettings { Converters = converters } : null; From 0b0ac043122a4050ac178e7da22f45275975b9bf Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:30:19 +0500 Subject: [PATCH 14/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonConvert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 5b6a24b3e..786fa75e4 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -776,7 +776,7 @@ private static string SerializeObjectInternal(object? value, Type? type, JsonSer [DebuggerStepThrough] public static object? DeserializeObject(string value, Type type, params JsonConverter[] converters) { - JsonSerializerSettings? settings = (converters != null && converters.Length > 0) + JsonSerializerSettings? settings = (converters?.Length > 0) ? new JsonSerializerSettings { Converters = converters } : null; From 94c7db43e2ec5421bed1bc6c15c81cef6d17e9b6 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:31:30 +0500 Subject: [PATCH 15/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonConvert.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 786fa75e4..603879bb0 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -841,7 +841,7 @@ public static void PopulateObject(string value, object target, JsonSerializerSet { jsonSerializer.Populate(jsonReader, target); - if (settings != null && settings.CheckAdditionalContent) + if (settings?.CheckAdditionalContent) { while (jsonReader.Read()) { From 34c5dde04f155d61ae71059f01271931aa58a998 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:34:20 +0500 Subject: [PATCH 16/20] using object initializers --- Src/Newtonsoft.Json/JsonConvert.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 603879bb0..99c5cc8ac 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -950,10 +950,12 @@ public static string SerializeXmlNode(XmlNode? node, Formatting formatting, bool /// The deserialized . public static XmlDocument? DeserializeXmlNode(string value, string? deserializeRootElementName, bool writeArrayAttribute, bool encodeSpecialCharacters) { - XmlNodeConverter converter = new XmlNodeConverter(); - converter.DeserializeRootElementName = deserializeRootElementName; - converter.WriteArrayAttribute = writeArrayAttribute; - converter.EncodeSpecialCharacters = encodeSpecialCharacters; + XmlNodeConverter converter = new XmlNodeConverter() + { + DeserializeRootElementName = deserializeRootElementName, + WriteArrayAttribute = writeArrayAttribute, + EncodeSpecialCharacters = encodeSpecialCharacters + }; return (XmlDocument?)DeserializeObject(value, typeof(XmlDocument), converter); } @@ -1059,6 +1061,6 @@ public static string SerializeXNode(XObject? node, Formatting formatting, bool o return (XDocument?)DeserializeObject(value, typeof(XDocument), converter); } #endif -#endregion + #endregion } } From 5ea7bef7743bdff5f393bec1e3beb4a2fd4e3255 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:40:24 +0500 Subject: [PATCH 17/20] shorter switch --- Src/Newtonsoft.Json/JsonPosition.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonPosition.cs b/Src/Newtonsoft.Json/JsonPosition.cs index 92a62344d..6b1222d49 100644 --- a/Src/Newtonsoft.Json/JsonPosition.cs +++ b/Src/Newtonsoft.Json/JsonPosition.cs @@ -59,16 +59,13 @@ public JsonPosition(JsonContainerType type) internal int CalculateLength() { - switch (Type) + return Type switch { - case JsonContainerType.Object: - return PropertyName!.Length + 5; - case JsonContainerType.Array: - case JsonContainerType.Constructor: - return MathUtils.IntLength((ulong)Position) + 2; - default: - throw new ArgumentOutOfRangeException(nameof(Type)); - } + JsonContainerType.Object => PropertyName!.Length + 5, + JsonContainerType.Array or JsonContainerType.Constructor + => MathUtils.IntLength((ulong)Position) + 2, + _ => throw new ArgumentOutOfRangeException(nameof(Type)) + }; } internal void WriteTo(StringBuilder sb, ref StringWriter? writer, ref char[]? buffer) From e65fd6ea6f8ac451124bd62a9f4e049ba887b1bc Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:42:25 +0500 Subject: [PATCH 18/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonPosition.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonPosition.cs b/Src/Newtonsoft.Json/JsonPosition.cs index 6b1222d49..b3f763835 100644 --- a/Src/Newtonsoft.Json/JsonPosition.cs +++ b/Src/Newtonsoft.Json/JsonPosition.cs @@ -161,7 +161,7 @@ internal static string FormatMessage(IJsonLineInfo? lineInfo, string path, strin message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path); - if (lineInfo != null && lineInfo.HasLineInfo()) + if (lineInfo?.HasLineInfo()) { message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition); } From 986fc7687567711375313cccaa24bf012a23f5a6 Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:45:35 +0500 Subject: [PATCH 19/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index e368afc15..53c26aa24 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -312,7 +312,7 @@ public CultureInfo Culture internal JsonPosition GetPosition(int depth) { - if (_stack != null && depth < _stack.Count) + if (depth < _stack?.Count) { return _stack[depth]; } From acfa6fe0bfa418f6c4813d58d5fd5f7c0e383b7b Mon Sep 17 00:00:00 2001 From: rinatxp Date: Wed, 3 May 2023 01:46:53 +0500 Subject: [PATCH 20/20] using null-conditional operator --- Src/Newtonsoft.Json/JsonReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 53c26aa24..36232bd43 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -364,7 +364,7 @@ private void Push(JsonContainerType value) private JsonContainerType Pop() { JsonPosition oldPosition; - if (_stack != null && _stack.Count > 0) + if (_stack?.Count > 0) { oldPosition = _currentPosition; _currentPosition = _stack[_stack.Count - 1];