Refactor some cases of repeated code into separate methods.

Make the MoveToContent() and ReadToContent() produced from this public.
This commit is contained in:
Jon Hanna
2015-12-20 00:28:40 +00:00
parent dca5260334
commit a06716c6a6
18 changed files with 351 additions and 503 deletions
@@ -264,7 +264,7 @@ namespace Newtonsoft.Json.Tests.Converters
[Test]
public void DeserializeBasicUnion_UnexpectedEnd()
{
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":"), "Unexpected end when reading union. Path 'Case', line 1, position 8.");
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Currency>(@"{""Case"":"), "Unexpected end when reading JSON. Path 'Case', line 1, position 8.");
}
[Test]
@@ -158,7 +158,7 @@ namespace Newtonsoft.Json.Tests.Converters
ExceptionAssert.Throws<JsonException>(() =>
{
converter.ReadJson(writer, typeof (JsonValue), null, null);
}, "Unexpected end. Path '', line 1, position 1.");
}, "Unexpected end when reading JSON. Path '', line 1, position 1.");
}
[Test]
@@ -173,7 +173,7 @@ namespace Newtonsoft.Json.Tests.Converters
ExceptionAssert.Throws<JsonException>(() =>
{
converter.ReadJson(writer, typeof (JsonValue), null, null);
}, "Unexpected end. Path '', line 1, position 13.");
}, "Unexpected end when reading JSON. Path '', line 1, position 13.");
}
[Test]
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Tests.Converters
ExceptionAssert.Throws<JsonException>(() =>
{
converter.ReadJson(writer, typeof (JsonValue), null, null);
}, "Unexpected end. Path 'hi', line 1, position 6.");
}, "Unexpected end when reading JSON. Path 'hi', line 1, position 6.");
}
[Test]
@@ -60,7 +60,7 @@ namespace Newtonsoft.Json.Tests.Converters
[Test]
public void DeserializeUnexpectedEnd()
{
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<KeyValuePair<string, int>>(@"{""Key"": ""123"","), "Unexpected end when reading KeyValuePair. Path 'Key', line 1, position 14.");
ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<KeyValuePair<string, int>>(@"{""Key"": ""123"","), "Unexpected end when reading JSON. Path 'Key', line 1, position 14.");
}
}
}
@@ -479,7 +479,7 @@ namespace Newtonsoft.Json.Tests.Serialization
PreserveReferencesHandling = PreserveReferencesHandling.All,
MetadataPropertyHandling = MetadataPropertyHandling.Default
});
}, @"Unexpected end when deserializing object. Path '$id', line 2, position 8.");
}, @"Unexpected end when reading JSON. Path '$id', line 2, position 8.");
}
public class CircularReferenceClassConverter : JsonConverter
@@ -82,7 +82,7 @@ namespace Newtonsoft.Json.Converters
DataTableConverter converter = new DataTableConverter();
CheckedRead(reader);
reader.ReadAndAssert();
while (reader.TokenType == JsonToken.PropertyName)
{
@@ -96,7 +96,7 @@ namespace Newtonsoft.Json.Converters
ds.Tables.Add(dt);
}
CheckedRead(reader);
reader.ReadAndAssert();
}
return ds;
@@ -113,14 +113,6 @@ namespace Newtonsoft.Json.Converters
{
return typeof(DataSet).IsAssignableFrom(valueType);
}
private void CheckedRead(JsonReader reader)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when reading DataSet.");
}
}
}
}
@@ -103,7 +103,7 @@ namespace Newtonsoft.Json.Converters
{
dt.TableName = (string)reader.Value;
CheckedRead(reader);
reader.ReadAndAssert();
if (reader.TokenType == JsonToken.Null)
{
@@ -116,13 +116,13 @@ namespace Newtonsoft.Json.Converters
throw JsonSerializationException.Create(reader, "Unexpected JSON token when reading DataTable. Expected StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
CheckedRead(reader);
reader.ReadAndAssert();
while (reader.TokenType != JsonToken.EndArray)
{
CreateRow(reader, dt, serializer);
CheckedRead(reader);
reader.ReadAndAssert();
}
return dt;
@@ -131,13 +131,13 @@ namespace Newtonsoft.Json.Converters
private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer serializer)
{
DataRow dr = dt.NewRow();
CheckedRead(reader);
reader.ReadAndAssert();
while (reader.TokenType == JsonToken.PropertyName)
{
string columnName = (string)reader.Value;
CheckedRead(reader);
reader.ReadAndAssert();
DataColumn column = dt.Columns[columnName];
if (column == null)
@@ -151,7 +151,7 @@ namespace Newtonsoft.Json.Converters
{
if (reader.TokenType == JsonToken.StartArray)
{
CheckedRead(reader);
reader.ReadAndAssert();
}
DataTable nestedDt = new DataTable();
@@ -160,7 +160,7 @@ namespace Newtonsoft.Json.Converters
{
CreateRow(reader, nestedDt, serializer);
CheckedRead(reader);
reader.ReadAndAssert();
}
dr[columnName] = nestedDt;
@@ -169,7 +169,7 @@ namespace Newtonsoft.Json.Converters
{
if (reader.TokenType == JsonToken.StartArray)
{
CheckedRead(reader);
reader.ReadAndAssert();
}
List<object> o = new List<object>();
@@ -177,7 +177,7 @@ namespace Newtonsoft.Json.Converters
while (reader.TokenType != JsonToken.EndArray)
{
o.Add(reader.Value);
CheckedRead(reader);
reader.ReadAndAssert();
}
Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count);
@@ -190,7 +190,7 @@ namespace Newtonsoft.Json.Converters
dr[columnName] = (reader.Value != null) ? serializer.Deserialize(reader, column.DataType) : DBNull.Value;
}
CheckedRead(reader);
reader.ReadAndAssert();
}
dr.EndEdit();
@@ -214,7 +214,7 @@ namespace Newtonsoft.Json.Converters
case JsonToken.Undefined:
return typeof(string);
case JsonToken.StartArray:
CheckedRead(reader);
reader.ReadAndAssert();
if (reader.TokenType == JsonToken.StartObject)
{
return typeof(DataTable); // nested datatable
@@ -227,14 +227,6 @@ namespace Newtonsoft.Json.Converters
}
}
private static void CheckedRead(JsonReader reader)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when reading DataTable.");
}
}
/// <summary>
/// Determines whether this instance can convert the specified value type.
/// </summary>
@@ -160,14 +160,14 @@ namespace Newtonsoft.Json.Converters
JArray fields = null;
// start object
ReadAndAssert(reader);
reader.ReadAndAssert();
while (reader.TokenType == JsonToken.PropertyName)
{
string propertyName = reader.Value.ToString();
if (string.Equals(propertyName, CasePropertyName, StringComparison.OrdinalIgnoreCase))
{
ReadAndAssert(reader);
reader.ReadAndAssert();
Union union = UnionCache.Get(objectType);
@@ -182,7 +182,7 @@ namespace Newtonsoft.Json.Converters
}
else if (string.Equals(propertyName, FieldsPropertyName, StringComparison.OrdinalIgnoreCase))
{
ReadAndAssert(reader);
reader.ReadAndAssert();
if (reader.TokenType != JsonToken.StartArray)
{
throw JsonSerializationException.Create(reader, "Union fields must been an array.");
@@ -195,7 +195,7 @@ namespace Newtonsoft.Json.Converters
throw JsonSerializationException.Create(reader, "Unexpected property '{0}' found when reading union.".FormatWith(CultureInfo.InvariantCulture, propertyName));
}
ReadAndAssert(reader);
reader.ReadAndAssert();
}
if (caseInfo == null)
@@ -274,14 +274,6 @@ namespace Newtonsoft.Json.Converters
return (bool)FSharpUtils.IsUnion(null, objectType, null);
}
private static void ReadAndAssert(JsonReader reader)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when reading union.");
}
}
}
}
@@ -91,7 +91,7 @@ namespace Newtonsoft.Json.Converters
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
{
ReadAndAssert(reader);
reader.ReadAndAssert();
if (reader.TokenType != JsonToken.PropertyName || !string.Equals(reader.Value.ToString(), propertyName, StringComparison.OrdinalIgnoreCase))
{
@@ -99,14 +99,6 @@ namespace Newtonsoft.Json.Converters
}
}
private static void ReadAndAssert(JsonReader reader)
{
if (!reader.Read())
{
throw new JsonSerializationException("Unexpected end.");
}
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
@@ -122,20 +114,20 @@ namespace Newtonsoft.Json.Converters
object entityKeyMember = _reflectionObject.Creator();
ReadAndAssertProperty(reader, KeyPropertyName);
ReadAndAssert(reader);
reader.ReadAndAssert();
_reflectionObject.SetValue(entityKeyMember, KeyPropertyName, reader.Value.ToString());
ReadAndAssertProperty(reader, TypePropertyName);
ReadAndAssert(reader);
reader.ReadAndAssert();
string type = reader.Value.ToString();
Type t = Type.GetType(type);
ReadAndAssertProperty(reader, ValuePropertyName);
ReadAndAssert(reader);
reader.ReadAndAssert();
_reflectionObject.SetValue(entityKeyMember, ValuePropertyName, serializer.Deserialize(reader, t));
ReadAndAssert(reader);
reader.ReadAndAssert();
return entityKeyMember;
}
@@ -66,12 +66,9 @@ namespace Newtonsoft.Json.Converters
private object ReadValue(JsonReader reader)
{
while (reader.TokenType == JsonToken.Comment)
if (!reader.MoveToContentUnsafe())
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
}
throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
}
switch (reader.TokenType)
@@ -93,7 +93,7 @@ namespace Newtonsoft.Json.Converters
object key = null;
object value = null;
ReadAndAssert(reader);
reader.ReadAndAssert();
Type t = ReflectionUtils.IsNullableType(objectType)
? Nullable.GetUnderlyingType(objectType)
@@ -106,12 +106,12 @@ namespace Newtonsoft.Json.Converters
string propertyName = reader.Value.ToString();
if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase))
{
ReadAndAssert(reader);
reader.ReadAndAssert();
key = serializer.Deserialize(reader, reflectionObject.GetType(KeyName));
}
else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase))
{
ReadAndAssert(reader);
reader.ReadAndAssert();
value = serializer.Deserialize(reader, reflectionObject.GetType(ValueName));
}
else
@@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Converters
reader.Skip();
}
ReadAndAssert(reader);
reader.ReadAndAssert();
}
return reflectionObject.Creator(key, value);
@@ -145,13 +145,5 @@ namespace Newtonsoft.Json.Converters
return false;
}
private static void ReadAndAssert(JsonReader reader)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when reading KeyValuePair.");
}
}
}
}
+248 -306
View File
@@ -457,67 +457,46 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsDateTimeOffset;
JsonToken t;
JsonToken t = ReadToContentInternal();
do
switch (t)
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
case JsonToken.None:
case JsonToken.Null:
case JsonToken.EndArray:
return null;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
case JsonToken.Date:
if (Value is DateTime)
{
SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value), false);
}
if (t == JsonToken.Date)
{
if (Value is DateTime)
{
SetToken(JsonToken.Date, new DateTimeOffset((DateTime)Value), false);
}
return (DateTimeOffset)Value;
case JsonToken.String:
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
return (DateTimeOffset)Value;
DateTimeOffset dt;
if (DateTimeUtils.TryParseDateTimeOffset(s, _dateFormatString, Culture, out dt))
{
SetToken(JsonToken.Date, dt, false);
return dt;
}
if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
{
SetToken(JsonToken.Date, dt, false);
return dt;
}
throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
default:
throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
if (t == JsonToken.Null)
{
return null;
}
if (t == JsonToken.String)
{
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
DateTimeOffset dt;
if (DateTimeUtils.TryParseDateTimeOffset(s, _dateFormatString, Culture, out dt))
{
SetToken(JsonToken.Date, dt, false);
return dt;
}
if (DateTimeOffset.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
{
SetToken(JsonToken.Date, dt, false);
return dt;
}
throw JsonReaderException.Create(this, "Could not convert string to DateTimeOffset: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
if (t == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
#endif
@@ -525,20 +504,12 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsBytes;
JsonToken t;
JsonToken t = ReadToContentInternal();
do
if (t == JsonToken.None)
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
return null;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
return null;
}
if (IsWrappedInTypeObject())
{
@@ -548,81 +519,66 @@ namespace Newtonsoft.Json
return data;
}
// attempt to convert possible base 64 or GUID string to bytes
// GUID has to have format 00000000-0000-0000-0000-000000000000
if (t == JsonToken.String)
switch (t)
{
string s = (string)Value;
byte[] data;
Guid g;
if (s.Length == 0)
{
data = new byte[0];
}
else if (ConvertUtils.TryConvertGuid(s, out g))
{
data = g.ToByteArray();
}
else
{
data = Convert.FromBase64String(s);
}
SetToken(JsonToken.Bytes, data, false);
return data;
}
if (t == JsonToken.Null)
{
return null;
}
if (t == JsonToken.Bytes)
{
if (ValueType == typeof(Guid))
{
byte[] data = ((Guid)Value).ToByteArray();
SetToken(JsonToken.Bytes, data, false);
return data;
}
return (byte[])Value;
}
if (t == JsonToken.StartArray)
{
List<byte> data = new List<byte>();
while (ReadInternal())
{
t = TokenType;
switch (t)
case JsonToken.String:
{
case JsonToken.Integer:
data.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture));
break;
case JsonToken.EndArray:
byte[] d = data.ToArray();
SetToken(JsonToken.Bytes, d, false);
return d;
case JsonToken.Comment:
// skip
break;
default:
throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
// attempt to convert possible base 64 or GUID string to bytes
// GUID has to have format 00000000-0000-0000-0000-000000000000
string s = (string)Value;
byte[] data;
Guid g;
if (s.Length == 0)
{
data = new byte[0];
}
else if (ConvertUtils.TryConvertGuid(s, out g))
{
data = g.ToByteArray();
}
else
{
data = Convert.FromBase64String(s);
}
SetToken(JsonToken.Bytes, data, false);
return data;
}
case JsonToken.Null:
case JsonToken.EndArray:
return null;
case JsonToken.Bytes:
if (ValueType == typeof(Guid))
{
byte[] data = ((Guid)Value).ToByteArray();
SetToken(JsonToken.Bytes, data, false);
return data;
}
}
throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
return (byte[])Value;
case JsonToken.StartArray:
List<byte> buffer = new List<byte>();
while (true)
{
switch (t = ReadToContentInternal())
{
case JsonToken.None:
throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
case JsonToken.Integer:
buffer.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture));
break;
case JsonToken.EndArray:
byte[] d = buffer.ToArray();
SetToken(JsonToken.Bytes, d, false);
return d;
default:
throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
}
}
if (t == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
@@ -630,62 +586,41 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsDecimal;
JsonToken t;
JsonToken t = ReadToContentInternal();
do
switch (t)
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
case JsonToken.None:
case JsonToken.Null:
case JsonToken.EndArray:
return null;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
case JsonToken.Integer:
case JsonToken.Float:
if (!(Value is decimal))
{
SetToken(JsonToken.Float, Convert.ToDecimal(Value, CultureInfo.InvariantCulture), false);
}
if (t == JsonToken.Integer || t == JsonToken.Float)
{
if (!(Value is decimal))
{
SetToken(JsonToken.Float, Convert.ToDecimal(Value, CultureInfo.InvariantCulture), false);
}
return (decimal)Value;
case JsonToken.String:
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
return (decimal)Value;
decimal d;
if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
{
SetToken(JsonToken.Float, d, false);
return d;
}
else
{
throw JsonReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
}
if (t == JsonToken.Null)
{
return null;
}
if (t == JsonToken.String)
{
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
decimal d;
if (decimal.TryParse(s, NumberStyles.Number, Culture, out d))
{
SetToken(JsonToken.Float, d, false);
return d;
}
else
{
throw JsonReaderException.Create(this, "Could not convert string to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
}
if (t == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading decimal. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
@@ -693,62 +628,39 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsInt32;
JsonToken t;
do
switch (ReadToContentInternal())
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
case JsonToken.None:
case JsonToken.Null:
case JsonToken.EndArray:
return null;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
case JsonToken.Integer:
case JsonToken.Float:
if (!(Value is int))
{
SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture), false);
}
if (t == JsonToken.Integer || t == JsonToken.Float)
{
if (!(Value is int))
{
SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture), false);
}
return (int)Value;
case JsonToken.String:
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
return (int)Value;
int i;
if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
{
SetToken(JsonToken.Integer, i, false);
return i;
}
else
{
throw JsonReaderException.Create(this, "Could not convert string to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
}
if (t == JsonToken.Null)
{
return null;
}
int i;
if (t == JsonToken.String)
{
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
if (int.TryParse(s, NumberStyles.Integer, Culture, out i))
{
SetToken(JsonToken.Integer, i, false);
return i;
}
else
{
throw JsonReaderException.Create(this, "Could not convert string to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
}
if (t == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading integer. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
}
@@ -756,29 +668,16 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsString;
JsonToken t;
JsonToken t = ReadToContentInternal();
do
switch (t)
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
case JsonToken.None:
case JsonToken.Null:
case JsonToken.EndArray:
return null;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
if (t == JsonToken.String)
{
return (string)Value;
}
if (t == JsonToken.Null)
{
return null;
case JsonToken.String:
return (string)Value;
}
if (JsonTokenUtils.IsPrimitiveToken(t))
@@ -800,11 +699,6 @@ namespace Newtonsoft.Json
}
}
if (t == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
}
@@ -812,55 +706,39 @@ namespace Newtonsoft.Json
{
_readType = ReadType.ReadAsDateTime;
do
switch (ReadToContentInternal())
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
case JsonToken.None:
case JsonToken.Null:
case JsonToken.EndArray:
return null;
}
} while (TokenType == JsonToken.Comment);
case JsonToken.Date:
return (DateTime)Value;
case JsonToken.String:
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
if (TokenType == JsonToken.Date)
{
return (DateTime)Value;
}
DateTime dt;
if (DateTimeUtils.TryParseDateTime(s, DateTimeZoneHandling, _dateFormatString, Culture, out dt))
{
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
SetToken(JsonToken.Date, dt, false);
return dt;
}
if (TokenType == JsonToken.Null)
{
return null;
}
if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
{
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
SetToken(JsonToken.Date, dt, false);
return dt;
}
if (TokenType == JsonToken.String)
{
string s = (string)Value;
if (string.IsNullOrEmpty(s))
{
SetToken(JsonToken.Null);
return null;
}
throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
DateTime dt;
if (DateTimeUtils.TryParseDateTime(s, DateTimeZoneHandling, _dateFormatString, Culture, out dt))
{
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
SetToken(JsonToken.Date, dt, false);
return dt;
}
if (DateTime.TryParse(s, Culture, DateTimeStyles.RoundtripKind, out dt))
{
dt = DateTimeUtils.EnsureDateTime(dt, DateTimeZoneHandling);
SetToken(JsonToken.Date, dt, false);
return dt;
}
throw JsonReaderException.Create(this, "Could not convert string to DateTime: {0}.".FormatWith(CultureInfo.InvariantCulture, Value));
}
if (TokenType == JsonToken.EndArray)
{
return null;
}
throw JsonReaderException.Create(this, "Error reading date. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
@@ -1109,5 +987,69 @@ namespace Newtonsoft.Json
_tokenType = JsonToken.None;
_value = null;
}
internal void ReadAndAssert()
{
if (!Read())
{
throw JsonSerializationException.Create(this, "Unexpected end when reading JSON.");
}
}
/// <summary>
/// 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();
}
/// <summary>
/// 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())
{
return false;
}
return MoveToContentUnsafe();
}
// MoveToContent() but assumes stream was already read into.
internal bool MoveToContentUnsafe()
{
while (TokenType == JsonToken.Comment)
{
if (!Read())
{
return false;
}
}
return true;
}
private JsonToken ReadToContentInternal()
{
JsonToken t;
do
{
if (!ReadInternal())
{
SetToken(JsonToken.None);
return JsonToken.None;
}
else
{
t = TokenType;
}
} while (t == JsonToken.Comment);
return t;
}
}
}
+1 -1
View File
@@ -1039,7 +1039,7 @@ namespace Newtonsoft.Json
}
else
{
throw JsonReaderException.Create(this, "Unexpected end.");
throw JsonReaderException.Create(this, "Unexpected end when reading JSON.");
}
return true;
case 'N':
+1 -4
View File
@@ -132,10 +132,7 @@ namespace Newtonsoft.Json.Linq
}
}
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
reader.MoveToContentUnsafe();
if (reader.TokenType != JsonToken.StartArray)
{
+1 -4
View File
@@ -221,10 +221,7 @@ namespace Newtonsoft.Json.Linq
}
}
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
reader.MoveToContentUnsafe();
if (reader.TokenType != JsonToken.StartConstructor)
{
+1 -4
View File
@@ -370,10 +370,7 @@ namespace Newtonsoft.Json.Linq
}
}
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
reader.MoveToContentUnsafe();
if (reader.TokenType != JsonToken.StartObject)
{
+1 -4
View File
@@ -382,10 +382,7 @@ namespace Newtonsoft.Json.Linq
}
}
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
reader.MoveToContentUnsafe();
if (reader.TokenType != JsonToken.PropertyName)
{
+35 -48
View File
@@ -2113,60 +2113,47 @@ namespace Newtonsoft.Json.Linq
if (reader.TokenType == JsonToken.None)
{
if (!reader.Read())
if (!(settings != null && settings.CommentHandling == CommentHandling.Ignore ? reader.ReadToContent() : reader.Read()))
{
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
}
}
while (true)
IJsonLineInfo lineInfo = reader as IJsonLineInfo;
switch (reader.TokenType)
{
IJsonLineInfo lineInfo = reader as IJsonLineInfo;
switch (reader.TokenType)
{
case JsonToken.StartObject:
return JObject.Load(reader, settings);
case JsonToken.StartArray:
return JArray.Load(reader, settings);
case JsonToken.StartConstructor:
return JConstructor.Load(reader, settings);
case JsonToken.PropertyName:
return JProperty.Load(reader, settings);
case JsonToken.String:
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.Date:
case JsonToken.Boolean:
case JsonToken.Bytes:
JValue v = new JValue(reader.Value);
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Comment:
if (settings != null && settings.CommentHandling == CommentHandling.Ignore)
{
if (!reader.Read())
{
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
}
continue;
}
v = JValue.CreateComment(reader.Value.ToString());
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Null:
v = JValue.CreateNull();
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Undefined:
v = JValue.CreateUndefined();
v.SetLineInfo(lineInfo, settings);
return v;
default:
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
case JsonToken.StartObject:
return JObject.Load(reader, settings);
case JsonToken.StartArray:
return JArray.Load(reader, settings);
case JsonToken.StartConstructor:
return JConstructor.Load(reader, settings);
case JsonToken.PropertyName:
return JProperty.Load(reader, settings);
case JsonToken.String:
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.Date:
case JsonToken.Boolean:
case JsonToken.Bytes:
JValue v = new JValue(reader.Value);
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Comment:
v = JValue.CreateComment(reader.Value.ToString());
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Null:
v = JValue.CreateNull();
v.SetLineInfo(lineInfo, settings);
return v;
case JsonToken.Undefined:
v = JValue.CreateUndefined();
v.SetLineInfo(lineInfo, settings);
return v;
default:
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
}
@@ -71,12 +71,9 @@ namespace Newtonsoft.Json.Serialization
JsonContract contract = Serializer._contractResolver.ResolveContract(objectType);
while (reader.TokenType == JsonToken.None || reader.TokenType == JsonToken.Comment)
if (!reader.MoveToContent())
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "No JSON content found.");
}
throw JsonSerializationException.Create(reader, "No JSON content found.");
}
if (reader.TokenType == JsonToken.StartArray)
@@ -94,16 +91,16 @@ namespace Newtonsoft.Json.Serialization
}
else if (reader.TokenType == JsonToken.StartObject)
{
CheckedRead(reader);
reader.ReadAndAssert();
string id = null;
if (Serializer.MetadataPropertyHandling != MetadataPropertyHandling.Ignore
&& reader.TokenType == JsonToken.PropertyName
&& string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
id = (reader.Value != null) ? reader.Value.ToString() : null;
CheckedRead(reader);
reader.ReadAndAssert();
}
if (contract.ContractType == JsonContractType.Dictionary)
@@ -249,13 +246,10 @@ namespace Newtonsoft.Json.Serialization
if (reader.TokenType == JsonToken.PropertyName)
{
string propertyName = (string)reader.Value;
do
if (!reader.ReadToContent())
{
if (!reader.Read())
{
break;
}
} while (reader.TokenType == JsonToken.Comment);
break;
}
if (CheckPropertyName(reader, propertyName))
{
@@ -421,7 +415,7 @@ namespace Newtonsoft.Json.Serialization
if (Serializer.MetadataPropertyHandling == MetadataPropertyHandling.Ignore)
{
// don't look for metadata properties
CheckedRead(reader);
reader.ReadAndAssert();
id = null;
}
else if (Serializer.MetadataPropertyHandling == MetadataPropertyHandling.ReadAhead)
@@ -439,7 +433,7 @@ namespace Newtonsoft.Json.Serialization
tokenReader.SupportMultipleContent = reader.SupportMultipleContent;
// start
CheckedRead(tokenReader);
tokenReader.ReadAndAssert();
reader = tokenReader;
}
@@ -452,7 +446,7 @@ namespace Newtonsoft.Json.Serialization
}
else
{
CheckedRead(reader);
reader.ReadAndAssert();
object newValue;
if (ReadMetadataProperties(reader, ref resolvedObjectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
{
@@ -498,7 +492,7 @@ namespace Newtonsoft.Json.Serialization
&& reader.TokenType == JsonToken.PropertyName
&& string.Equals(reader.Value.ToString(), JsonTypeReflector.ValuePropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
// the token should not be an object because the $type value could have been included in the object
// without needing the $value property
@@ -509,7 +503,7 @@ namespace Newtonsoft.Json.Serialization
object value = CreateValueInternal(reader, resolvedObjectType, primitiveContract, member, null, null, existingValue);
CheckedRead(reader);
reader.ReadAndAssert();
return value;
}
break;
@@ -641,7 +635,7 @@ namespace Newtonsoft.Json.Serialization
{
string qualifiedTypeName = (string)typeToken;
JsonReader typeTokenReader = typeToken.CreateReader();
CheckedRead(typeTokenReader);
typeTokenReader.ReadAndAssert();
ResolveTypeName(typeTokenReader, ref objectType, ref contract, member, containerContract, containerMember, qualifiedTypeName);
JToken valueToken = current[JsonTypeReflector.ValuePropertyName];
@@ -649,7 +643,7 @@ namespace Newtonsoft.Json.Serialization
{
while (true)
{
CheckedRead(reader);
reader.ReadAndAssert();
if (reader.TokenType == JsonToken.PropertyName)
{
if ((string)reader.Value == JsonTypeReflector.ValuePropertyName)
@@ -658,7 +652,7 @@ namespace Newtonsoft.Json.Serialization
}
}
CheckedRead(reader);
reader.ReadAndAssert();
reader.Skip();
}
}
@@ -672,7 +666,7 @@ namespace Newtonsoft.Json.Serialization
if (valuesToken != null)
{
JsonReader listReader = valuesToken.CreateReader();
CheckedRead(listReader);
listReader.ReadAndAssert();
newValue = CreateList(listReader, objectType, contract, member, existingValue, id);
reader.Skip();
@@ -680,7 +674,7 @@ namespace Newtonsoft.Json.Serialization
}
}
CheckedRead(reader);
reader.ReadAndAssert();
return false;
}
@@ -705,7 +699,7 @@ namespace Newtonsoft.Json.Serialization
if (string.Equals(propertyName, JsonTypeReflector.RefPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Null)
{
throw JsonSerializationException.Create(reader, "JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName));
@@ -713,7 +707,7 @@ namespace Newtonsoft.Json.Serialization
string reference = (reader.Value != null) ? reader.Value.ToString() : null;
CheckedRead(reader);
reader.ReadAndAssert();
if (reference != null)
{
@@ -738,29 +732,29 @@ namespace Newtonsoft.Json.Serialization
}
else if (string.Equals(propertyName, JsonTypeReflector.TypePropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
string qualifiedTypeName = reader.Value.ToString();
ResolveTypeName(reader, ref objectType, ref contract, member, containerContract, containerMember, qualifiedTypeName);
CheckedRead(reader);
reader.ReadAndAssert();
metadataProperty = true;
}
else if (string.Equals(propertyName, JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
id = (reader.Value != null) ? reader.Value.ToString() : null;
CheckedRead(reader);
reader.ReadAndAssert();
metadataProperty = true;
}
else if (string.Equals(propertyName, JsonTypeReflector.ArrayValuesPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
reader.ReadAndAssert();
object list = CreateList(reader, objectType, contract, member, existingValue, id);
CheckedRead(reader);
reader.ReadAndAssert();
newValue = list;
return true;
}
@@ -842,14 +836,6 @@ namespace Newtonsoft.Json.Serialization
return arrayContract;
}
private void CheckedRead(JsonReader reader)
{
if (!reader.Read())
{
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
}
}
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id)
{
object value;
@@ -1475,8 +1461,6 @@ namespace Newtonsoft.Json.Serialization
currentList = listStack.Peek();
previousErrorIndex = null;
break;
case JsonToken.Comment:
break;
default:
object value;
@@ -1630,8 +1614,6 @@ namespace Newtonsoft.Json.Serialization
case JsonToken.EndArray:
finished = true;
break;
case JsonToken.Comment:
break;
default:
object value;
@@ -1762,7 +1744,7 @@ namespace Newtonsoft.Json.Serialization
JsonConverter itemConverter = GetConverter(itemContract, null, contract, member);
JsonReader tokenReader = token.CreateReader();
CheckedRead(tokenReader); // Move to first token
tokenReader.ReadAndAssert(); // Move to first token
object result;
if (itemConverter != null && itemConverter.CanRead)
@@ -2213,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.Read();
return reader.ReadToContent();
}
ReadType t = (contract != null) ? contract.InternalReadType : ReadType.Read;
@@ -2221,15 +2203,7 @@ namespace Newtonsoft.Json.Serialization
switch (t)
{
case ReadType.Read:
do
{
if (!reader.Read())
{
return false;
}
} while (reader.TokenType == JsonToken.Comment);
return true;
return reader.ReadToContent();
case ReadType.ReadAsInt32:
reader.ReadAsInt32();
break;