-Refactored buffer pool

This commit is contained in:
James Newton-King
2015-12-13 15:40:37 +13:00
parent 4e2acac33a
commit 815b283e58
48 changed files with 187 additions and 186 deletions
@@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
{
if (website == null)
{
throw new ArgumentNullException("website");
throw new ArgumentNullException(nameof(website));
}
Url = website.Url;
+22 -24
View File
@@ -1179,39 +1179,37 @@ third line", jsonTextReader.Value);
Assert.AreEqual("type", reader.Value);
}
public class FakeBufferPool : IJsonBufferPool<char>
public class FakeArrayPool : IArrayPool<char>
{
public readonly List<char[]> FreeBuffers = new List<char[]>();
public readonly List<char[]> UsedBuffers = new List<char[]>();
public readonly List<char[]> FreeArrays = new List<char[]>();
public readonly List<char[]> UsedArrays = new List<char[]>();
public char[] RentBuffer(int minSize)
public char[] Rent(int minimumLength)
{
char[] buffer = FreeBuffers.FirstOrDefault(b => b.Length >= minSize);
if (buffer != null)
char[] a = FreeArrays.FirstOrDefault(b => b.Length >= minimumLength);
if (a != null)
{
FreeBuffers.Remove(buffer);
UsedBuffers.Add(buffer);
FreeArrays.Remove(a);
UsedArrays.Add(a);
return buffer;
return a;
}
buffer = new char[minSize];
UsedBuffers.Add(buffer);
a = new char[minimumLength];
UsedArrays.Add(a);
return buffer;
return a;
}
public void ReturnBuffer(ref char[] buffer)
public void Return(char[] array)
{
if (UsedBuffers.Remove(buffer))
if (UsedArrays.Remove(array))
{
FreeBuffers.Add(buffer);
FreeArrays.Add(array);
// smallest first so the first buffer large enough is rented
FreeBuffers.Sort((b1, b2) => Comparer<int>.Default.Compare(b1.Length, b2.Length));
// smallest first so the first array large enough is rented
FreeArrays.Sort((b1, b2) => Comparer<int>.Default.Compare(b1.Length, b2.Length));
}
buffer = null;
}
}
@@ -1228,13 +1226,13 @@ third line", jsonTextReader.Value);
]
}";
FakeBufferPool bufferPool = new FakeBufferPool();
FakeArrayPool arrayPool = new FakeArrayPool();
for (int i = 0; i < 1000; i++)
{
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
reader.BufferPool = bufferPool;
reader.ArrayPool = arrayPool;
while (reader.Read())
{
@@ -1243,12 +1241,12 @@ third line", jsonTextReader.Value);
if ((i + 1) % 100 == 0)
{
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
}
}
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
Assert.AreEqual(6, bufferPool.FreeBuffers.Count);
Assert.AreEqual(0, arrayPool.UsedArrays.Count);
Assert.AreEqual(6, arrayPool.FreeArrays.Count);
}
[Test]
@@ -59,7 +59,7 @@ namespace Newtonsoft.Json.Tests
[Test]
public void BufferTest()
{
JsonTextReaderTest.FakeBufferPool bufferPool = new JsonTextReaderTest.FakeBufferPool();
JsonTextReaderTest.FakeArrayPool arrayPool = new JsonTextReaderTest.FakeArrayPool();
string longString = new string('A', 2000);
string longEscapedString = "Hello!" + new string('!', 50) + new string('\n', 1000) + "Good bye!";
@@ -71,7 +71,7 @@ namespace Newtonsoft.Json.Tests
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.BufferPool = bufferPool;
writer.ArrayPool = arrayPool;
writer.WriteStartObject();
@@ -92,12 +92,12 @@ namespace Newtonsoft.Json.Tests
if ((i + 1) % 100 == 0)
{
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
}
}
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
Assert.AreEqual(3, bufferPool.FreeBuffers.Count);
Assert.AreEqual(0, arrayPool.UsedArrays.Count);
Assert.AreEqual(3, arrayPool.FreeArrays.Count);
}
[Test]
@@ -784,7 +784,7 @@ If attributes are not mentioned, default values are used in each case.
json = SerializeBinaryFormatter(value);
break;
default:
throw new ArgumentOutOfRangeException("method");
throw new ArgumentOutOfRangeException(nameof(method));
}
return json;
@@ -991,7 +991,7 @@ If attributes are not mentioned, default values are used in each case.
case SerializeMethod.DataContractJsonSerializer:
return DeserializeDataContractJson<T>((string)json);
default:
throw new ArgumentOutOfRangeException("method");
throw new ArgumentOutOfRangeException(nameof(method));
}
}
@@ -60,7 +60,7 @@ namespace Newtonsoft.Json.Tests.Schema
[TestFixture]
public class JsonSchemaSpecTests : TestFixtureBase
{
[TestCaseSourceAttribute("GetSpecTestDetails")]
[TestCaseSourceAttribute(nameof(GetSpecTestDetails))]
public void SpecTest(JsonSchemaSpecTest jsonSchemaSpecTest)
{
JsonSchema s = JsonSchema.Read(jsonSchemaSpecTest.Schema.CreateReader());
@@ -4514,7 +4514,7 @@ Path '', line 1, position 1.");
{
if (dictionary == null)
{
throw new ArgumentNullException("dictionary");
throw new ArgumentNullException(nameof(dictionary));
}
foreach (var entry in dictionary)
@@ -8534,7 +8534,7 @@ Path '', line 1, position 1.");
{
if (bar == null)
{
throw new ArgumentNullException("bar");
throw new ArgumentNullException(nameof(bar));
}
Bar = bar;
+2 -2
View File
@@ -190,7 +190,7 @@ namespace Newtonsoft.Json.Bson
}
break;
default:
throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
}
}
@@ -324,7 +324,7 @@ namespace Newtonsoft.Json.Bson
return value.CalculatedSize;
}
default:
throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
throw new ArgumentOutOfRangeException(nameof(t), "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
}
}
}
+1 -1
View File
@@ -48,7 +48,7 @@ namespace Newtonsoft.Json.Bson
ValidationUtils.ArgumentNotNull(value, "value");
if (value.Length != 12)
{
throw new ArgumentException("An ObjectId must be 12 bytes", "value");
throw new ArgumentException("An ObjectId must be 12 bytes", nameof(value));
}
Value = value;
+1 -1
View File
@@ -623,7 +623,7 @@ namespace Newtonsoft.Json.Bson
SetToken(JsonToken.Integer, ReadInt64());
break;
default:
throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type);
throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + type);
}
}
@@ -985,7 +985,7 @@ namespace Newtonsoft.Json.Converters
}
#endif
throw new ArgumentException("Value must be an XML object.", "value");
throw new ArgumentException("Value must be an XML object.", nameof(value));
}
private void PushParentNamespaces(IXmlNode node, XmlNamespaceManager manager)
+22
View File
@@ -0,0 +1,22 @@
namespace Newtonsoft.Json
{
/// <summary>
/// Provides an interface for using pooled arrays.
/// </summary>
/// <typeparam name="T">The array type content.</typeparam>
public interface IArrayPool<T>
{
/// <summary>
/// Rent a array from the pool. This array must be returned when it is no longer needed.
/// </summary>
/// <param name="minimumLength">The minimum required length of the array. The returned array may be larger.</param>
/// <returns>The rented array from the pool. This array must be returned when it is no longer needed.</returns>
T[] Rent(int minimumLength);
/// <summary>
/// Return an array to the pool.
/// </summary>
/// <param name="array">The array that is being returned.</param>
void Return(T[] array);
}
}
-22
View File
@@ -1,22 +0,0 @@
namespace Newtonsoft.Json
{
/// <summary>
/// Provides an interface for using pooled buffers.
/// </summary>
/// <typeparam name="T">The buffer type content.</typeparam>
public interface IJsonBufferPool<T>
{
/// <summary>
/// Rent a buffer from the pool. This buffer must be returned when it is no longer needed.
/// </summary>
/// <param name="minSize">The minimum required size of the buffer. The returned buffer may be larger.</param>
/// <returns>The rented buffer from the pool.</returns>
T[] RentBuffer(int minSize);
/// <summary>
/// Return a buffer to the pool.
/// </summary>
/// <param name="buffer">The buffer that is being returned.</param>
void ReturnBuffer(ref T[] buffer);
}
}
+1 -1
View File
@@ -444,7 +444,7 @@ namespace Newtonsoft.Json
{
if (delimiter != '"' && delimiter != '\'')
{
throw new ArgumentException("Delimiter must be a single or double quote.", "delimiter");
throw new ArgumentException("Delimiter must be a single or double quote.", nameof(delimiter));
}
return JavaScriptUtils.ToEscapedJavaScriptString(value, delimiter, true, stringEscapeHandling);
@@ -60,7 +60,7 @@ namespace Newtonsoft.Json
{
if (converterType == null)
{
throw new ArgumentNullException("converterType");
throw new ArgumentNullException(nameof(converterType));
}
_converterType = converterType;
+4 -4
View File
@@ -177,7 +177,7 @@ namespace Newtonsoft.Json
{
if (value < DateTimeZoneHandling.Local || value > DateTimeZoneHandling.RoundtripKind)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_dateTimeZoneHandling = value;
@@ -200,7 +200,7 @@ namespace Newtonsoft.Json
#endif
)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_dateParseHandling = value;
@@ -217,7 +217,7 @@ namespace Newtonsoft.Json
{
if (value < FloatParseHandling.Double || value > FloatParseHandling.Decimal)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_floatParseHandling = value;
@@ -243,7 +243,7 @@ namespace Newtonsoft.Json
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", "value");
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
+13 -13
View File
@@ -91,7 +91,7 @@ namespace Newtonsoft.Json
{
if (value == null)
{
throw new ArgumentNullException("value", "Reference resolver cannot be null.");
throw new ArgumentNullException(nameof(value), "Reference resolver cannot be null.");
}
_referenceResolver = value;
@@ -108,7 +108,7 @@ namespace Newtonsoft.Json
{
if (value == null)
{
throw new ArgumentNullException("value", "Serialization binder cannot be null.");
throw new ArgumentNullException(nameof(value), "Serialization binder cannot be null.");
}
_binder = value;
@@ -145,7 +145,7 @@ namespace Newtonsoft.Json
{
if (value < TypeNameHandling.None || value > TypeNameHandling.Auto)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_typeNameHandling = value;
@@ -163,7 +163,7 @@ namespace Newtonsoft.Json
{
if (value < FormatterAssemblyStyle.Simple || value > FormatterAssemblyStyle.Full)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_typeNameAssemblyFormat = value;
@@ -180,7 +180,7 @@ namespace Newtonsoft.Json
{
if (value < PreserveReferencesHandling.None || value > PreserveReferencesHandling.All)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_preserveReferencesHandling = value;
@@ -197,7 +197,7 @@ namespace Newtonsoft.Json
{
if (value < ReferenceLoopHandling.Error || value > ReferenceLoopHandling.Serialize)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_referenceLoopHandling = value;
@@ -214,7 +214,7 @@ namespace Newtonsoft.Json
{
if (value < MissingMemberHandling.Ignore || value > MissingMemberHandling.Error)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_missingMemberHandling = value;
@@ -231,7 +231,7 @@ namespace Newtonsoft.Json
{
if (value < NullValueHandling.Include || value > NullValueHandling.Ignore)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_nullValueHandling = value;
@@ -248,7 +248,7 @@ namespace Newtonsoft.Json
{
if (value < DefaultValueHandling.Include || value > DefaultValueHandling.IgnoreAndPopulate)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_defaultValueHandling = value;
@@ -266,7 +266,7 @@ namespace Newtonsoft.Json
{
if (value < ObjectCreationHandling.Auto || value > ObjectCreationHandling.Replace)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_objectCreationHandling = value;
@@ -284,7 +284,7 @@ namespace Newtonsoft.Json
{
if (value < ConstructorHandling.Default || value > ConstructorHandling.AllowNonPublicDefaultConstructor)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_constructorHandling = value;
@@ -302,7 +302,7 @@ namespace Newtonsoft.Json
{
if (value < MetadataPropertyHandling.Default || value > MetadataPropertyHandling.Ignore)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_metadataPropertyHandling = value;
@@ -443,7 +443,7 @@ namespace Newtonsoft.Json
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", "value");
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
@@ -287,7 +287,7 @@ namespace Newtonsoft.Json
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", "value");
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
+18 -17
View File
@@ -64,7 +64,7 @@ namespace Newtonsoft.Json
private bool _isEndOfFile;
private StringBuffer _stringBuffer;
private StringReference _stringReference;
private IJsonBufferPool<char> _bufferPool;
private IArrayPool<char> _arrayPool;
internal PropertyNameTable NameTable;
/// <summary>
@@ -75,7 +75,7 @@ namespace Newtonsoft.Json
{
if (reader == null)
{
throw new ArgumentNullException("reader");
throw new ArgumentNullException(nameof(reader));
}
_reader = reader;
@@ -92,17 +92,17 @@ namespace Newtonsoft.Json
/// <summary>
/// Gets or sets the reader's character buffer pool.
/// </summary>
public IJsonBufferPool<char> BufferPool
public IArrayPool<char> ArrayPool
{
get { return _bufferPool; }
get { return _arrayPool; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
throw new ArgumentNullException(nameof(value));
}
_bufferPool = value;
_arrayPool = value;
}
}
@@ -110,7 +110,7 @@ namespace Newtonsoft.Json
{
if (_stringBuffer.IsEmpty)
{
_stringBuffer = new StringBuffer(_bufferPool, 1024);
_stringBuffer = new StringBuffer(_arrayPool, 1024);
}
}
@@ -249,11 +249,11 @@ namespace Newtonsoft.Json
int newArrayLength = Math.Max(_chars.Length * 2, _charsUsed + charsRequired + 1);
// increase the size of the buffer
char[] dst = BufferUtils.RentBuffer(_bufferPool, newArrayLength);
char[] dst = BufferUtils.RentBuffer(_arrayPool, newArrayLength);
BlockCopyChars(_chars, 0, dst, 0, _chars.Length);
BufferUtils.ReturnBuffer(_bufferPool, ref _chars);
BufferUtils.ReturnBuffer(_arrayPool, _chars);
_chars = dst;
}
@@ -264,14 +264,14 @@ namespace Newtonsoft.Json
if (remainingCharCount + charsRequired + 1 >= _chars.Length)
{
// the remaining count plus the required is bigger than the current buffer size
char[] dst = BufferUtils.RentBuffer(_bufferPool, remainingCharCount + charsRequired + 1);
char[] dst = BufferUtils.RentBuffer(_arrayPool, remainingCharCount + charsRequired + 1);
if (remainingCharCount > 0)
{
BlockCopyChars(_chars, _charPos, dst, 0, remainingCharCount);
}
BufferUtils.ReturnBuffer(_bufferPool, ref _chars);
BufferUtils.ReturnBuffer(_arrayPool, _chars);
_chars = dst;
}
@@ -429,7 +429,7 @@ namespace Newtonsoft.Json
{
if (_chars == null)
{
_chars = BufferUtils.RentBuffer(_bufferPool, 1024);
_chars = BufferUtils.RentBuffer(_arrayPool, 1024);
_chars[0] = '\0';
}
@@ -644,7 +644,7 @@ namespace Newtonsoft.Json
if (charPos > lastWritePosition)
{
_stringBuffer.Append(_bufferPool, _chars, lastWritePosition, charPos - lastWritePosition);
_stringBuffer.Append(_arrayPool, _chars, lastWritePosition, charPos - lastWritePosition);
}
_stringReference = new StringReference(_stringBuffer.InternalBuffer, 0, _stringBuffer.Position);
@@ -663,10 +663,10 @@ namespace Newtonsoft.Json
{
if (writeToPosition > lastWritePosition)
{
_stringBuffer.Append(_bufferPool, _chars, lastWritePosition, writeToPosition - lastWritePosition);
_stringBuffer.Append(_arrayPool, _chars, lastWritePosition, writeToPosition - lastWritePosition);
}
_stringBuffer.Append(_bufferPool, writeChar);
_stringBuffer.Append(_arrayPool, writeChar);
}
private char ParseUnicode()
@@ -1759,7 +1759,8 @@ namespace Newtonsoft.Json
if (_chars != null)
{
BufferUtils.ReturnBuffer(_bufferPool, ref _chars);
BufferUtils.ReturnBuffer(_arrayPool, _chars);
_chars = null;
}
if (CloseInput && _reader != null)
@@ -1771,7 +1772,7 @@ namespace Newtonsoft.Json
#endif
}
_stringBuffer.Clear(_bufferPool);
_stringBuffer.Clear(_arrayPool);
}
/// <summary>
+14 -10
View File
@@ -49,7 +49,7 @@ namespace Newtonsoft.Json
private bool _quoteName;
private bool[] _charEscapeFlags;
private char[] _writeBuffer;
private IJsonBufferPool<char> _bufferPool;
private IArrayPool<char> _arrayPool;
private char[] _indentChars;
private Base64Encoder Base64Encoder
@@ -66,19 +66,19 @@ namespace Newtonsoft.Json
}
/// <summary>
/// Gets or sets the writer's character buffer pool.
/// Gets or sets the writer's character array pool.
/// </summary>
public IJsonBufferPool<char> BufferPool
public IArrayPool<char> ArrayPool
{
get { return _bufferPool; }
get { return _arrayPool; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
throw new ArgumentNullException(nameof(value));
}
_bufferPool = value;
_arrayPool = value;
}
}
@@ -150,7 +150,7 @@ namespace Newtonsoft.Json
{
if (textWriter == null)
{
throw new ArgumentNullException("textWriter");
throw new ArgumentNullException(nameof(textWriter));
}
_writer = textWriter;
@@ -177,7 +177,11 @@ namespace Newtonsoft.Json
{
base.Close();
BufferUtils.ReturnBuffer(_bufferPool, ref _writeBuffer);
if (_writeBuffer != null)
{
BufferUtils.ReturnBuffer(_arrayPool, _writeBuffer);
_writeBuffer = null;
}
if (CloseOutput && _writer != null)
{
@@ -418,7 +422,7 @@ namespace Newtonsoft.Json
private void WriteEscapedString(string value, bool quote)
{
EnsureWriteBuffer();
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, quote, _charEscapeFlags, StringEscapeHandling, _bufferPool, ref _writeBuffer);
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, quote, _charEscapeFlags, StringEscapeHandling, _arrayPool, ref _writeBuffer);
}
/// <summary>
@@ -754,7 +758,7 @@ namespace Newtonsoft.Json
if (_writeBuffer == null)
{
// maximum buffer sized used when writing iso date
_writeBuffer = BufferUtils.RentBuffer(_bufferPool, 35);
_writeBuffer = BufferUtils.RentBuffer(_arrayPool, 35);
}
}
+7 -7
View File
@@ -230,7 +230,7 @@ namespace Newtonsoft.Json
{
if (value < Formatting.None || value > Formatting.Indented)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_formatting = value;
@@ -247,7 +247,7 @@ namespace Newtonsoft.Json
{
if (value < DateFormatHandling.IsoDateFormat || value > DateFormatHandling.MicrosoftDateFormat)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_dateFormatHandling = value;
@@ -264,7 +264,7 @@ namespace Newtonsoft.Json
{
if (value < DateTimeZoneHandling.Local || value > DateTimeZoneHandling.RoundtripKind)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_dateTimeZoneHandling = value;
@@ -281,7 +281,7 @@ namespace Newtonsoft.Json
{
if (value < StringEscapeHandling.Default || value > StringEscapeHandling.EscapeHtml)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_stringEscapeHandling = value;
@@ -306,7 +306,7 @@ namespace Newtonsoft.Json
{
if (value < FloatFormatHandling.String || value > FloatFormatHandling.DefaultValue)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_floatFormatHandling = value;
@@ -1632,7 +1632,7 @@ namespace Newtonsoft.Json
case JsonToken.PropertyName:
if (!(value is string))
{
throw new ArgumentException("A name is required when setting property name state.", "value");
throw new ArgumentException("A name is required when setting property name state.", nameof(value));
}
InternalWritePropertyName((string)value);
@@ -1663,7 +1663,7 @@ namespace Newtonsoft.Json
InternalWriteEnd(JsonContainerType.Constructor);
break;
default:
throw new ArgumentOutOfRangeException("token");
throw new ArgumentOutOfRangeException(nameof(token));
}
}
+8 -8
View File
@@ -382,7 +382,7 @@ namespace Newtonsoft.Json.Linq
if (index > children.Count)
{
throw new ArgumentOutOfRangeException("index", "Index must be within the bounds of the List.");
throw new ArgumentOutOfRangeException(nameof(index), "Index must be within the bounds of the List.");
}
CheckReentrancy();
@@ -431,11 +431,11 @@ namespace Newtonsoft.Json.Linq
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", "Index is less than 0.");
throw new ArgumentOutOfRangeException(nameof(index), "Index is less than 0.");
}
if (index >= children.Count)
{
throw new ArgumentOutOfRangeException("index", "Index is equal to or greater than Count.");
throw new ArgumentOutOfRangeException(nameof(index), "Index is equal to or greater than Count.");
}
CheckReentrancy();
@@ -496,11 +496,11 @@ namespace Newtonsoft.Json.Linq
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", "Index is less than 0.");
throw new ArgumentOutOfRangeException(nameof(index), "Index is less than 0.");
}
if (index >= children.Count)
{
throw new ArgumentOutOfRangeException("index", "Index is equal to or greater than Count.");
throw new ArgumentOutOfRangeException(nameof(index), "Index is equal to or greater than Count.");
}
JToken existing = children[index];
@@ -602,11 +602,11 @@ namespace Newtonsoft.Json.Linq
{
if (array == null)
{
throw new ArgumentNullException("array");
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException("arrayIndex", "arrayIndex is less than 0.");
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
}
if (arrayIndex >= array.Length && arrayIndex != 0)
{
@@ -1249,7 +1249,7 @@ namespace Newtonsoft.Json.Linq
}
break;
default:
throw new ArgumentOutOfRangeException("settings", "Unexpected merge array handling when merging JSON.");
throw new ArgumentOutOfRangeException(nameof(settings), "Unexpected merge array handling when merging JSON.");
}
}
}
+2 -2
View File
@@ -627,11 +627,11 @@ namespace Newtonsoft.Json.Linq
{
if (array == null)
{
throw new ArgumentNullException("array");
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException("arrayIndex", "arrayIndex is less than 0.");
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
}
if (arrayIndex >= array.Length && arrayIndex != 0)
{
@@ -77,7 +77,7 @@ namespace Newtonsoft.Json.Linq
{
if (key == null)
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(key));
}
if (_dictionary != null)
@@ -123,7 +123,7 @@ namespace Newtonsoft.Json.Linq
{
if (key == null)
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(key));
}
if (_dictionary != null)
@@ -179,7 +179,7 @@ namespace Newtonsoft.Json.Linq
{
if (key == null)
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(key));
}
if (_dictionary != null)
+5 -5
View File
@@ -456,7 +456,7 @@ namespace Newtonsoft.Json.Linq
{
if (value == null)
{
throw new ArgumentNullException("value");
throw new ArgumentNullException(nameof(value));
}
if (value is JProperty)
@@ -2411,7 +2411,7 @@ namespace Newtonsoft.Json.Linq
{
if (annotation == null)
{
throw new ArgumentNullException("annotation");
throw new ArgumentNullException(nameof(annotation));
}
if (_annotations == null)
@@ -2484,7 +2484,7 @@ namespace Newtonsoft.Json.Linq
{
if (type == null)
{
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
}
if (_annotations != null)
@@ -2568,7 +2568,7 @@ namespace Newtonsoft.Json.Linq
{
if (type == null)
{
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
}
if (_annotations == null)
@@ -2662,7 +2662,7 @@ namespace Newtonsoft.Json.Linq
{
if (type == null)
{
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
}
if (_annotations != null)
+2 -2
View File
@@ -21,7 +21,7 @@ namespace Newtonsoft.Json.Linq
{
if (value < CommentHandling.Ignore || value > CommentHandling.Load)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_commentHandling = value;
@@ -39,7 +39,7 @@ namespace Newtonsoft.Json.Linq
{
if (value < LineInfoHandling.Ignore || value > LineInfoHandling.Load)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_lineInfoHandling = value;
@@ -20,7 +20,7 @@ namespace Newtonsoft.Json.Linq
{
if (value < MergeArrayHandling.Concat || value > MergeArrayHandling.Merge)
{
throw new ArgumentOutOfRangeException("value");
throw new ArgumentOutOfRangeException(nameof(value));
}
_mergeArrayHandling = value;
@@ -103,7 +103,7 @@
<Compile Include="FloatParseHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="Formatting.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonDictionaryAttribute.cs" />
<Compile Include="JsonException.cs" />
@@ -116,7 +116,7 @@
<Compile Include="FloatParseHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="Formatting.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonDictionaryAttribute.cs" />
<Compile Include="JsonException.cs" />
@@ -113,7 +113,7 @@
<Compile Include="Converters\VersionConverter.cs" />
<Compile Include="FloatFormatHandling.cs" />
<Compile Include="FloatParseHandling.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="JsonDictionaryAttribute.cs" />
<Compile Include="JsonException.cs" />
<Compile Include="DateFormatHandling.cs" />
@@ -77,7 +77,7 @@
<Compile Include="FloatParseHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="Formatting.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="IJsonLineInfo.cs" />
<Compile Include="JsonArrayAttribute.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
@@ -82,7 +82,7 @@
<Compile Include="FloatParseHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="Formatting.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="IJsonLineInfo.cs" />
<Compile Include="JsonArrayAttribute.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
+1 -1
View File
@@ -81,7 +81,7 @@
<Compile Include="FloatFormatHandling.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="Formatting.cs" />
<Compile Include="IJsonBufferPool.cs" />
<Compile Include="IArrayPool.cs" />
<Compile Include="IJsonLineInfo.cs" />
<Compile Include="JsonArrayAttribute.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
@@ -236,7 +236,7 @@ namespace Newtonsoft.Json.Serialization
{
if (type == null)
{
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
}
DefaultContractResolverState state = GetState();
@@ -33,7 +33,7 @@ namespace Newtonsoft.Json.Serialization
case TraceLevel.Verbose:
return TraceEventType.Verbose;
default:
throw new ArgumentOutOfRangeException("level");
throw new ArgumentOutOfRangeException(nameof(level));
}
}
@@ -63,7 +63,7 @@ namespace Newtonsoft.Json.Serialization
JToken token = value as JToken;
if (token == null)
{
throw new ArgumentException("Value is not a JToken.", "value");
throw new ArgumentException("Value is not a JToken.", nameof(value));
}
return _reader.CreateISerializableItem(token, type, _contract, _member);
@@ -140,7 +140,7 @@ namespace Newtonsoft.Json.Serialization
{
if (reader == null)
{
throw new ArgumentNullException("reader");
throw new ArgumentNullException(nameof(reader));
}
JsonContract contract = GetContractSafe(objectType);
@@ -61,7 +61,7 @@ namespace Newtonsoft.Json.Serialization
{
if (jsonWriter == null)
{
throw new ArgumentNullException("jsonWriter");
throw new ArgumentNullException(nameof(jsonWriter));
}
_rootType = objectType;
@@ -49,22 +49,22 @@ namespace Newtonsoft.Json.Utilities
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
throw new ArgumentNullException(nameof(buffer));
}
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
throw new ArgumentOutOfRangeException(nameof(index));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count");
throw new ArgumentOutOfRangeException(nameof(count));
}
if (count > (buffer.Length - index))
{
throw new ArgumentOutOfRangeException("count");
throw new ArgumentOutOfRangeException(nameof(count));
}
if (_leftOverBytesCount > 0)
@@ -66,7 +66,7 @@ namespace Newtonsoft.Json.Utilities
{
if (initial == null)
{
throw new ArgumentNullException("initial");
throw new ArgumentNullException(nameof(initial));
}
if (collection == null)
@@ -170,7 +170,7 @@ namespace Newtonsoft.Json.Utilities
if (source == null)
{
throw new ArgumentNullException("source");
throw new ArgumentNullException(nameof(source));
}
foreach (TSource local in source)
@@ -316,7 +316,7 @@ namespace Newtonsoft.Json.Utilities
{
if (!IsCompatibleObject(value))
{
throw new ArgumentException("The value '{0}' is not of type '{1}' and cannot be used in this generic collection.".FormatWith(CultureInfo.InvariantCulture, value, typeof(T)), "value");
throw new ArgumentException("The value '{0}' is not of type '{1}' and cannot be used in this generic collection.".FormatWith(CultureInfo.InvariantCulture, value, typeof(T)), nameof(value));
}
}
@@ -407,7 +407,7 @@ namespace Newtonsoft.Json.Utilities
case ConvertResult.CannotConvertNull:
throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));
case ConvertResult.NotInstantiableType:
throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), nameof(targetType));
case ConvertResult.NoValidConversion:
throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));
default:
@@ -438,7 +438,7 @@ namespace Newtonsoft.Json.Utilities
{
if (initialValue == null)
{
throw new ArgumentNullException("initialValue");
throw new ArgumentNullException(nameof(initialValue));
}
if (ReflectionUtils.IsNullableType(targetType))
+1 -1
View File
@@ -121,7 +121,7 @@ namespace Newtonsoft.Json.Utilities
{
if (enumType == null)
{
throw new ArgumentNullException("enumType");
throw new ArgumentNullException(nameof(enumType));
}
ValidationUtils.ArgumentTypeIsEnum(enumType, "enumType");
@@ -41,42 +41,40 @@ namespace Newtonsoft.Json.Utilities
{
internal static class BufferUtils
{
public static char[] RentBuffer(IJsonBufferPool<char> bufferPool, int minSize)
public static char[] RentBuffer(IArrayPool<char> bufferPool, int minSize)
{
if (bufferPool == null)
{
return new char[minSize];
}
char[] buffer = bufferPool.RentBuffer(minSize);
char[] buffer = bufferPool.Rent(minSize);
return buffer;
}
public static void ReturnBuffer(IJsonBufferPool<char> bufferPool, ref char[] buffer)
public static void ReturnBuffer(IArrayPool<char> bufferPool, char[] buffer)
{
if (bufferPool == null)
{
buffer = null;
return;
}
bufferPool.ReturnBuffer(ref buffer);
bufferPool.Return(buffer);
}
public static void EnsureBufferSize(IJsonBufferPool<char> bufferPool, int size, ref char[] buffer)
public static char[] EnsureBufferSize(IArrayPool<char> bufferPool, int size, char[] buffer)
{
if (bufferPool == null)
{
buffer = new char[size];
return;
return new char[size];
}
if (buffer != null)
{
bufferPool.ReturnBuffer(ref buffer);
bufferPool.Return(buffer);
}
buffer = bufferPool.RentBuffer(size);
return bufferPool.Rent(size);
}
}
@@ -149,7 +147,7 @@ namespace Newtonsoft.Json.Utilities
}
public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IJsonBufferPool<char> bufferPool, ref char[] writeBuffer)
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool<char> bufferPool, ref char[] writeBuffer)
{
// leading delimiter
if (appendDelimiters)
@@ -216,7 +214,7 @@ namespace Newtonsoft.Json.Utilities
{
if (writeBuffer == null || writeBuffer.Length < UnicodeTextLength)
{
BufferUtils.EnsureBufferSize(bufferPool, UnicodeTextLength, ref writeBuffer);
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, UnicodeTextLength, writeBuffer);
}
StringUtils.ToCharAsUnicode(c, writeBuffer);
@@ -255,7 +253,7 @@ namespace Newtonsoft.Json.Utilities
Array.Copy(writeBuffer, newBuffer, UnicodeTextLength);
}
BufferUtils.ReturnBuffer(bufferPool, ref writeBuffer);
BufferUtils.ReturnBuffer(bufferPool, writeBuffer);
writeBuffer = newBuffer;
}
@@ -78,7 +78,7 @@ namespace Newtonsoft.Json.Utilities
{
if (key == null)
{
throw new ArgumentNullException("key");
throw new ArgumentNullException(nameof(key));
}
int length = key.Length;
@@ -458,7 +458,7 @@ namespace Newtonsoft.Json.Utilities
case MemberTypes.Method:
return ((MethodInfo)member).ReturnType;
default:
throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo, EventInfo or MethodInfo", "member");
throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo, EventInfo or MethodInfo", nameof(member));
}
}
@@ -524,7 +524,7 @@ namespace Newtonsoft.Json.Utilities
throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(CultureInfo.InvariantCulture, member.Name), e);
}
default:
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, CultureInfo.InvariantCulture, member.Name), "member");
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, CultureInfo.InvariantCulture, member.Name), nameof(member));
}
}
@@ -548,7 +548,7 @@ namespace Newtonsoft.Json.Utilities
((PropertyInfo)member).SetValue(target, value, null);
break;
default:
throw new ArgumentException("MemberInfo '{0}' must be of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, member.Name), "member");
throw new ArgumentException("MemberInfo '{0}' must be of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, member.Name), nameof(member));
}
}
@@ -46,7 +46,7 @@ namespace Newtonsoft.Json.Utilities
get { return _buffer == null; }
}
public StringBuffer(IJsonBufferPool<char> bufferPool, int initalSize) : this(BufferUtils.RentBuffer(bufferPool, initalSize))
public StringBuffer(IArrayPool<char> bufferPool, int initalSize) : this(BufferUtils.RentBuffer(bufferPool, initalSize))
{
}
@@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Utilities
_position = 0;
}
public void Append(IJsonBufferPool<char> bufferPool, char value)
public void Append(IArrayPool<char> bufferPool, char value)
{
// test if the buffer array is large enough to take the value
if (_position == _buffer.Length)
@@ -68,7 +68,7 @@ namespace Newtonsoft.Json.Utilities
_buffer[_position++] = value;
}
public void Append(IJsonBufferPool<char> bufferPool, char[] buffer, int startIndex, int count)
public void Append(IArrayPool<char> bufferPool, char[] buffer, int startIndex, int count)
{
if (_position + count >= _buffer.Length)
{
@@ -80,24 +80,24 @@ namespace Newtonsoft.Json.Utilities
_position += count;
}
public void Clear(IJsonBufferPool<char> bufferPool)
public void Clear(IArrayPool<char> bufferPool)
{
if (_buffer != null)
{
BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
BufferUtils.ReturnBuffer(bufferPool, _buffer);
_buffer = null;
}
_position = 0;
}
private void EnsureSize(IJsonBufferPool<char> bufferPool, int appendLength)
private void EnsureSize(IArrayPool<char> bufferPool, int appendLength)
{
char[] newBuffer = BufferUtils.RentBuffer(bufferPool, (_position + appendLength) * 2);
if (_buffer != null)
{
Array.Copy(_buffer, newBuffer, _position);
BufferUtils.ReturnBuffer(bufferPool, ref _buffer);
BufferUtils.ReturnBuffer(bufferPool, _buffer);
}
_buffer = newBuffer;
+3 -3
View File
@@ -85,7 +85,7 @@ namespace Newtonsoft.Json.Utilities
{
if (s == null)
{
throw new ArgumentNullException("s");
throw new ArgumentNullException(nameof(s));
}
if (s.Length == 0)
@@ -148,11 +148,11 @@ namespace Newtonsoft.Json.Utilities
{
if (source == null)
{
throw new ArgumentNullException("source");
throw new ArgumentNullException(nameof(source));
}
if (valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
throw new ArgumentNullException(nameof(valueSelector));
}
var caseInsensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase));
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Utilities
{
if (creator == null)
{
throw new ArgumentNullException("creator");
throw new ArgumentNullException(nameof(creator));
}
_creator = creator;