-Refactor

This commit is contained in:
James Newton-King
2015-12-20 18:07:25 +13:00
parent 4304ea6431
commit a7046b0f6c
8 changed files with 28 additions and 40 deletions
@@ -66,7 +66,7 @@ namespace Newtonsoft.Json.Converters
private object ReadValue(JsonReader reader) private object ReadValue(JsonReader reader)
{ {
if (!reader.MoveToContentUnsafe()) if (!reader.MoveToContent())
{ {
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject."); throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
} }
+15 -31
View File
@@ -457,7 +457,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsDateTimeOffset; _readType = ReadType.ReadAsDateTimeOffset;
JsonToken t = ReadToContentInternal(); JsonToken t = GetContentTokenInternal();
switch (t) switch (t)
{ {
@@ -504,7 +504,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsBytes; _readType = ReadType.ReadAsBytes;
JsonToken t = ReadToContentInternal(); JsonToken t = GetContentTokenInternal();
if (t == JsonToken.None) if (t == JsonToken.None)
{ {
@@ -563,7 +563,7 @@ namespace Newtonsoft.Json
while (true) while (true)
{ {
switch (t = ReadToContentInternal()) switch (t = GetContentTokenInternal())
{ {
case JsonToken.None: case JsonToken.None:
throw JsonReaderException.Create(this, "Unexpected end when reading bytes."); throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
@@ -586,7 +586,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsDecimal; _readType = ReadType.ReadAsDecimal;
JsonToken t = ReadToContentInternal(); JsonToken t = GetContentTokenInternal();
switch (t) switch (t)
{ {
@@ -628,7 +628,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsInt32; _readType = ReadType.ReadAsInt32;
switch (ReadToContentInternal()) switch (GetContentTokenInternal())
{ {
case JsonToken.None: case JsonToken.None:
case JsonToken.Null: case JsonToken.Null:
@@ -668,7 +668,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsString; _readType = ReadType.ReadAsString;
JsonToken t = ReadToContentInternal(); JsonToken t = GetContentTokenInternal();
switch (t) switch (t)
{ {
@@ -706,7 +706,7 @@ namespace Newtonsoft.Json
{ {
_readType = ReadType.ReadAsDateTime; _readType = ReadType.ReadAsDateTime;
switch (ReadToContentInternal()) switch (GetContentTokenInternal())
{ {
case JsonToken.None: case JsonToken.None:
case JsonToken.Null: case JsonToken.Null:
@@ -996,44 +996,28 @@ namespace Newtonsoft.Json
} }
} }
/// <summary> internal bool ReadAndMoveToContent()
/// Reads the next non-comment JSON token from the stream.
/// </summary>
/// <returns>true if the next non-comment token was read successfully; false if there are no more tokens to read.</returns>
public bool ReadToContent()
{ {
return Read() && MoveToContentUnsafe(); return Read() && MoveToContent();
} }
/// <summary> internal bool MoveToContent()
/// Checks whether the current JSON token is a content (non-comment) token, reading past comments if necessary.
/// </summary>
/// <returns>true if the current token was not a comment, or the reader successfully read to a non-comment token; false if there are no more tokens to read.</returns>
public bool MoveToContent()
{ {
if (TokenType == JsonToken.None && !Read()) JsonToken t = TokenType;
{ while (t == JsonToken.None || t == JsonToken.Comment)
return false;
}
return MoveToContentUnsafe();
}
// MoveToContent() but assumes stream was already read into.
internal bool MoveToContentUnsafe()
{
while (TokenType == JsonToken.Comment)
{ {
if (!Read()) if (!Read())
{ {
return false; return false;
} }
t = TokenType;
} }
return true; return true;
} }
private JsonToken ReadToContentInternal() private JsonToken GetContentTokenInternal()
{ {
JsonToken t; JsonToken t;
do do
+1 -1
View File
@@ -132,7 +132,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
reader.MoveToContentUnsafe(); reader.MoveToContent();
if (reader.TokenType != JsonToken.StartArray) if (reader.TokenType != JsonToken.StartArray)
{ {
+1 -1
View File
@@ -221,7 +221,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
reader.MoveToContentUnsafe(); reader.MoveToContent();
if (reader.TokenType != JsonToken.StartConstructor) if (reader.TokenType != JsonToken.StartConstructor)
{ {
+1 -1
View File
@@ -370,7 +370,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
reader.MoveToContentUnsafe(); reader.MoveToContent();
if (reader.TokenType != JsonToken.StartObject) if (reader.TokenType != JsonToken.StartObject)
{ {
+1 -1
View File
@@ -382,7 +382,7 @@ namespace Newtonsoft.Json.Linq
} }
} }
reader.MoveToContentUnsafe(); reader.MoveToContent();
if (reader.TokenType != JsonToken.PropertyName) if (reader.TokenType != JsonToken.PropertyName)
{ {
+5 -1
View File
@@ -2113,7 +2113,11 @@ namespace Newtonsoft.Json.Linq
if (reader.TokenType == JsonToken.None) if (reader.TokenType == JsonToken.None)
{ {
if (!(settings != null && settings.CommentHandling == CommentHandling.Ignore ? reader.ReadToContent() : reader.Read())) bool hasContent = (settings != null && settings.CommentHandling == CommentHandling.Ignore)
? reader.ReadAndMoveToContent()
: reader.Read();
if (!hasContent)
{ {
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader."); throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
} }
@@ -246,7 +246,7 @@ namespace Newtonsoft.Json.Serialization
if (reader.TokenType == JsonToken.PropertyName) if (reader.TokenType == JsonToken.PropertyName)
{ {
string propertyName = (string)reader.Value; string propertyName = (string)reader.Value;
if (!reader.ReadToContent()) if (!reader.ReadAndMoveToContent())
{ {
break; break;
} }
@@ -2195,7 +2195,7 @@ namespace Newtonsoft.Json.Serialization
// the value might be a string which will then get converted which will error if read as date for example // the value might be a string which will then get converted which will error if read as date for example
if (hasConverter) if (hasConverter)
{ {
return reader.ReadToContent(); return reader.ReadAndMoveToContent();
} }
ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read; ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read;
@@ -2203,7 +2203,7 @@ namespace Newtonsoft.Json.Serialization
switch (t) switch (t)
{ {
case ReadType.Read: case ReadType.Read:
return reader.ReadToContent(); return reader.ReadAndMoveToContent();
case ReadType.ReadAsInt32: case ReadType.ReadAsInt32:
reader.ReadAsInt32(); reader.ReadAsInt32();
break; break;