From e370fc2e3ae7d01b490483350c51510285f359e3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Jan 2018 10:32:50 +1300 Subject: [PATCH] -Added readonly to structs --- Src/Newtonsoft.Json/Linq/JEnumerable.cs | 2 +- Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs | 6 ++++-- .../Serialization/CamelCasePropertyNamesContractResolver.cs | 2 +- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 2 +- Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs | 2 +- Src/Newtonsoft.Json/Utilities/LinqBridge.cs | 6 +++--- Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs | 2 +- Src/Newtonsoft.Json/Utilities/StringReference.cs | 2 +- 8 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Src/Newtonsoft.Json/Linq/JEnumerable.cs b/Src/Newtonsoft.Json/Linq/JEnumerable.cs index fd6729a7..93bbe1a7 100644 --- a/Src/Newtonsoft.Json/Linq/JEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/JEnumerable.cs @@ -39,7 +39,7 @@ namespace Newtonsoft.Json.Linq /// Represents a collection of objects. /// /// The type of token. - public struct JEnumerable : IJEnumerable, IEquatable> where T : JToken + public readonly struct JEnumerable : IJEnumerable, IEquatable> where T : JToken { /// /// An empty collection of objects. diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs index 331e6f6a..4357ac5a 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs @@ -33,6 +33,8 @@ namespace Newtonsoft.Json.Linq.JsonPath { internal class JPath { + private static readonly char[] FloatCharacters = new[] {'.', 'E', 'e'}; + private readonly string _expression; public List Filters { get; } @@ -601,7 +603,7 @@ namespace Newtonsoft.Json.Linq.JsonPath { string numberText = sb.ToString(); - if (numberText.IndexOfAny(new[] { '.', 'E', 'e' }) != -1) + if (numberText.IndexOfAny(FloatCharacters) != -1) { bool result = double.TryParse(numberText, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var d); value = d; @@ -688,7 +690,7 @@ namespace Newtonsoft.Json.Linq.JsonPath resolvedChar = currentChar; break; default: - throw new JsonException(@"Unknown escape character: \" + _expression[_currentIndex]); + throw new JsonException(@"Unknown escape character: \" + currentChar); } sb.Append(resolvedChar); diff --git a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs index d093f622..786922d9 100644 --- a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs @@ -30,7 +30,7 @@ using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Serialization { - internal struct ResolverContractKey : IEquatable + internal readonly struct ResolverContractKey : IEquatable { private readonly Type _resolverType; private readonly Type _contractType; diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index 18394129..ba8d0c1c 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -248,7 +248,7 @@ namespace Newtonsoft.Json.Utilities #endif } - internal struct TypeConvertKey : IEquatable + internal readonly struct TypeConvertKey : IEquatable { public Type InitialType { get; } diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index fdfd55d8..96a80a35 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -482,7 +482,7 @@ namespace Newtonsoft.Json.Utilities } } - private struct DictionaryEnumerator : IDictionaryEnumerator + private readonly struct DictionaryEnumerator : IDictionaryEnumerator { private readonly IEnumerator> _e; diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index be82784a..dc812583 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -2991,10 +2991,10 @@ namespace Newtonsoft.Json.Utilities.LinqBridge } [Serializable] - internal struct Tuple : IEquatable> + internal readonly struct Tuple : IEquatable> { - public TFirst First { get; private set; } - public TSecond Second { get; private set; } + public TFirst First { get; } + public TSecond Second { get; } public Tuple(TFirst first, TSecond second) : this() diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index b49df890..3927b8ee 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -1109,7 +1109,7 @@ namespace Newtonsoft.Json.Utilities } } - internal struct TypeNameKey : IEquatable + internal readonly struct TypeNameKey : IEquatable { internal readonly string AssemblyName; internal readonly string TypeName; diff --git a/Src/Newtonsoft.Json/Utilities/StringReference.cs b/Src/Newtonsoft.Json/Utilities/StringReference.cs index 49d257c6..44591d11 100644 --- a/Src/Newtonsoft.Json/Utilities/StringReference.cs +++ b/Src/Newtonsoft.Json/Utilities/StringReference.cs @@ -27,7 +27,7 @@ using System; namespace Newtonsoft.Json.Utilities { - internal struct StringReference + internal readonly struct StringReference { private readonly char[] _chars; private readonly int _startIndex;