-Fixed JToken Load and Parse methods not checking for incomplete content
This commit is contained in:
@@ -429,5 +429,12 @@ Parameter name: index")]
|
|||||||
Assert.AreEqual(1, a.Count);
|
Assert.AreEqual(1, a.Count);
|
||||||
Assert.AreEqual(1, (int)a[0]);
|
Assert.AreEqual(1, (int)a[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected end of content while loading JArray.")]
|
||||||
|
public void ParseIncomplete()
|
||||||
|
{
|
||||||
|
JArray.Parse("[1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -464,13 +464,17 @@ Parameter name: arrayIndex")]
|
|||||||
public void DeserializeClassManually()
|
public void DeserializeClassManually()
|
||||||
{
|
{
|
||||||
string jsonText = @"{
|
string jsonText = @"{
|
||||||
""short"":{
|
""short"":
|
||||||
""original"":""http://www.foo.com/"",
|
{
|
||||||
""short"":""krehqk"",
|
""original"":""http://www.foo.com/"",
|
||||||
""error"":{
|
""short"":""krehqk"",
|
||||||
""code"":0,
|
""error"":
|
||||||
""msg"":""No action taken""}
|
{
|
||||||
}";
|
""code"":0,
|
||||||
|
""msg"":""No action taken""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
JObject json = JObject.Parse(jsonText);
|
JObject json = JObject.Parse(jsonText);
|
||||||
|
|
||||||
@@ -1490,6 +1494,63 @@ Parameter name: arrayIndex")]
|
|||||||
JObject.Parse(json);
|
JObject.Parse(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected end of content while loading JObject.")]
|
||||||
|
public void ParseIncomplete()
|
||||||
|
{
|
||||||
|
JObject.Parse("{ foo:");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void LoadFromNestedObject()
|
||||||
|
{
|
||||||
|
string jsonText = @"{
|
||||||
|
""short"":
|
||||||
|
{
|
||||||
|
""error"":
|
||||||
|
{
|
||||||
|
""code"":0,
|
||||||
|
""msg"":""No action taken""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}";
|
||||||
|
|
||||||
|
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
JObject o = (JObject)JToken.ReadFrom(reader);
|
||||||
|
Assert.IsNotNull(o);
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""code"": 0,
|
||||||
|
""msg"": ""No action taken""
|
||||||
|
}", o.ToString(Formatting.Indented));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[ExpectedException(typeof(Exception), ExpectedMessage = "Unexpected end of content while loading JObject.")]
|
||||||
|
public void LoadFromNestedObjectIncomplete()
|
||||||
|
{
|
||||||
|
string jsonText = @"{
|
||||||
|
""short"":
|
||||||
|
{
|
||||||
|
""error"":
|
||||||
|
{
|
||||||
|
""code"":0";
|
||||||
|
|
||||||
|
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
JToken.ReadFrom(reader);
|
||||||
|
}
|
||||||
|
|
||||||
#if !SILVERLIGHT
|
#if !SILVERLIGHT
|
||||||
[Test]
|
[Test]
|
||||||
public void GetProperties()
|
public void GetProperties()
|
||||||
|
|||||||
@@ -111,10 +111,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
JArray a = new JArray();
|
JArray a = new JArray();
|
||||||
a.SetLineInfo(reader as IJsonLineInfo);
|
a.SetLineInfo(reader as IJsonLineInfo);
|
||||||
|
|
||||||
if (!reader.Read())
|
a.ReadTokenFrom(reader);
|
||||||
throw new Exception("Error reading JArray from JsonReader.");
|
|
||||||
|
|
||||||
a.ReadContentFrom(reader);
|
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,10 +185,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
JConstructor c = new JConstructor((string)reader.Value);
|
JConstructor c = new JConstructor((string)reader.Value);
|
||||||
c.SetLineInfo(reader as IJsonLineInfo);
|
c.SetLineInfo(reader as IJsonLineInfo);
|
||||||
|
|
||||||
if (!reader.Read())
|
c.ReadTokenFrom(reader);
|
||||||
throw new Exception("Error reading JConstructor from JsonReader.");
|
|
||||||
|
|
||||||
c.ReadContentFrom(reader);
|
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -668,6 +668,21 @@ namespace Newtonsoft.Json.Linq
|
|||||||
ClearItems();
|
ClearItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void ReadTokenFrom(JsonReader r)
|
||||||
|
{
|
||||||
|
int startDepth = r.Depth;
|
||||||
|
|
||||||
|
if (!r.Read())
|
||||||
|
throw new Exception("Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
|
||||||
|
|
||||||
|
ReadContentFrom(r);
|
||||||
|
|
||||||
|
int endDepth = r.Depth;
|
||||||
|
|
||||||
|
if (endDepth > startDepth)
|
||||||
|
throw new Exception("Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
|
||||||
|
}
|
||||||
|
|
||||||
internal void ReadContentFrom(JsonReader r)
|
internal void ReadContentFrom(JsonReader r)
|
||||||
{
|
{
|
||||||
ValidationUtils.ArgumentNotNull(r, "r");
|
ValidationUtils.ArgumentNotNull(r, "r");
|
||||||
|
|||||||
@@ -257,6 +257,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
if (!reader.Read())
|
if (!reader.Read())
|
||||||
throw new Exception("Error reading JObject from JsonReader.");
|
throw new Exception("Error reading JObject from JsonReader.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reader.TokenType != JsonToken.StartObject)
|
if (reader.TokenType != JsonToken.StartObject)
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
"Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(
|
"Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(
|
||||||
@@ -265,10 +266,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
JObject o = new JObject();
|
JObject o = new JObject();
|
||||||
o.SetLineInfo(reader as IJsonLineInfo);
|
o.SetLineInfo(reader as IJsonLineInfo);
|
||||||
|
|
||||||
if (!reader.Read())
|
o.ReadTokenFrom(reader);
|
||||||
throw new Exception("Error reading JObject from JsonReader.");
|
|
||||||
|
|
||||||
o.ReadContentFrom(reader);
|
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -260,10 +260,7 @@ namespace Newtonsoft.Json.Linq
|
|||||||
JProperty p = new JProperty((string)reader.Value);
|
JProperty p = new JProperty((string)reader.Value);
|
||||||
p.SetLineInfo(reader as IJsonLineInfo);
|
p.SetLineInfo(reader as IJsonLineInfo);
|
||||||
|
|
||||||
if (!reader.Read())
|
p.ReadTokenFrom(reader);
|
||||||
throw new Exception("Error reading JProperty from JsonReader.");
|
|
||||||
|
|
||||||
p.ReadContentFrom(reader);
|
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user