diff --git a/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs index 45d522e1..4ca6a612 100644 --- a/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs +++ b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs @@ -66,7 +66,7 @@ namespace Newtonsoft.Json.Converters private object ReadValue(JsonReader reader) { - if (!reader.MoveToContentUnsafe()) + if (!reader.MoveToContent()) { throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject."); } diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 6fd4f827..e15a6496 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -457,7 +457,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsDateTimeOffset; - JsonToken t = ReadToContentInternal(); + JsonToken t = GetContentTokenInternal(); switch (t) { @@ -504,7 +504,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsBytes; - JsonToken t = ReadToContentInternal(); + JsonToken t = GetContentTokenInternal(); if (t == JsonToken.None) { @@ -563,7 +563,7 @@ namespace Newtonsoft.Json while (true) { - switch (t = ReadToContentInternal()) + switch (t = GetContentTokenInternal()) { case JsonToken.None: throw JsonReaderException.Create(this, "Unexpected end when reading bytes."); @@ -586,7 +586,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsDecimal; - JsonToken t = ReadToContentInternal(); + JsonToken t = GetContentTokenInternal(); switch (t) { @@ -628,7 +628,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsInt32; - switch (ReadToContentInternal()) + switch (GetContentTokenInternal()) { case JsonToken.None: case JsonToken.Null: @@ -668,7 +668,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsString; - JsonToken t = ReadToContentInternal(); + JsonToken t = GetContentTokenInternal(); switch (t) { @@ -706,7 +706,7 @@ namespace Newtonsoft.Json { _readType = ReadType.ReadAsDateTime; - switch (ReadToContentInternal()) + switch (GetContentTokenInternal()) { case JsonToken.None: case JsonToken.Null: @@ -996,44 +996,28 @@ namespace Newtonsoft.Json } } - /// - /// Reads the next non-comment JSON token from the stream. - /// - /// true if the next non-comment token was read successfully; false if there are no more tokens to read. - public bool ReadToContent() + internal bool ReadAndMoveToContent() { - return Read() && MoveToContentUnsafe(); + return Read() && MoveToContent(); } - /// - /// Checks whether the current JSON token is a content (non-comment) token, reading past comments if necessary. - /// - /// 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. - public bool MoveToContent() + internal bool MoveToContent() { - if (TokenType == JsonToken.None && !Read()) - { - return false; - } - - return MoveToContentUnsafe(); - } - - // MoveToContent() but assumes stream was already read into. - internal bool MoveToContentUnsafe() - { - while (TokenType == JsonToken.Comment) + JsonToken t = TokenType; + while (t == JsonToken.None || t == JsonToken.Comment) { if (!Read()) { return false; } + + t = TokenType; } return true; } - private JsonToken ReadToContentInternal() + private JsonToken GetContentTokenInternal() { JsonToken t; do diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index ad924eae..da10242d 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -132,7 +132,7 @@ namespace Newtonsoft.Json.Linq } } - reader.MoveToContentUnsafe(); + reader.MoveToContent(); if (reader.TokenType != JsonToken.StartArray) { diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index 3a44a20c..ed5f362f 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -221,7 +221,7 @@ namespace Newtonsoft.Json.Linq } } - reader.MoveToContentUnsafe(); + reader.MoveToContent(); if (reader.TokenType != JsonToken.StartConstructor) { diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index c5c1a9d2..754c0919 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -370,7 +370,7 @@ namespace Newtonsoft.Json.Linq } } - reader.MoveToContentUnsafe(); + reader.MoveToContent(); if (reader.TokenType != JsonToken.StartObject) { diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index 6501681e..259fe430 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -382,7 +382,7 @@ namespace Newtonsoft.Json.Linq } } - reader.MoveToContentUnsafe(); + reader.MoveToContent(); if (reader.TokenType != JsonToken.PropertyName) { diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 6918b2bd..55b31696 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -2113,7 +2113,11 @@ namespace Newtonsoft.Json.Linq 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."); } diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 1b21efbb..6dbc9c91 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -246,7 +246,7 @@ namespace Newtonsoft.Json.Serialization if (reader.TokenType == JsonToken.PropertyName) { string propertyName = (string)reader.Value; - if (!reader.ReadToContent()) + if (!reader.ReadAndMoveToContent()) { 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 if (hasConverter) { - return reader.ReadToContent(); + return reader.ReadAndMoveToContent(); } ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read; @@ -2203,7 +2203,7 @@ namespace Newtonsoft.Json.Serialization switch (t) { case ReadType.Read: - return reader.ReadToContent(); + return reader.ReadAndMoveToContent(); case ReadType.ReadAsInt32: reader.ReadAsInt32(); break;