diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index 5a467dc5..051a5e65 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -51,18 +51,18 @@ namespace Newtonsoft.Json.TestConsole //PerformanceTests t = new PerformanceTests(); //t.DeserializeLargeJson(); - PerformanceTests t = new PerformanceTests(); - t.Iterations = 10000; - t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); + //PerformanceTests t = new PerformanceTests(); + //t.Iterations = 50000; + //t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); - Console.WriteLine("Wait to do stuff again"); - Console.ReadKey(); + //Console.WriteLine("Wait to do stuff again"); + //Console.ReadKey(); - t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); + //t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); //ReadLargeJson(); //WriteLargeJson(); - //DeserializeJson(); + DeserializeJson(); //ReadLargeJson(); //ReadLargeJsonJavaScriptSerializer(); diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index 3b405fd9..e56a551a 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -463,36 +463,36 @@ namespace Newtonsoft.Json.Tests { string result; - result = JavaScriptUtils.ToEscapedJavaScriptString("How now brown cow?", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("How now brown cow?", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""How now brown cow?""", result); - result = JavaScriptUtils.ToEscapedJavaScriptString("How now 'brown' cow?", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("How now 'brown' cow?", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""How now 'brown' cow?""", result); - result = JavaScriptUtils.ToEscapedJavaScriptString("How now cow?", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("How now cow?", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""How now cow?""", result); - result = JavaScriptUtils.ToEscapedJavaScriptString("How \r\nnow brown cow?", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("How \r\nnow brown cow?", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""How \r\nnow brown cow?""", result); - result = JavaScriptUtils.ToEscapedJavaScriptString("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007""", result); result = - JavaScriptUtils.ToEscapedJavaScriptString("\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013", '"', true); + JavaScriptUtils.ToEscapedJavaScriptString("\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013""", result); result = JavaScriptUtils.ToEscapedJavaScriptString( - "\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f ", '"', true); + "\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f ", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f """, result); result = JavaScriptUtils.ToEscapedJavaScriptString( - "!\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]", '"', true); + "!\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""!\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]""", result); - result = JavaScriptUtils.ToEscapedJavaScriptString("^_`abcdefghijklmnopqrstuvwxyz{|}~", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("^_`abcdefghijklmnopqrstuvwxyz{|}~", '"', true, StringEscapeHandling.Default); Assert.AreEqual(@"""^_`abcdefghijklmnopqrstuvwxyz{|}~""", result); string data = @@ -500,22 +500,22 @@ namespace Newtonsoft.Json.Tests string expected = @"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""; - result = JavaScriptUtils.ToEscapedJavaScriptString(data, '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString(data, '"', true, StringEscapeHandling.Default); Assert.AreEqual(expected, result); - result = JavaScriptUtils.ToEscapedJavaScriptString("Fred's cat.", '\'', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("Fred's cat.", '\'', true, StringEscapeHandling.Default); Assert.AreEqual(result, @"'Fred\'s cat.'"); - result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are you gentlemen?"" said Cats.", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are you gentlemen?"" said Cats.", '"', true, StringEscapeHandling.Default); Assert.AreEqual(result, @"""\""How are you gentlemen?\"" said Cats."""); - result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are' you gentlemen?"" said Cats.", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are' you gentlemen?"" said Cats.", '"', true, StringEscapeHandling.Default); Assert.AreEqual(result, @"""\""How are' you gentlemen?\"" said Cats."""); - result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true); + result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true, StringEscapeHandling.Default); Assert.AreEqual(result, @"'Fred\'s ""cat"".'"); - result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress", '"', true); + result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress", '"', true, StringEscapeHandling.Default); Assert.AreEqual(result, @"""\u001farrayThe Oid value. public BsonObjectId(byte[] value) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value.Length != 12) { throw new ArgumentException("An ObjectId must be 12 bytes", nameof(value)); diff --git a/Src/Newtonsoft.Json/Bson/BsonReader.cs b/Src/Newtonsoft.Json/Bson/BsonReader.cs index 276adf18..0ea895a6 100644 --- a/Src/Newtonsoft.Json/Bson/BsonReader.cs +++ b/Src/Newtonsoft.Json/Bson/BsonReader.cs @@ -145,7 +145,7 @@ namespace Newtonsoft.Json.Bson /// The used when reading values from BSON. public BsonReader(Stream stream, bool readRootValueAsArray, DateTimeKind dateTimeKindHandling) { - ValidationUtils.ArgumentNotNull(stream, "stream"); + ValidationUtils.ArgumentNotNull(stream, nameof(stream)); _reader = new BinaryReader(stream); _stack = new List(); _readRootValueAsArray = readRootValueAsArray; @@ -160,7 +160,7 @@ namespace Newtonsoft.Json.Bson /// The used when reading values from BSON. public BsonReader(BinaryReader reader, bool readRootValueAsArray, DateTimeKind dateTimeKindHandling) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); _reader = reader; _stack = new List(); _readRootValueAsArray = readRootValueAsArray; @@ -586,7 +586,7 @@ namespace Newtonsoft.Json.Bson int totalBytesRead = 0; // used in case of left over multibyte characters in the buffer int offset = 0; - do + while (true) { int count = offset; byte b; @@ -638,7 +638,7 @@ namespace Newtonsoft.Json.Bson offset = 0; } } - } while (true); + } } private string ReadLengthString() diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index ac2d8b0e..2c0c2ff9 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Bson /// The stream. public BsonWriter(Stream stream) { - ValidationUtils.ArgumentNotNull(stream, "stream"); + ValidationUtils.ArgumentNotNull(stream, nameof(stream)); _writer = new BsonBinaryWriter(new BinaryWriter(stream)); } @@ -75,7 +75,7 @@ namespace Newtonsoft.Json.Bson /// The writer. public BsonWriter(BinaryWriter writer) { - ValidationUtils.ArgumentNotNull(writer, "writer"); + ValidationUtils.ArgumentNotNull(writer, nameof(writer)); _writer = new BsonBinaryWriter(writer); } @@ -500,7 +500,7 @@ namespace Newtonsoft.Json.Bson /// The Object ID value to write. public void WriteObjectId(byte[] value) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value.Length != 12) { @@ -520,7 +520,7 @@ namespace Newtonsoft.Json.Bson /// The regex options. public void WriteRegex(string pattern, string options) { - ValidationUtils.ArgumentNotNull(pattern, "pattern"); + ValidationUtils.ArgumentNotNull(pattern, nameof(pattern)); // hack to update the writer state UpdateScopeWithFinishedValue(); diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index c49358de..e1021b4a 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -848,7 +848,7 @@ namespace Newtonsoft.Json /// The deserialized object from the JSON string. public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings); diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index d2a0ec80..e00f41ae 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -414,7 +414,9 @@ namespace Newtonsoft.Json /// A . This method will return null at the end of an array. public virtual int? ReadAsInt32() { - switch (GetContentToken()) + JsonToken t = GetContentToken(); + + switch (t) { case JsonToken.None: case JsonToken.Null: @@ -433,7 +435,7 @@ namespace Newtonsoft.Json return ReadInt32String(s); } - throw JsonReaderException.Create(this, "Error reading integer. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); + throw JsonReaderException.Create(this, "Error reading integer. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t)); } internal int? ReadInt32String(string s) @@ -632,7 +634,7 @@ namespace Newtonsoft.Json } double d; - if (double.TryParse(s, NumberStyles.Float, Culture, out d)) + if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, Culture, out d)) { SetToken(JsonToken.Float, d, false); return d; diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index da6f71a6..75401162 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -729,8 +729,8 @@ namespace Newtonsoft.Json internal virtual void PopulateInternal(JsonReader reader, object target) { - ValidationUtils.ArgumentNotNull(reader, "reader"); - ValidationUtils.ArgumentNotNull(target, "target"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); + ValidationUtils.ArgumentNotNull(target, nameof(target)); // set serialization options onto reader CultureInfo previousCulture; @@ -804,7 +804,7 @@ namespace Newtonsoft.Json internal virtual object DeserializeInternal(JsonReader reader, Type objectType) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); // set serialization options onto reader CultureInfo previousCulture; @@ -996,7 +996,7 @@ namespace Newtonsoft.Json internal virtual void SerializeInternal(JsonWriter jsonWriter, object value, Type objectType) { - ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter"); + ValidationUtils.ArgumentNotNull(jsonWriter, nameof(jsonWriter)); // set serialization options onto writer Formatting? previousFormatting = null; @@ -1109,7 +1109,7 @@ namespace Newtonsoft.Json internal static JsonConverter GetMatchingConverter(IList converters, Type objectType) { #if DEBUG - ValidationUtils.ArgumentNotNull(objectType, "objectType"); + ValidationUtils.ArgumentNotNull(objectType, nameof(objectType)); #endif if (converters != null) diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 14102dcf..13cf1a70 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -528,7 +528,7 @@ namespace Newtonsoft.Json // eat break; } - } + }; case State.Finished: ReadFinished(); SetToken(JsonToken.None); diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index bd820c4e..d35cf4ba 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -318,7 +318,7 @@ namespace Newtonsoft.Json /// The to read from while validating. public JsonValidatingReader(JsonReader reader) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); _reader = reader; _stack = new Stack(); } diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 869a1239..293b1536 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -493,7 +493,7 @@ namespace Newtonsoft.Json /// A flag indicating whether the current token's children should be written. public void WriteToken(JsonReader reader, bool writeChildren) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); WriteToken(reader, writeChildren, true, true); } @@ -520,18 +520,18 @@ namespace Newtonsoft.Json WriteStartArray(); break; case JsonToken.StartConstructor: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteStartConstructor(value.ToString()); break; case JsonToken.PropertyName: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); WritePropertyName(value.ToString()); break; case JsonToken.Comment: WriteComment((value != null) ? value.ToString() : null); break; case JsonToken.Integer: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); #if !(NET20 || NET35 || PORTABLE || PORTABLE40) if (value is BigInteger) { @@ -544,7 +544,7 @@ namespace Newtonsoft.Json } break; case JsonToken.Float: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value is decimal) { WriteValue((decimal)value); @@ -563,11 +563,11 @@ namespace Newtonsoft.Json } break; case JsonToken.String: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteValue(value.ToString()); break; case JsonToken.Boolean: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); WriteValue(Convert.ToBoolean(value, CultureInfo.InvariantCulture)); break; case JsonToken.Null: @@ -586,7 +586,7 @@ namespace Newtonsoft.Json WriteEndConstructor(); break; case JsonToken.Date: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); #if !NET20 if (value is DateTimeOffset) { @@ -602,7 +602,7 @@ namespace Newtonsoft.Json WriteRawValue((value != null) ? value.ToString() : null); break; case JsonToken.Bytes: - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value is Guid) { WriteValue((Guid)value); diff --git a/Src/Newtonsoft.Json/Linq/Extensions.cs b/Src/Newtonsoft.Json/Linq/Extensions.cs index c26c13a5..f9f8976c 100644 --- a/Src/Newtonsoft.Json/Linq/Extensions.cs +++ b/Src/Newtonsoft.Json/Linq/Extensions.cs @@ -49,7 +49,7 @@ namespace Newtonsoft.Json.Linq /// An of that contains the ancestors of every token in the source collection. public static IJEnumerable Ancestors(this IEnumerable source) where T : JToken { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(j => j.Ancestors()).AsJEnumerable(); } @@ -62,7 +62,7 @@ namespace Newtonsoft.Json.Linq /// An of that contains every token in the source collection, the ancestors of every token in the source collection. public static IJEnumerable AncestorsAndSelf(this IEnumerable source) where T : JToken { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(j => j.AncestorsAndSelf()).AsJEnumerable(); } @@ -75,7 +75,7 @@ namespace Newtonsoft.Json.Linq /// An of that contains the descendants of every token in the source collection. public static IJEnumerable Descendants(this IEnumerable source) where T : JContainer { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(j => j.Descendants()).AsJEnumerable(); } @@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Linq /// An of that contains every token in the source collection, and the descendants of every token in the source collection. public static IJEnumerable DescendantsAndSelf(this IEnumerable source) where T : JContainer { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(j => j.DescendantsAndSelf()).AsJEnumerable(); } @@ -100,7 +100,7 @@ namespace Newtonsoft.Json.Linq /// An of that contains the properties of every object in the source collection. public static IJEnumerable Properties(this IEnumerable source) { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(d => d.Properties()).AsJEnumerable(); } @@ -169,7 +169,7 @@ namespace Newtonsoft.Json.Linq /// A converted value. public static U Value(this IEnumerable value) where T : JToken { - ValidationUtils.ArgumentNotNull(value, "source"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); JToken token = value as JToken; if (token == null) @@ -182,7 +182,7 @@ namespace Newtonsoft.Json.Linq internal static IEnumerable Values(this IEnumerable source, object key) where T : JToken { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); foreach (JToken token in source) { @@ -236,14 +236,14 @@ namespace Newtonsoft.Json.Linq /// An that contains the converted values of every token in the source collection. public static IEnumerable Children(this IEnumerable source) where T : JToken { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); return source.SelectMany(c => c.Children()).Convert(); } internal static IEnumerable Convert(this IEnumerable source) where T : JToken { - ValidationUtils.ArgumentNotNull(source, "source"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); foreach (T token in source) { diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index da10242d..5ca9a165 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -238,7 +238,7 @@ namespace Newtonsoft.Json.Linq { get { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); if (!(key is int)) { @@ -249,7 +249,7 @@ namespace Newtonsoft.Json.Linq } set { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); if (!(key is int)) { diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index ed5f362f..0a49a93e 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -126,7 +126,15 @@ namespace Newtonsoft.Json.Linq /// The constructor name. public JConstructor(string name) { - ValidationUtils.ArgumentNotNullOrEmpty(name, "name"); + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (name.Length == 0) + { + throw new ArgumentException("Constructor name cannot be empty.", nameof(name)); + } _name = name; } @@ -167,7 +175,7 @@ namespace Newtonsoft.Json.Linq { get { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); if (!(key is int)) { @@ -178,7 +186,7 @@ namespace Newtonsoft.Json.Linq } set { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); if (!(key is int)) { diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 07f306e4..6e902010 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -107,7 +107,7 @@ namespace Newtonsoft.Json.Linq internal JContainer(JContainer other) : this() { - ValidationUtils.ArgumentNotNull(other, "c"); + ValidationUtils.ArgumentNotNull(other, nameof(other)); int i = 0; foreach (JToken child in other) @@ -644,7 +644,7 @@ namespace Newtonsoft.Json.Linq internal virtual void ValidateToken(JToken o, JToken existing) { - ValidationUtils.ArgumentNotNull(o, "o"); + ValidationUtils.ArgumentNotNull(o, nameof(o)); if (o.Type == JTokenType.Property) { @@ -775,7 +775,7 @@ namespace Newtonsoft.Json.Linq internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings) { - ValidationUtils.ArgumentNotNull(r, "r"); + ValidationUtils.ArgumentNotNull(r, nameof(r)); IJsonLineInfo lineInfo = r as IJsonLineInfo; JContainer parent = this; diff --git a/Src/Newtonsoft.Json/Linq/JEnumerable.cs b/Src/Newtonsoft.Json/Linq/JEnumerable.cs index 8e75ed8f..612d4280 100644 --- a/Src/Newtonsoft.Json/Linq/JEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/JEnumerable.cs @@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Linq /// The enumerable. public JEnumerable(IEnumerable enumerable) { - ValidationUtils.ArgumentNotNull(enumerable, "enumerable"); + ValidationUtils.ArgumentNotNull(enumerable, nameof(enumerable)); _enumerable = enumerable; } diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 754c0919..431282fb 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -141,7 +141,7 @@ namespace Newtonsoft.Json.Linq internal override void ValidateToken(JToken o, JToken existing) { - ValidationUtils.ArgumentNotNull(o, "o"); + ValidationUtils.ArgumentNotNull(o, nameof(o)); if (o.Type != JTokenType.Property) { @@ -285,7 +285,7 @@ namespace Newtonsoft.Json.Linq { get { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); string propertyName = key as string; if (propertyName == null) @@ -297,7 +297,7 @@ namespace Newtonsoft.Json.Linq } set { - ValidationUtils.ArgumentNotNull(key, "o"); + ValidationUtils.ArgumentNotNull(key, nameof(key)); string propertyName = key as string; if (propertyName == null) @@ -317,7 +317,7 @@ namespace Newtonsoft.Json.Linq { get { - ValidationUtils.ArgumentNotNull(propertyName, "propertyName"); + ValidationUtils.ArgumentNotNull(propertyName, nameof(propertyName)); JProperty property = Property(propertyName); @@ -360,7 +360,7 @@ namespace Newtonsoft.Json.Linq /// A that contains the JSON that was read from the specified . public new static JObject Load(JsonReader reader, JsonLoadSettings settings) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); if (reader.TokenType == JsonToken.None) { diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index 259fe430..a1049cf0 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -299,7 +299,7 @@ namespace Newtonsoft.Json.Linq internal JProperty(string name) { // called from JTokenWriter - ValidationUtils.ArgumentNotNull(name, "name"); + ValidationUtils.ArgumentNotNull(name, nameof(name)); _name = name; } @@ -321,7 +321,7 @@ namespace Newtonsoft.Json.Linq /// The property content. public JProperty(string name, object content) { - ValidationUtils.ArgumentNotNull(name, "name"); + ValidationUtils.ArgumentNotNull(name, nameof(name)); _name = name; diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 55b31696..7a162847 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -471,7 +471,7 @@ namespace Newtonsoft.Json.Linq private static string GetType(JToken token) { - ValidationUtils.ArgumentNotNull(token, "token"); + ValidationUtils.ArgumentNotNull(token, nameof(token)); if (token is JProperty) { @@ -1888,8 +1888,8 @@ namespace Newtonsoft.Json.Linq internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer) { - ValidationUtils.ArgumentNotNull(o, "o"); - ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer"); + ValidationUtils.ArgumentNotNull(o, nameof(o)); + ValidationUtils.ArgumentNotNull(jsonSerializer, nameof(jsonSerializer)); JToken token; using (JTokenWriter jsonWriter = new JTokenWriter()) @@ -2074,7 +2074,7 @@ namespace Newtonsoft.Json.Linq /// The new object created from the JSON value. public object ToObject(Type objectType, JsonSerializer jsonSerializer) { - ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer"); + ValidationUtils.ArgumentNotNull(jsonSerializer, nameof(jsonSerializer)); using (JTokenReader jsonReader = new JTokenReader(this)) { @@ -2109,7 +2109,7 @@ namespace Newtonsoft.Json.Linq /// public static JToken ReadFrom(JsonReader reader, JsonLoadSettings settings) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); if (reader.TokenType == JsonToken.None) { diff --git a/Src/Newtonsoft.Json/Linq/JTokenReader.cs b/Src/Newtonsoft.Json/Linq/JTokenReader.cs index c3f26736..5f0ca876 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenReader.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenReader.cs @@ -52,7 +52,7 @@ namespace Newtonsoft.Json.Linq /// The token to read from. public JTokenReader(JToken token) { - ValidationUtils.ArgumentNotNull(token, "token"); + ValidationUtils.ArgumentNotNull(token, nameof(token)); _root = token; } diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index 1bed4e4e..03318509 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -74,7 +74,7 @@ namespace Newtonsoft.Json.Linq /// The container being written to. public JTokenWriter(JContainer container) { - ValidationUtils.ArgumentNotNull(container, "container"); + ValidationUtils.ArgumentNotNull(container, nameof(container)); _token = container; _parent = container; diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs index 49d80f1a..b0e02422 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs @@ -40,7 +40,7 @@ namespace Newtonsoft.Json.Linq.JsonPath public JPath(string expression) { - ValidationUtils.ArgumentNotNull(expression, "expression"); + ValidationUtils.ArgumentNotNull(expression, nameof(expression)); _expression = expression; Filters = new List(); diff --git a/Src/Newtonsoft.Json/Schema/Extensions.cs b/Src/Newtonsoft.Json/Schema/Extensions.cs index b1876e60..6cfbf300 100644 --- a/Src/Newtonsoft.Json/Schema/Extensions.cs +++ b/Src/Newtonsoft.Json/Schema/Extensions.cs @@ -117,8 +117,8 @@ namespace Newtonsoft.Json.Schema [Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")] public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler) { - ValidationUtils.ArgumentNotNull(source, "source"); - ValidationUtils.ArgumentNotNull(schema, "schema"); + ValidationUtils.ArgumentNotNull(source, nameof(source)); + ValidationUtils.ArgumentNotNull(schema, nameof(schema)); using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader())) { diff --git a/Src/Newtonsoft.Json/Schema/JsonSchema.cs b/Src/Newtonsoft.Json/Schema/JsonSchema.cs index 9da2eb83..7b322252 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchema.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchema.cs @@ -280,8 +280,8 @@ namespace Newtonsoft.Json.Schema /// The object representing the JSON Schema. public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver) { - ValidationUtils.ArgumentNotNull(reader, "reader"); - ValidationUtils.ArgumentNotNull(resolver, "resolver"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); + ValidationUtils.ArgumentNotNull(resolver, nameof(resolver)); JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver); return builder.Read(reader); @@ -305,7 +305,7 @@ namespace Newtonsoft.Json.Schema /// A populated from the string that contains JSON. public static JsonSchema Parse(string json, JsonSchemaResolver resolver) { - ValidationUtils.ArgumentNotNull(json, "json"); + ValidationUtils.ArgumentNotNull(json, nameof(json)); using (JsonReader reader = new JsonTextReader(new StringReader(json))) { @@ -329,8 +329,8 @@ namespace Newtonsoft.Json.Schema /// The resolver used. public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver) { - ValidationUtils.ArgumentNotNull(writer, "writer"); - ValidationUtils.ArgumentNotNull(resolver, "resolver"); + ValidationUtils.ArgumentNotNull(writer, nameof(writer)); + ValidationUtils.ArgumentNotNull(resolver, nameof(resolver)); JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver); schemaWriter.WriteSchema(this); diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index 0ecde46a..f003fdc6 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -82,8 +82,8 @@ namespace Newtonsoft.Json.Schema public TypeSchema(Type type, JsonSchema schema) { - ValidationUtils.ArgumentNotNull(type, "type"); - ValidationUtils.ArgumentNotNull(schema, "schema"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); + ValidationUtils.ArgumentNotNull(schema, nameof(schema)); Type = type; Schema = schema; @@ -164,8 +164,8 @@ namespace Newtonsoft.Json.Schema /// A generated from the specified type. public JsonSchema Generate(Type type, JsonSchemaResolver resolver, bool rootSchemaNullable) { - ValidationUtils.ArgumentNotNull(type, "type"); - ValidationUtils.ArgumentNotNull(resolver, "resolver"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); + ValidationUtils.ArgumentNotNull(resolver, nameof(resolver)); _resolver = resolver; @@ -231,7 +231,7 @@ namespace Newtonsoft.Json.Schema private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required) { - ValidationUtils.ArgumentNotNull(type, "type"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); string resolvedId = GetTypeId(type, false); string explicitId = GetTypeId(type, true); diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs index 5f478aed..c4d64edc 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs @@ -45,7 +45,7 @@ namespace Newtonsoft.Json.Schema public JsonSchemaWriter(JsonWriter writer, JsonSchemaResolver resolver) { - ValidationUtils.ArgumentNotNull(writer, "writer"); + ValidationUtils.ArgumentNotNull(writer, nameof(writer)); _writer = writer; _resolver = resolver; } @@ -67,7 +67,7 @@ namespace Newtonsoft.Json.Schema public void WriteSchema(JsonSchema schema) { - ValidationUtils.ArgumentNotNull(schema, "schema"); + ValidationUtils.ArgumentNotNull(schema, nameof(schema)); if (!_resolver.LoadedSchemas.Contains(schema)) { diff --git a/Src/Newtonsoft.Json/Schema/ValidationEventArgs.cs b/Src/Newtonsoft.Json/Schema/ValidationEventArgs.cs index 4e7753aa..b1444598 100644 --- a/Src/Newtonsoft.Json/Schema/ValidationEventArgs.cs +++ b/Src/Newtonsoft.Json/Schema/ValidationEventArgs.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Schema internal ValidationEventArgs(JsonSchemaException ex) { - ValidationUtils.ArgumentNotNull(ex, "ex"); + ValidationUtils.ArgumentNotNull(ex, nameof(ex)); _ex = ex; } diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index f4cd8df3..d65a4b4d 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -582,7 +582,7 @@ namespace Newtonsoft.Json.Serialization public DictionaryEnumerator(IEnumerable> e) { - ValidationUtils.ArgumentNotNull(e, "e"); + ValidationUtils.ArgumentNotNull(e, nameof(e)); _e = e.GetEnumerator(); } diff --git a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs index 211fcc55..dd9dce72 100644 --- a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs @@ -51,7 +51,7 @@ namespace Newtonsoft.Json.Serialization /// The member info. public DynamicValueProvider(MemberInfo memberInfo) { - ValidationUtils.ArgumentNotNull(memberInfo, "memberInfo"); + ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo)); _memberInfo = memberInfo; } diff --git a/Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs b/Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs index 81f8f3d6..97747405 100644 --- a/Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs @@ -51,7 +51,7 @@ namespace Newtonsoft.Json.Serialization /// The member info. public ExpressionValueProvider(MemberInfo memberInfo) { - ValidationUtils.ArgumentNotNull(memberInfo, "memberInfo"); + ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo)); _memberInfo = memberInfo; } diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index c9de554b..befe9e94 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -309,7 +309,7 @@ namespace Newtonsoft.Json.Serialization internal JsonContract(Type underlyingType) { - ValidationUtils.ArgumentNotNull(underlyingType, "underlyingType"); + ValidationUtils.ArgumentNotNull(underlyingType, nameof(underlyingType)); UnderlyingType = underlyingType; diff --git a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs index 549ff675..f4cbfc11 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs @@ -85,7 +85,7 @@ namespace Newtonsoft.Json.Serialization internal bool TryGetMember(IDynamicMetaObjectProvider dynamicProvider, string name, out object value) { - ValidationUtils.ArgumentNotNull(dynamicProvider, "dynamicProvider"); + ValidationUtils.ArgumentNotNull(dynamicProvider, nameof(dynamicProvider)); CallSite> callSite = _callSiteGetters.Get(name); @@ -105,7 +105,7 @@ namespace Newtonsoft.Json.Serialization internal bool TrySetMember(IDynamicMetaObjectProvider dynamicProvider, string name, object value) { - ValidationUtils.ArgumentNotNull(dynamicProvider, "dynamicProvider"); + ValidationUtils.ArgumentNotNull(dynamicProvider, nameof(dynamicProvider)); CallSite> callSite = _callSiteSetters.Get(name); diff --git a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs index f39201ea..df833574 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs @@ -40,8 +40,8 @@ namespace Newtonsoft.Json.Serialization public JsonFormatterConverter(JsonSerializerInternalReader reader, JsonISerializableContract contract, JsonProperty member) { - ValidationUtils.ArgumentNotNull(reader, "serializer"); - ValidationUtils.ArgumentNotNull(contract, "contract"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); + ValidationUtils.ArgumentNotNull(contract, nameof(contract)); _reader = reader; _contract = contract; @@ -50,7 +50,7 @@ namespace Newtonsoft.Json.Serialization private T GetTokenValue(object value) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); JValue v = (JValue)value; return (T)System.Convert.ChangeType(v.Value, typeof(T), CultureInfo.InvariantCulture); @@ -58,7 +58,7 @@ namespace Newtonsoft.Json.Serialization public object Convert(object value, Type type) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); JToken token = value as JToken; if (token == null) @@ -71,7 +71,7 @@ namespace Newtonsoft.Json.Serialization public object Convert(object value, TypeCode typeCode) { - ValidationUtils.ArgumentNotNull(value, "value"); + ValidationUtils.ArgumentNotNull(value, nameof(value)); if (value is JValue) { diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs index c39f2fa0..f7e09fa3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalBase.cs @@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Serialization protected JsonSerializerInternalBase(JsonSerializer serializer) { - ValidationUtils.ArgumentNotNull(serializer, "serializer"); + ValidationUtils.ArgumentNotNull(serializer, nameof(serializer)); Serializer = serializer; TraceWriter = serializer.TraceWriter; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 89a82ef7..5da4476f 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Serialization public void Populate(JsonReader reader, object target) { - ValidationUtils.ArgumentNotNull(target, "target"); + ValidationUtils.ArgumentNotNull(target, nameof(target)); Type objectType = target.GetType(); @@ -207,7 +207,7 @@ namespace Newtonsoft.Json.Serialization private JToken CreateJToken(JsonReader reader, JsonContract contract) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); if (contract != null) { @@ -234,7 +234,7 @@ namespace Newtonsoft.Json.Serialization private JToken CreateJObject(JsonReader reader) { - ValidationUtils.ArgumentNotNull(reader, "reader"); + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); // this is needed because we've already read inside the object, looking for metadata properties using (JTokenWriter writer = new JTokenWriter()) @@ -1884,7 +1884,7 @@ namespace Newtonsoft.Json.Serialization private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor creator, string id) { - ValidationUtils.ArgumentNotNull(creator, "creator"); + ValidationUtils.ArgumentNotNull(creator, nameof(creator)); // only need to keep a track of properies presence if they are required or a value should be defaulted if missing bool trackPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate)); diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerProxy.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerProxy.cs index d63d5670..cd1c9101 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerProxy.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerProxy.cs @@ -225,7 +225,7 @@ namespace Newtonsoft.Json.Serialization public JsonSerializerProxy(JsonSerializerInternalReader serializerReader) { - ValidationUtils.ArgumentNotNull(serializerReader, "serializerReader"); + ValidationUtils.ArgumentNotNull(serializerReader, nameof(serializerReader)); _serializerReader = serializerReader; _serializer = serializerReader.Serializer; @@ -233,7 +233,7 @@ namespace Newtonsoft.Json.Serialization public JsonSerializerProxy(JsonSerializerInternalWriter serializerWriter) { - ValidationUtils.ArgumentNotNull(serializerWriter, "serializerWriter"); + ValidationUtils.ArgumentNotNull(serializerWriter, nameof(serializerWriter)); _serializerWriter = serializerWriter; _serializer = serializerWriter.Serializer; diff --git a/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs b/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs index 8de11a2b..5e9bc4ec 100644 --- a/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Serialization /// The instance to get attributes for. This parameter should be a , , or . public ReflectionAttributeProvider(object attributeProvider) { - ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider"); + ValidationUtils.ArgumentNotNull(attributeProvider, nameof(attributeProvider)); _attributeProvider = attributeProvider; } diff --git a/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs b/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs index 0fcc11ff..93af29ef 100644 --- a/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Serialization /// The member info. public ReflectionValueProvider(MemberInfo memberInfo) { - ValidationUtils.ArgumentNotNull(memberInfo, "memberInfo"); + ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo)); _memberInfo = memberInfo; } diff --git a/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs b/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs index 7bad0516..8f298d13 100644 --- a/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs +++ b/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs @@ -41,7 +41,7 @@ namespace Newtonsoft.Json.Utilities public Base64Encoder(TextWriter writer) { - ValidationUtils.ArgumentNotNull(writer, "writer"); + ValidationUtils.ArgumentNotNull(writer, nameof(writer)); _writer = writer; } diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs index 01acbad9..6b84fc34 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs @@ -83,7 +83,7 @@ namespace Newtonsoft.Json.Utilities #if (NET20 || NET35 || PORTABLE40) public static void AddRange(this IList initial, IEnumerable collection) { - ValidationUtils.ArgumentNotNull(initial, "initial"); + ValidationUtils.ArgumentNotNull(initial, nameof(initial)); // because earlier versions of .NET didn't support covariant generics initial.AddRange(collection.Cast()); @@ -92,7 +92,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsDictionaryType(Type type) { - ValidationUtils.ArgumentNotNull(type, "type"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); if (typeof(IDictionary).IsAssignableFrom(type)) { diff --git a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs index 97c37811..6afc9ac6 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs @@ -50,7 +50,7 @@ namespace Newtonsoft.Json.Utilities public CollectionWrapper(IList list) { - ValidationUtils.ArgumentNotNull(list, "list"); + ValidationUtils.ArgumentNotNull(list, nameof(list)); if (list is ICollection) { @@ -64,7 +64,7 @@ namespace Newtonsoft.Json.Utilities public CollectionWrapper(ICollection list) { - ValidationUtils.ArgumentNotNull(list, "list"); + ValidationUtils.ArgumentNotNull(list, nameof(list)); _genericCollection = list; } diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index 2c44aa36..4819c23d 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -53,14 +53,14 @@ namespace Newtonsoft.Json.Utilities public DictionaryWrapper(IDictionary dictionary) { - ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); + ValidationUtils.ArgumentNotNull(dictionary, nameof(dictionary)); _dictionary = dictionary; } public DictionaryWrapper(IDictionary dictionary) { - ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); + ValidationUtils.ArgumentNotNull(dictionary, nameof(dictionary)); _genericDictionary = dictionary; } @@ -68,7 +68,7 @@ namespace Newtonsoft.Json.Utilities #if !(NET40 || NET35 || NET20 || PORTABLE40) public DictionaryWrapper(IReadOnlyDictionary dictionary) { - ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); + ValidationUtils.ArgumentNotNull(dictionary, nameof(dictionary)); _readOnlyDictionary = dictionary; } @@ -478,7 +478,7 @@ namespace Newtonsoft.Json.Utilities public DictionaryEnumerator(IEnumerator> e) { - ValidationUtils.ArgumentNotNull(e, "e"); + ValidationUtils.ArgumentNotNull(e, nameof(e)); _e = e; } diff --git a/Src/Newtonsoft.Json/Utilities/EnumUtils.cs b/Src/Newtonsoft.Json/Utilities/EnumUtils.cs index cb05b96a..69d33dcb 100644 --- a/Src/Newtonsoft.Json/Utilities/EnumUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/EnumUtils.cs @@ -124,7 +124,10 @@ namespace Newtonsoft.Json.Utilities throw new ArgumentNullException(nameof(enumType)); } - ValidationUtils.ArgumentTypeIsEnum(enumType, "enumType"); + if (!enumType.IsEnum()) + { + throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), nameof(enumType)); + } IList enumValues = GetValues(enumType); IList enumNames = GetNames(enumType); diff --git a/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs index e87a93bb..02042626 100644 --- a/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/ExpressionReflectionDelegateFactory.cs @@ -45,7 +45,7 @@ namespace Newtonsoft.Json.Utilities public override ObjectConstructor CreateParameterizedConstructor(MethodBase method) { - ValidationUtils.ArgumentNotNull(method, "method"); + ValidationUtils.ArgumentNotNull(method, nameof(method)); Type type = typeof(object); @@ -61,7 +61,7 @@ namespace Newtonsoft.Json.Utilities public override MethodCall CreateMethodCall(MethodBase method) { - ValidationUtils.ArgumentNotNull(method, "method"); + ValidationUtils.ArgumentNotNull(method, nameof(method)); Type type = typeof(object); @@ -222,7 +222,7 @@ namespace Newtonsoft.Json.Utilities public override Func CreateGet(PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); Type instanceType = typeof(T); Type resultType = typeof(object); @@ -253,7 +253,7 @@ namespace Newtonsoft.Json.Utilities public override Func CreateGet(FieldInfo fieldInfo) { - ValidationUtils.ArgumentNotNull(fieldInfo, "fieldInfo"); + ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo)); ParameterExpression sourceParameter = Expression.Parameter(typeof(T), "source"); @@ -277,7 +277,7 @@ namespace Newtonsoft.Json.Utilities public override Action CreateSet(FieldInfo fieldInfo) { - ValidationUtils.ArgumentNotNull(fieldInfo, "fieldInfo"); + ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo)); // use reflection for structs // expression doesn't correctly set value @@ -313,7 +313,7 @@ namespace Newtonsoft.Json.Utilities public override Action CreateSet(PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); // use reflection for structs // expression doesn't correctly set value diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 313b5047..4fe8b051 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -303,11 +303,6 @@ namespace Newtonsoft.Json.Utilities } } - public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters) - { - return ToEscapedJavaScriptString(value, delimiter, appendDelimiters, StringEscapeHandling.Default); - } - public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling) { bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter); diff --git a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs index d5683925..52e1f5a5 100644 --- a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs @@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Utilities public override ObjectConstructor CreateParameterizedConstructor(MethodBase method) { - ValidationUtils.ArgumentNotNull(method, "method"); + ValidationUtils.ArgumentNotNull(method, nameof(method)); ConstructorInfo c = method as ConstructorInfo; if (c != null) @@ -63,7 +63,7 @@ namespace Newtonsoft.Json.Utilities public override MethodCall CreateMethodCall(MethodBase method) { - ValidationUtils.ArgumentNotNull(method, "method"); + ValidationUtils.ArgumentNotNull(method, nameof(method)); ConstructorInfo c = method as ConstructorInfo; if (c != null) @@ -76,7 +76,7 @@ namespace Newtonsoft.Json.Utilities public override Func CreateDefaultConstructor(Type type) { - ValidationUtils.ArgumentNotNull(type, "type"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); if (type.IsValueType()) { @@ -90,28 +90,28 @@ namespace Newtonsoft.Json.Utilities public override Func CreateGet(PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); return o => propertyInfo.GetValue(o, null); } public override Func CreateGet(FieldInfo fieldInfo) { - ValidationUtils.ArgumentNotNull(fieldInfo, "fieldInfo"); + ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo)); return o => fieldInfo.GetValue(o); } public override Action CreateSet(FieldInfo fieldInfo) { - ValidationUtils.ArgumentNotNull(fieldInfo, "fieldInfo"); + ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo)); return (o, v) => fieldInfo.SetValue(o, v); } public override Action CreateSet(PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); return (o, v) => propertyInfo.SetValue(o, v, null); } diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index ed4ddba4..ba79efca 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -96,7 +96,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsVirtual(this PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); MethodInfo m = propertyInfo.GetGetMethod(); if (m != null && m.IsVirtual) @@ -115,7 +115,7 @@ namespace Newtonsoft.Json.Utilities public static MethodInfo GetBaseDefinition(this PropertyInfo propertyInfo) { - ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); MethodInfo m = propertyInfo.GetGetMethod(); if (m != null) @@ -227,7 +227,7 @@ namespace Newtonsoft.Json.Utilities public static bool HasDefaultConstructor(Type t, bool nonPublic) { - ValidationUtils.ArgumentNotNull(t, "t"); + ValidationUtils.ArgumentNotNull(t, nameof(t)); if (t.IsValueType()) { @@ -255,7 +255,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsNullable(Type t) { - ValidationUtils.ArgumentNotNull(t, "t"); + ValidationUtils.ArgumentNotNull(t, nameof(t)); if (t.IsValueType()) { @@ -267,7 +267,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsNullableType(Type t) { - ValidationUtils.ArgumentNotNull(t, "t"); + ValidationUtils.ArgumentNotNull(t, nameof(t)); return (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } @@ -298,8 +298,8 @@ namespace Newtonsoft.Json.Utilities public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType) { - ValidationUtils.ArgumentNotNull(type, "type"); - ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, "genericInterfaceDefinition"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); + ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, nameof(genericInterfaceDefinition)); if (!genericInterfaceDefinition.IsInterface() || !genericInterfaceDefinition.IsGenericTypeDefinition()) { @@ -346,8 +346,8 @@ namespace Newtonsoft.Json.Utilities public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType) { - ValidationUtils.ArgumentNotNull(type, "type"); - ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); + ValidationUtils.ArgumentNotNull(genericClassDefinition, nameof(genericClassDefinition)); if (!genericClassDefinition.IsClass() || !genericClassDefinition.IsGenericTypeDefinition()) { @@ -386,7 +386,7 @@ namespace Newtonsoft.Json.Utilities /// The type of the typed collection's items. public static Type GetCollectionItemType(Type type) { - ValidationUtils.ArgumentNotNull(type, "type"); + ValidationUtils.ArgumentNotNull(type, nameof(type)); Type genericListType; if (type.IsArray) @@ -412,7 +412,7 @@ namespace Newtonsoft.Json.Utilities public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType) { - ValidationUtils.ArgumentNotNull(dictionaryType, "type"); + ValidationUtils.ArgumentNotNull(dictionaryType, nameof(dictionaryType)); Type genericDictionaryType; if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType)) @@ -445,7 +445,7 @@ namespace Newtonsoft.Json.Utilities /// The underlying type of the member. public static Type GetMemberUnderlyingType(MemberInfo member) { - ValidationUtils.ArgumentNotNull(member, "member"); + ValidationUtils.ArgumentNotNull(member, nameof(member)); switch (member.MemberType()) { @@ -471,7 +471,7 @@ namespace Newtonsoft.Json.Utilities /// public static bool IsIndexedProperty(MemberInfo member) { - ValidationUtils.ArgumentNotNull(member, "member"); + ValidationUtils.ArgumentNotNull(member, nameof(member)); PropertyInfo propertyInfo = member as PropertyInfo; @@ -494,7 +494,7 @@ namespace Newtonsoft.Json.Utilities /// public static bool IsIndexedProperty(PropertyInfo property) { - ValidationUtils.ArgumentNotNull(property, "property"); + ValidationUtils.ArgumentNotNull(property, nameof(property)); return (property.GetIndexParameters().Length > 0); } @@ -507,8 +507,8 @@ namespace Newtonsoft.Json.Utilities /// The member's value on the object. public static object GetMemberValue(MemberInfo member, object target) { - ValidationUtils.ArgumentNotNull(member, "member"); - ValidationUtils.ArgumentNotNull(target, "target"); + ValidationUtils.ArgumentNotNull(member, nameof(member)); + ValidationUtils.ArgumentNotNull(target, nameof(target)); switch (member.MemberType()) { @@ -536,8 +536,8 @@ namespace Newtonsoft.Json.Utilities /// The value. public static void SetMemberValue(MemberInfo member, object target, object value) { - ValidationUtils.ArgumentNotNull(member, "member"); - ValidationUtils.ArgumentNotNull(target, "target"); + ValidationUtils.ArgumentNotNull(member, nameof(member)); + ValidationUtils.ArgumentNotNull(target, nameof(target)); switch (member.MemberType()) { @@ -755,7 +755,7 @@ namespace Newtonsoft.Json.Utilities public static Attribute[] GetAttributes(object attributeProvider, Type attributeType, bool inherit) { - ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider"); + ValidationUtils.ArgumentNotNull(attributeProvider, nameof(attributeProvider)); object provider = attributeProvider; @@ -919,7 +919,7 @@ namespace Newtonsoft.Json.Utilities public static IEnumerable GetFields(Type targetType, BindingFlags bindingAttr) { - ValidationUtils.ArgumentNotNull(targetType, "targetType"); + ValidationUtils.ArgumentNotNull(targetType, nameof(targetType)); List fieldInfos = new List(targetType.GetFields(bindingAttr)); #if !PORTABLE @@ -955,7 +955,7 @@ namespace Newtonsoft.Json.Utilities public static IEnumerable GetProperties(Type targetType, BindingFlags bindingAttr) { - ValidationUtils.ArgumentNotNull(targetType, "targetType"); + ValidationUtils.ArgumentNotNull(targetType, nameof(targetType)); List propertyInfos = new List(targetType.GetProperties(bindingAttr)); diff --git a/Src/Newtonsoft.Json/Utilities/StringReference.cs b/Src/Newtonsoft.Json/Utilities/StringReference.cs index d1e8c474..5028a41c 100644 --- a/Src/Newtonsoft.Json/Utilities/StringReference.cs +++ b/Src/Newtonsoft.Json/Utilities/StringReference.cs @@ -68,11 +68,6 @@ namespace Newtonsoft.Json.Utilities internal static class StringReferenceExtensions { - public static int IndexOf(this StringReference s, char c) - { - return IndexOf(s, c, 0, 0); - } - public static int IndexOf(this StringReference s, char c, int startIndex, int length) { int index = Array.IndexOf(s.Chars, c, s.StartIndex + startIndex, length); diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index 53adce4c..3fad3249 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -69,7 +69,7 @@ namespace Newtonsoft.Json.Utilities { // leave this a private to force code to use an explicit overload // avoids stack memory being reserved for the object array - ValidationUtils.ArgumentNotNull(format, "format"); + ValidationUtils.ArgumentNotNull(format, nameof(format)); return string.Format(provider, format, args); } diff --git a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs index 9f122540..845ba3ea 100644 --- a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs @@ -24,38 +24,11 @@ #endregion using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using System.Globalization; namespace Newtonsoft.Json.Utilities { internal static class ValidationUtils { - public static void ArgumentNotNullOrEmpty(string value, string parameterName) - { - if (value == null) - { - throw new ArgumentNullException(parameterName); - } - - if (value.Length == 0) - { - throw new ArgumentException("'{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName); - } - } - - public static void ArgumentTypeIsEnum(Type enumType, string parameterName) - { - ArgumentNotNull(enumType, "enumType"); - - if (!enumType.IsEnum()) - { - throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName); - } - } - public static void ArgumentNotNull(object value, string parameterName) { if (value == null)