-Changed LINQ to JSON load methods to support reading past preceding comments

This commit is contained in:
James Newton-King
2012-07-16 00:01:40 +12:00
parent 55a09d2f73
commit 5d83daa804
8 changed files with 38 additions and 3 deletions
@@ -803,6 +803,7 @@ keyword such as type of business.""
o.Children()["item"].Children()["title"].Values<string>().ToArray()); o.Children()["item"].Children()["title"].Values<string>().ToArray());
} }
[Test]
public void UriGuidTimeSpanTestClassEmptyTest() public void UriGuidTimeSpanTestClassEmptyTest()
{ {
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass(); UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();
@@ -824,6 +825,7 @@ keyword such as type of business.""
Assert.AreEqual(c1.Uri, c2.Uri); Assert.AreEqual(c1.Uri, c2.Uri);
} }
[Test]
public void UriGuidTimeSpanTestClassValuesTest() public void UriGuidTimeSpanTestClassValuesTest()
{ {
UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
@@ -851,5 +853,17 @@ keyword such as type of business.""
Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan); Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
Assert.AreEqual(c1.Uri, c2.Uri); Assert.AreEqual(c1.Uri, c2.Uri);
} }
[Test]
public void ParseWithPrecendingComments()
{
string json = @"/* blah */ {'hi':'hi!'}";
JObject o = JObject.Parse(json);
Assert.AreEqual("hi!", (string)o["hi"]);
json = @"/* blah */ ['hi!']";
JArray a = JArray.Parse(json);
Assert.AreEqual("hi!", (string)a[0]);
}
} }
} }
@@ -72,5 +72,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.5.7.15114")] [assembly: AssemblyFileVersion("4.5.7.15115")]
#endif #endif
+6
View File
@@ -113,6 +113,12 @@ namespace Newtonsoft.Json.Linq
if (!reader.Read()) if (!reader.Read())
throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader."); throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader.");
} }
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
if (reader.TokenType != JsonToken.StartArray) if (reader.TokenType != JsonToken.StartArray)
throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
+5
View File
@@ -187,6 +187,11 @@ namespace Newtonsoft.Json.Linq
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader."); throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
} }
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
if (reader.TokenType != JsonToken.StartConstructor) if (reader.TokenType != JsonToken.StartConstructor)
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
+5
View File
@@ -292,6 +292,11 @@ namespace Newtonsoft.Json.Linq
throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader."); throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader.");
} }
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
if (reader.TokenType != JsonToken.StartObject) if (reader.TokenType != JsonToken.StartObject)
{ {
throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
+6
View File
@@ -229,6 +229,12 @@ namespace Newtonsoft.Json.Linq
if (!reader.Read()) if (!reader.Read())
throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader."); throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
} }
while (reader.TokenType == JsonToken.Comment)
{
reader.Read();
}
if (reader.TokenType != JsonToken.PropertyName) if (reader.TokenType != JsonToken.PropertyName)
throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
-1
View File
@@ -1222,7 +1222,6 @@ namespace Newtonsoft.Json.Linq
if (!JsonReader.IsStartToken(reader.TokenType)) if (!JsonReader.IsStartToken(reader.TokenType))
return new JValue(reader.Value); return new JValue(reader.Value);
// TODO: loading constructor and parameters?
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
} }
@@ -90,7 +90,7 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.5.7.15114")] [assembly: AssemblyFileVersion("4.5.7.15115")]
#endif #endif
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]