-Fixed DateTimeZoneHandling not being used when writing DateTime dictionary keys

-Buffer pooling fixes
-Improved date parsing performance
This commit is contained in:
James Newton-King
2015-11-14 15:22:59 +13:00
parent f3822513df
commit a3eb2d596c
16 changed files with 689 additions and 227 deletions
+126 -11
View File
@@ -54,6 +54,7 @@ namespace Newtonsoft.Json
{
private const char UnicodeReplacementChar = '\uFFFD';
private const int MaximumJavascriptIntegerCharacterLength = 380;
private readonly TextReader _reader;
private char[] _chars;
private int _charsUsed;
@@ -73,7 +74,9 @@ namespace Newtonsoft.Json
public JsonTextReader(TextReader reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
_reader = reader;
_lineNumber = 1;
@@ -107,7 +110,7 @@ namespace Newtonsoft.Json
{
if (_stringBuffer.IsEmpty)
{
_stringBuffer = new StringBuffer(_bufferPool, 1025);
_stringBuffer = new StringBuffer(_bufferPool, 1024);
}
}
@@ -153,33 +156,52 @@ namespace Newtonsoft.Json
}
else
{
string text = _stringReference.ToString();
if (_dateParseHandling != DateParseHandling.None)
{
DateParseHandling dateParseHandling;
if (_readType == ReadType.ReadAsDateTime)
{
dateParseHandling = DateParseHandling.DateTime;
}
#if !NET20
else if (_readType == ReadType.ReadAsDateTimeOffset)
{
dateParseHandling = DateParseHandling.DateTimeOffset;
}
#endif
else
dateParseHandling = _dateParseHandling;
object dt;
if (DateTimeUtils.TryParseDateTime(text, dateParseHandling, DateTimeZoneHandling, DateFormatString, Culture, out dt))
{
SetToken(JsonToken.Date, dt, false);
return;
dateParseHandling = _dateParseHandling;
}
if (dateParseHandling == DateParseHandling.DateTime)
{
DateTime dt;
if (DateTimeUtils.TryParseDateTime(_stringReference, DateTimeZoneHandling, DateFormatString, Culture, out dt))
{
SetToken(JsonToken.Date, dt, false);
return;
}
}
#if !NET20
else
{
DateTimeOffset dt;
if (DateTimeUtils.TryParseDateTimeOffset(_stringReference, DateFormatString, Culture, out dt))
{
SetToken(JsonToken.Date, dt, false);
return;
}
}
#endif
}
SetToken(JsonToken.String, text, false);
SetToken(JsonToken.String, _stringReference.ToString(), false);
_quoteChar = quote;
}
}
private static void BlockCopyChars(char[] src, int srcOffset, char[] dst, int dstOffset, int count)
{
const int charByteCount = 2;
@@ -196,7 +218,9 @@ namespace Newtonsoft.Json
{
int count = _charsUsed - _charPos;
if (count > 0)
{
BlockCopyChars(_chars, _charPos, _chars, 0, count);
}
_lineStartPos -= _charPos;
_charPos = 0;
@@ -213,7 +237,9 @@ namespace Newtonsoft.Json
private int ReadData(bool append, int charsRequired)
{
if (_isEndOfFile)
{
return 0;
}
// char buffer is full
if (_charsUsed + charsRequired >= _chars.Length - 1)
@@ -242,7 +268,9 @@ namespace Newtonsoft.Json
char[] dst = BufferUtils.RentBuffer(_bufferPool, remainingCharCount + charsRequired + 1);
if (remainingCharCount > 0)
{
BlockCopyChars(_chars, _charPos, dst, 0, remainingCharCount);
}
BufferUtils.ReturnBuffer(_bufferPool, ref _chars);
@@ -252,7 +280,9 @@ namespace Newtonsoft.Json
{
// copy any remaining data to the beginning of the buffer if needed and reset positions
if (remainingCharCount > 0)
{
BlockCopyChars(_chars, _charPos, _chars, 0, remainingCharCount);
}
}
_lineStartPos -= _charPos;
@@ -268,7 +298,9 @@ namespace Newtonsoft.Json
_charsUsed += charsRead;
if (charsRead == 0)
{
_isEndOfFile = true;
}
_chars[_charsUsed] = '\0';
return charsRead;
@@ -277,7 +309,9 @@ namespace Newtonsoft.Json
private bool EnsureChars(int relativePosition, bool append)
{
if (_charPos + relativePosition >= _charsUsed)
{
return ReadChars(relativePosition, append);
}
return true;
}
@@ -285,7 +319,9 @@ namespace Newtonsoft.Json
private bool ReadChars(int relativePosition, bool append)
{
if (_isEndOfFile)
{
return false;
}
int charsRequired = _charPos + relativePosition - _charsUsed + 1;
@@ -299,13 +335,17 @@ namespace Newtonsoft.Json
// no more content
if (charsRead == 0)
{
break;
}
totalCharsRead += charsRead;
} while (totalCharsRead < charsRequired);
if (totalCharsRead < charsRequired)
{
return false;
}
return true;
}
@@ -369,7 +409,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Reads the next JSON token from the stream as a <see cref="Nullable{DateTime}"/>.
/// </summary>
/// <returns>A <see cref="String"/>. This method will return <c>null</c> at the end of an array.</returns>
/// <returns>A <see cref="Nullable{DateTime}"/>. This method will return <c>null</c> at the end of an array.</returns>
public override DateTime? ReadAsDateTime()
{
return ReadAsDateTimeInternal();
@@ -391,6 +431,7 @@ namespace Newtonsoft.Json
if (_chars == null)
{
_chars = BufferUtils.RentBuffer(_bufferPool, 1024);
_chars[0] = '\0';
}
while (true)
@@ -658,7 +699,9 @@ namespace Newtonsoft.Json
if (_charsUsed == charPos)
{
if (ReadData(true) == 0)
{
return;
}
}
else
{
@@ -800,7 +843,9 @@ namespace Newtonsoft.Json
if (_charsUsed == _charPos)
{
if (ReadData(false) == 0)
{
return false;
}
}
else
{
@@ -871,7 +916,9 @@ namespace Newtonsoft.Json
// no match in name table
if (propertyName == null)
{
propertyName = _stringReference.ToString();
}
}
else
{
@@ -881,7 +928,9 @@ namespace Newtonsoft.Json
EatWhitespace(false);
if (_chars[_charPos] != ':')
{
throw JsonReaderException.Create(this, "Invalid character after parsing property name. Expected ':' but got: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
}
_charPos++;
@@ -971,11 +1020,17 @@ namespace Newtonsoft.Json
char next = _chars[_charPos + 1];
if (next == 'u')
{
ParseNull();
}
else if (next == 'e')
{
ParseConstructor();
}
else
{
throw JsonReaderException.Create(this, "Unexpected character encountered while parsing value: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
}
}
else
{
@@ -990,9 +1045,13 @@ namespace Newtonsoft.Json
return true;
case '-':
if (EnsureChars(1, true) && _chars[_charPos + 1] == 'I')
{
ParseNumberNegativeInfinity();
}
else
{
ParseNumber();
}
return true;
case '/':
ParseComment();
@@ -1061,7 +1120,9 @@ namespace Newtonsoft.Json
_charPos++;
if (EnsureChars(1, append) && _chars[_charPos] == StringUtils.LineFeed)
{
_charPos++;
}
OnNewLine(_charPos);
}
@@ -1080,7 +1141,9 @@ namespace Newtonsoft.Json
if (_charsUsed == _charPos)
{
if (ReadData(false) == 0)
{
finished = true;
}
}
else
{
@@ -1127,7 +1190,9 @@ namespace Newtonsoft.Json
if (_charsUsed == _charPos)
{
if (ReadData(true) == 0)
{
throw JsonReaderException.Create(this, "Unexpected end while parsing constructor.");
}
}
else
{
@@ -1175,7 +1240,9 @@ namespace Newtonsoft.Json
EatWhitespace(false);
if (_chars[_charPos] != '(')
{
throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
}
_charPos++;
@@ -1241,11 +1308,17 @@ namespace Newtonsoft.Json
int value;
ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
if (parseResult == ParseResult.Success)
{
numberValue = value;
}
else if (parseResult == ParseResult.Overflow)
{
throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
else
{
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
}
numberType = JsonToken.Integer;
@@ -1281,9 +1354,13 @@ namespace Newtonsoft.Json
decimal value;
if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out value))
{
numberValue = value;
}
else
{
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
}
numberType = JsonToken.Float;
@@ -1328,7 +1405,9 @@ namespace Newtonsoft.Json
string number = _stringReference.ToString();
if (number.Length > MaximumJavascriptIntegerCharacterLength)
{
throw JsonReaderException.Create(this, "JSON integer {0} is too large to parse.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
numberValue = BigIntegerParse(number, CultureInfo.InvariantCulture);
numberType = JsonToken.Integer;
@@ -1344,17 +1423,25 @@ namespace Newtonsoft.Json
{
decimal d;
if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d))
{
numberValue = d;
}
else
{
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number));
}
}
else
{
double d;
if (double.TryParse(number, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out d))
{
numberValue = d;
}
else
{
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number));
}
}
numberType = JsonToken.Float;
@@ -1391,11 +1478,17 @@ namespace Newtonsoft.Json
bool singlelineComment;
if (_chars[_charPos] == '*')
{
singlelineComment = false;
}
else if (_chars[_charPos] == '/')
{
singlelineComment = true;
}
else
{
throw JsonReaderException.Create(this, "Error parsing comment. Expected: *, got {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
}
_charPos++;
@@ -1413,7 +1506,9 @@ namespace Newtonsoft.Json
if (ReadData(true) == 0)
{
if (!singlelineComment)
{
throw JsonReaderException.Create(this, "Unexpected end while parsing comment.");
}
_stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);
commentFinished = true;
@@ -1471,7 +1566,9 @@ namespace Newtonsoft.Json
private bool MatchValue(string value)
{
if (!EnsureChars(value.Length - 1, true))
{
return false;
}
for (int i = 0; i < value.Length; i++)
{
@@ -1492,10 +1589,14 @@ namespace Newtonsoft.Json
bool match = MatchValue(value);
if (!match)
{
return false;
}
if (!EnsureChars(0, false))
{
return true;
}
return IsSeparator(_chars[_charPos]) || _chars[_charPos] == '\0';
}
@@ -1511,14 +1612,18 @@ namespace Newtonsoft.Json
case '/':
// check next character to see if start of a comment
if (!EnsureChars(1, false))
{
return false;
}
var nextChart = _chars[_charPos + 1];
return (nextChart == '*' || nextChart == '/');
case ')':
if (CurrentState == State.Constructor || CurrentState == State.ConstructorStart)
{
return true;
}
break;
case ' ':
case StringUtils.Tab:
@@ -1527,7 +1632,9 @@ namespace Newtonsoft.Json
return true;
default:
if (char.IsWhiteSpace(c))
{
return true;
}
break;
}
@@ -1590,7 +1697,9 @@ namespace Newtonsoft.Json
if (MatchValueWithTrailingSeparator(JsonConvert.NegativeInfinity))
{
if (_floatParseHandling == FloatParseHandling.Decimal)
{
throw new JsonReaderException("Cannot read -Infinity as a decimal.");
}
SetToken(JsonToken.Float, double.NegativeInfinity);
}
@@ -1605,7 +1714,9 @@ namespace Newtonsoft.Json
if (MatchValueWithTrailingSeparator(JsonConvert.PositiveInfinity))
{
if (_floatParseHandling == FloatParseHandling.Decimal)
{
throw new JsonReaderException("Cannot read Infinity as a decimal.");
}
SetToken(JsonToken.Float, double.PositiveInfinity);
}
@@ -1620,7 +1731,9 @@ namespace Newtonsoft.Json
if (MatchValueWithTrailingSeparator(JsonConvert.NaN))
{
if (_floatParseHandling == FloatParseHandling.Decimal)
{
throw new JsonReaderException("Cannot read NaN as a decimal.");
}
SetToken(JsonToken.Float, double.NaN);
}
@@ -1676,7 +1789,9 @@ namespace Newtonsoft.Json
get
{
if (CurrentState == State.Start && LinePosition == 0 && TokenType != JsonToken.Comment)
{
return 0;
}
return _lineNumber;
}