-Fixed JToken Load and Parse methods not checking for incomplete content

This commit is contained in:
JamesNK
2010-12-27 10:06:35 +00:00
parent 1297d64457
commit f32e1f53c5
7 changed files with 95 additions and 23 deletions
+1 -4
View File
@@ -111,10 +111,7 @@ namespace Newtonsoft.Json.Linq
JArray a = new JArray();
a.SetLineInfo(reader as IJsonLineInfo);
if (!reader.Read())
throw new Exception("Error reading JArray from JsonReader.");
a.ReadContentFrom(reader);
a.ReadTokenFrom(reader);
return a;
}
+1 -4
View File
@@ -185,10 +185,7 @@ namespace Newtonsoft.Json.Linq
JConstructor c = new JConstructor((string)reader.Value);
c.SetLineInfo(reader as IJsonLineInfo);
if (!reader.Read())
throw new Exception("Error reading JConstructor from JsonReader.");
c.ReadContentFrom(reader);
c.ReadTokenFrom(reader);
return c;
}
+15
View File
@@ -668,6 +668,21 @@ namespace Newtonsoft.Json.Linq
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)
{
ValidationUtils.ArgumentNotNull(r, "r");
+2 -4
View File
@@ -257,6 +257,7 @@ namespace Newtonsoft.Json.Linq
if (!reader.Read())
throw new Exception("Error reading JObject from JsonReader.");
}
if (reader.TokenType != JsonToken.StartObject)
throw new Exception(
"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();
o.SetLineInfo(reader as IJsonLineInfo);
if (!reader.Read())
throw new Exception("Error reading JObject from JsonReader.");
o.ReadContentFrom(reader);
o.ReadTokenFrom(reader);
return o;
}
+1 -4
View File
@@ -260,10 +260,7 @@ namespace Newtonsoft.Json.Linq
JProperty p = new JProperty((string)reader.Value);
p.SetLineInfo(reader as IJsonLineInfo);
if (!reader.Read())
throw new Exception("Error reading JProperty from JsonReader.");
p.ReadContentFrom(reader);
p.ReadTokenFrom(reader);
return p;
}