-Added JsonLoadSettings
-Added CommentsHandling -Changed LINQ to JSON load to ignore array comments by default
This commit is contained in:
@@ -544,5 +544,21 @@ Parameter name: index");
|
||||
l = decks.ToList();
|
||||
Assert.AreEqual(1, l.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_NoComments()
|
||||
{
|
||||
string json = "[1,2/*comment*/,3]";
|
||||
|
||||
JArray a = JArray.Parse(json, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Ignore
|
||||
});
|
||||
|
||||
Assert.AreEqual(3, a.Count);
|
||||
Assert.AreEqual(1, (int)a[0]);
|
||||
Assert.AreEqual(2, (int)a[1]);
|
||||
Assert.AreEqual(3, (int)a[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1977,5 +1977,21 @@ Parameter name: arrayIndex");
|
||||
|
||||
Assert.AreEqual(@"{""foo"":""bar"",""name"":""Daniel Wertheim"",""birthDate"":""0001-01-01T00:00:00"",""lastModified"":""0001-01-01T00:00:00""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_NoComments()
|
||||
{
|
||||
string json = "{'prop':[1,2/*comment*/,3]}";
|
||||
|
||||
JObject o = JObject.Parse(json, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Ignore
|
||||
});
|
||||
|
||||
Assert.AreEqual(3, o["prop"].Count());
|
||||
Assert.AreEqual(1, (int)o["prop"][0]);
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1240,5 +1240,21 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
Assert.AreEqual("[0]", a[0].Path);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_NoComments()
|
||||
{
|
||||
string json = "{'prop':[1,2/*comment*/,3]}";
|
||||
|
||||
JToken o = JToken.Parse(json, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Ignore
|
||||
});
|
||||
|
||||
Assert.AreEqual(3, o["prop"].Count());
|
||||
Assert.AreEqual(1, (int)o["prop"][0]);
|
||||
Assert.AreEqual(2, (int)o["prop"][1]);
|
||||
Assert.AreEqual(3, (int)o["prop"][2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,13 +171,34 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
]");
|
||||
|
||||
JsonTextReader jsonReader = new JsonTextReader(textReader);
|
||||
JArray a = (JArray)JToken.ReadFrom(jsonReader);
|
||||
JArray a = (JArray)JToken.ReadFrom(jsonReader, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Load
|
||||
});
|
||||
|
||||
Assert.AreEqual(4, a.Count);
|
||||
Assert.AreEqual(JTokenType.Comment, a[0].Type);
|
||||
Assert.AreEqual(" hi", ((JValue)a[0]).Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CommentsAndReadFrom_IgnoreComments()
|
||||
{
|
||||
StringReader textReader = new StringReader(@"[
|
||||
// hi
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]");
|
||||
|
||||
JsonTextReader jsonReader = new JsonTextReader(textReader);
|
||||
JArray a = (JArray)JToken.ReadFrom(jsonReader);
|
||||
|
||||
Assert.AreEqual(3, a.Count);
|
||||
Assert.AreEqual(JTokenType.Integer, a[0].Type);
|
||||
Assert.AreEqual(1, ((JValue)a[0]).Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StartingCommentAndReadFrom()
|
||||
{
|
||||
@@ -190,7 +211,10 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
]");
|
||||
|
||||
JsonTextReader jsonReader = new JsonTextReader(textReader);
|
||||
JValue v = (JValue)JToken.ReadFrom(jsonReader);
|
||||
JValue v = (JValue)JToken.ReadFrom(jsonReader, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Load
|
||||
});
|
||||
|
||||
Assert.AreEqual(JTokenType.Comment, v.Type);
|
||||
|
||||
@@ -200,6 +224,31 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(0, lineInfo.LinePosition);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StartingCommentAndReadFrom_IgnoreComments()
|
||||
{
|
||||
StringReader textReader = new StringReader(@"
|
||||
// hi
|
||||
[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]");
|
||||
|
||||
JsonTextReader jsonReader = new JsonTextReader(textReader);
|
||||
JArray a = (JArray)JToken.ReadFrom(jsonReader, new JsonLoadSettings
|
||||
{
|
||||
CommentHandling = CommentHandling.Ignore
|
||||
});
|
||||
|
||||
Assert.AreEqual(JTokenType.Array, a.Type);
|
||||
|
||||
IJsonLineInfo lineInfo = a;
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(3, lineInfo.LineNumber);
|
||||
Assert.AreEqual(1, lineInfo.LinePosition);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StartingUndefinedAndReadFrom()
|
||||
{
|
||||
|
||||
@@ -881,10 +881,9 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(3, o.Count);
|
||||
Assert.AreEqual(true, (bool)o["A"]);
|
||||
Assert.AreEqual(false, (bool)o["B"]);
|
||||
Assert.AreEqual(3, o["C"].Count());
|
||||
Assert.AreEqual(JTokenType.Comment, o["C"][0].Type);
|
||||
Assert.AreEqual(1, (int)o["C"][1]);
|
||||
Assert.AreEqual(JTokenType.Comment, o["C"][2].Type);
|
||||
Assert.AreEqual(1, o["C"].Count());
|
||||
Assert.AreEqual(1, (int)o["C"][0]);
|
||||
|
||||
Assert.IsTrue(JToken.DeepEquals(o, JObject.Parse(json)));
|
||||
|
||||
json = @"{/* Test */}";
|
||||
|
||||
@@ -434,7 +434,7 @@ namespace Newtonsoft.Json
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read the token from.</param>
|
||||
public void WriteToken(JsonReader reader)
|
||||
{
|
||||
WriteToken(reader, true, true);
|
||||
WriteToken(reader, true, true, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -446,7 +446,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(reader, "reader");
|
||||
|
||||
WriteToken(reader, writeChildren, true);
|
||||
WriteToken(reader, writeChildren, true, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -471,7 +471,7 @@ namespace Newtonsoft.Json
|
||||
WriteTokenInternal(token, null);
|
||||
}
|
||||
|
||||
internal void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate)
|
||||
internal void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
|
||||
{
|
||||
int initialDepth;
|
||||
|
||||
@@ -482,18 +482,25 @@ namespace Newtonsoft.Json
|
||||
else
|
||||
initialDepth = reader.Depth;
|
||||
|
||||
WriteToken(reader, initialDepth, writeChildren, writeDateConstructorAsDate);
|
||||
WriteToken(reader, initialDepth, writeChildren, writeDateConstructorAsDate, writeComments);
|
||||
}
|
||||
|
||||
internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate)
|
||||
internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
|
||||
{
|
||||
do
|
||||
{
|
||||
// write a JValue date when the constructor is for a date
|
||||
if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
|
||||
{
|
||||
WriteConstructorDate(reader);
|
||||
}
|
||||
else
|
||||
WriteTokenInternal(reader.TokenType, reader.Value);
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Comment || writeComments)
|
||||
{
|
||||
WriteTokenInternal(reader.TokenType, reader.Value);
|
||||
}
|
||||
}
|
||||
} while (
|
||||
// stop if we have reached the end of the token being read
|
||||
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies how JSON comments are handled when loading JSON.
|
||||
/// </summary>
|
||||
public enum CommentHandling
|
||||
{
|
||||
/// <summary>
|
||||
/// Ignore comments.
|
||||
/// </summary>
|
||||
Ignore = 0,
|
||||
/// <summary>
|
||||
/// Load comments as a <see cref="JValue"/> with type <see cref="JTokenType.Comment"/>.
|
||||
/// </summary>
|
||||
Load = 1
|
||||
}
|
||||
}
|
||||
@@ -111,6 +111,18 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
|
||||
/// <returns>A <see cref="JArray"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JArray Load(JsonReader reader)
|
||||
{
|
||||
return Load(reader, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="JArray"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JArray"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JArray Load(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.None)
|
||||
{
|
||||
@@ -129,7 +141,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JArray a = new JArray();
|
||||
a.SetLineInfo(reader as IJsonLineInfo);
|
||||
|
||||
a.ReadTokenFrom(reader);
|
||||
a.ReadTokenFrom(reader, settings);
|
||||
|
||||
return a;
|
||||
}
|
||||
@@ -143,10 +155,25 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from Text" />
|
||||
/// </example>
|
||||
public new static JArray Parse(string json)
|
||||
{
|
||||
return Parse(json, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load a <see cref="JArray"/> from a string that contains JSON.
|
||||
/// </summary>
|
||||
/// <param name="json">A <see cref="String"/> that contains JSON.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JArray"/> populated from the string that contains JSON.</returns>
|
||||
/// <example>
|
||||
/// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from Text" />
|
||||
/// </example>
|
||||
public new static JArray Parse(string json, JsonLoadSettings settings)
|
||||
{
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
JArray a = Load(reader);
|
||||
JArray a = Load(reader, settings);
|
||||
|
||||
if (reader.Read() && reader.TokenType != JsonToken.Comment)
|
||||
throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
|
||||
|
||||
@@ -192,6 +192,18 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
|
||||
/// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JConstructor Load(JsonReader reader)
|
||||
{
|
||||
return Load(reader, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JConstructor Load(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.None)
|
||||
{
|
||||
@@ -210,7 +222,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JConstructor c = new JConstructor((string)reader.Value);
|
||||
c.SetLineInfo(reader as IJsonLineInfo);
|
||||
|
||||
c.ReadTokenFrom(reader);
|
||||
c.ReadTokenFrom(reader, settings);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -678,14 +678,14 @@ namespace Newtonsoft.Json.Linq
|
||||
MergeItem(content, settings);
|
||||
}
|
||||
|
||||
internal void ReadTokenFrom(JsonReader reader)
|
||||
internal void ReadTokenFrom(JsonReader reader, JsonLoadSettings options)
|
||||
{
|
||||
int startDepth = reader.Depth;
|
||||
|
||||
if (!reader.Read())
|
||||
throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
|
||||
|
||||
ReadContentFrom(reader);
|
||||
ReadContentFrom(reader, options);
|
||||
|
||||
int endDepth = reader.Depth;
|
||||
|
||||
@@ -693,7 +693,7 @@ namespace Newtonsoft.Json.Linq
|
||||
throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
|
||||
}
|
||||
|
||||
internal void ReadContentFrom(JsonReader r)
|
||||
internal void ReadContentFrom(JsonReader r, JsonLoadSettings options)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(r, "r");
|
||||
IJsonLineInfo lineInfo = r as IJsonLineInfo;
|
||||
@@ -763,9 +763,12 @@ namespace Newtonsoft.Json.Linq
|
||||
parent.Add(v);
|
||||
break;
|
||||
case JsonToken.Comment:
|
||||
v = JValue.CreateComment(r.Value.ToString());
|
||||
v.SetLineInfo(lineInfo);
|
||||
parent.Add(v);
|
||||
if (options != null && options.CommentHandling == CommentHandling.Load)
|
||||
{
|
||||
v = JValue.CreateComment(r.Value.ToString());
|
||||
v.SetLineInfo(lineInfo);
|
||||
parent.Add(v);
|
||||
}
|
||||
break;
|
||||
case JsonToken.Null:
|
||||
v = JValue.CreateNull();
|
||||
|
||||
@@ -322,6 +322,18 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JObject"/>.</param>
|
||||
/// <returns>A <see cref="JObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JObject Load(JsonReader reader)
|
||||
{
|
||||
return Load(reader, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="JObject"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JObject"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JObject Load(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(reader, "reader");
|
||||
|
||||
@@ -344,7 +356,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JObject o = new JObject();
|
||||
o.SetLineInfo(reader as IJsonLineInfo);
|
||||
|
||||
o.ReadTokenFrom(reader);
|
||||
o.ReadTokenFrom(reader, settings);
|
||||
|
||||
return o;
|
||||
}
|
||||
@@ -358,10 +370,25 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Parsing a JSON Object from Text" />
|
||||
/// </example>
|
||||
public new static JObject Parse(string json)
|
||||
{
|
||||
return Parse(json, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load a <see cref="JObject"/> from a string that contains JSON.
|
||||
/// </summary>
|
||||
/// <param name="json">A <see cref="String"/> that contains JSON.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JObject"/> populated from the string that contains JSON.</returns>
|
||||
/// <example>
|
||||
/// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Parsing a JSON Object from Text" />
|
||||
/// </example>
|
||||
public new static JObject Parse(string json, JsonLoadSettings settings)
|
||||
{
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
JObject o = Load(reader);
|
||||
JObject o = Load(reader, settings);
|
||||
|
||||
if (reader.Read() && reader.TokenType != JsonToken.Comment)
|
||||
throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
|
||||
|
||||
@@ -329,6 +329,18 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
|
||||
/// <returns>A <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JProperty Load(JsonReader reader)
|
||||
{
|
||||
return Load(reader, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="JProperty"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
|
||||
public new static JProperty Load(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.None)
|
||||
{
|
||||
@@ -347,7 +359,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JProperty p = new JProperty((string)reader.Value);
|
||||
p.SetLineInfo(reader as IJsonLineInfo);
|
||||
|
||||
p.ReadTokenFrom(reader);
|
||||
p.ReadTokenFrom(reader, settings);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -1848,6 +1848,22 @@ namespace Newtonsoft.Json.Linq
|
||||
/// by the token type of the first token encountered in the reader.
|
||||
/// </returns>
|
||||
public static JToken ReadFrom(JsonReader reader)
|
||||
{
|
||||
return ReadFrom(reader, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="JToken"/> that contains the token and its descendant tokens
|
||||
/// that were read from the reader. The runtime type of the token is determined
|
||||
/// by the token type of the first token encountered in the reader.
|
||||
/// </returns>
|
||||
public static JToken ReadFrom(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(reader, "reader");
|
||||
|
||||
@@ -1857,41 +1873,52 @@ namespace Newtonsoft.Json.Linq
|
||||
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
|
||||
}
|
||||
|
||||
IJsonLineInfo lineInfo = reader as IJsonLineInfo;
|
||||
|
||||
switch (reader.TokenType)
|
||||
while (true)
|
||||
{
|
||||
case JsonToken.StartObject:
|
||||
return JObject.Load(reader);
|
||||
case JsonToken.StartArray:
|
||||
return JArray.Load(reader);
|
||||
case JsonToken.StartConstructor:
|
||||
return JConstructor.Load(reader);
|
||||
case JsonToken.PropertyName:
|
||||
return JProperty.Load(reader);
|
||||
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);
|
||||
return v;
|
||||
case JsonToken.Comment:
|
||||
v = JValue.CreateComment(reader.Value.ToString());
|
||||
v.SetLineInfo(lineInfo);
|
||||
return v;
|
||||
case JsonToken.Null:
|
||||
v = JValue.CreateNull();
|
||||
v.SetLineInfo(lineInfo);
|
||||
return v;
|
||||
case JsonToken.Undefined:
|
||||
v = JValue.CreateUndefined();
|
||||
v.SetLineInfo(lineInfo);
|
||||
return v;
|
||||
default:
|
||||
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, 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);
|
||||
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);
|
||||
return v;
|
||||
case JsonToken.Null:
|
||||
v = JValue.CreateNull();
|
||||
v.SetLineInfo(lineInfo);
|
||||
return v;
|
||||
case JsonToken.Undefined:
|
||||
v = JValue.CreateUndefined();
|
||||
v.SetLineInfo(lineInfo);
|
||||
return v;
|
||||
default:
|
||||
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1901,10 +1928,22 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <param name="json">A <see cref="String"/> that contains JSON.</param>
|
||||
/// <returns>A <see cref="JToken"/> populated from the string that contains JSON.</returns>
|
||||
public static JToken Parse(string json)
|
||||
{
|
||||
return Parse(json, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load a <see cref="JToken"/> from a string that contains JSON.
|
||||
/// </summary>
|
||||
/// <param name="json">A <see cref="String"/> that contains JSON.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>A <see cref="JToken"/> populated from the string that contains JSON.</returns>
|
||||
public static JToken Parse(string json, JsonLoadSettings settings)
|
||||
{
|
||||
using (JsonReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
JToken t = Load(reader);
|
||||
JToken t = Load(reader, settings);
|
||||
|
||||
if (reader.Read() && reader.TokenType != JsonToken.Comment)
|
||||
throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
|
||||
@@ -1913,6 +1952,22 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
|
||||
/// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
|
||||
/// If this is null, default load settings will be used.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="JToken"/> that contains the token and its descendant tokens
|
||||
/// that were read from the reader. The runtime type of the token is determined
|
||||
/// by the token type of the first token encountered in the reader.
|
||||
/// </returns>
|
||||
public static JToken Load(JsonReader reader, JsonLoadSettings settings)
|
||||
{
|
||||
return ReadFrom(reader, settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
@@ -1924,7 +1979,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// </returns>
|
||||
public static JToken Load(JsonReader reader)
|
||||
{
|
||||
return ReadFrom(reader);
|
||||
return Load(reader, null);
|
||||
}
|
||||
|
||||
internal void SetLineInfo(IJsonLineInfo lineInfo)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies the settings used when loading JSON.
|
||||
/// </summary>
|
||||
public class JsonLoadSettings
|
||||
{
|
||||
private CommentHandling _commentHandling;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how JSON comments are handled when loading JSON.
|
||||
/// </summary>
|
||||
/// <value>The JSON comment handling.</value>
|
||||
public CommentHandling CommentHandling
|
||||
{
|
||||
get { return _commentHandling; }
|
||||
set
|
||||
{
|
||||
if (value < CommentHandling.Ignore || value > CommentHandling.Load)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("value");
|
||||
}
|
||||
|
||||
_commentHandling = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,9 @@
|
||||
<Compile Include="JsonExtensionDataAttribute.cs" />
|
||||
<Compile Include="JsonPosition.cs" />
|
||||
<Compile Include="JsonRequiredAttribute.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\JsonMergeSettings.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
|
||||
@@ -122,8 +122,10 @@
|
||||
<Compile Include="JsonExtensionDataAttribute.cs" />
|
||||
<Compile Include="JsonPosition.cs" />
|
||||
<Compile Include="JsonRequiredAttribute.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\JPropertyDescriptor.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\JsonMergeSettings.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
|
||||
@@ -123,7 +123,9 @@
|
||||
<Compile Include="JsonExtensionDataAttribute.cs" />
|
||||
<Compile Include="JsonPosition.cs" />
|
||||
<Compile Include="JsonRequiredAttribute.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\JsonMergeSettings.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
|
||||
@@ -104,6 +104,7 @@
|
||||
<Compile Include="JsonValidatingReader.cs" />
|
||||
<Compile Include="JsonWriter.cs" />
|
||||
<Compile Include="JsonWriterException.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\Extensions.cs" />
|
||||
<Compile Include="Linq\IJEnumerable.cs" />
|
||||
<Compile Include="Linq\JArray.cs" />
|
||||
@@ -115,6 +116,7 @@
|
||||
<Compile Include="Linq\JPropertyDescriptor.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JRaw.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\JsonMergeSettings.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
|
||||
@@ -109,6 +109,7 @@
|
||||
<Compile Include="JsonValidatingReader.cs" />
|
||||
<Compile Include="JsonWriter.cs" />
|
||||
<Compile Include="JsonWriterException.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\Extensions.cs" />
|
||||
<Compile Include="Linq\IJEnumerable.cs" />
|
||||
<Compile Include="Linq\JArray.cs" />
|
||||
@@ -120,6 +121,7 @@
|
||||
<Compile Include="Linq\JPropertyDescriptor.cs" />
|
||||
<Compile Include="Linq\JPropertyKeyedCollection.cs" />
|
||||
<Compile Include="Linq\JRaw.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\JsonMergeSettings.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayIndexFilter.cs" />
|
||||
<Compile Include="Linq\JsonPath\ArrayMultipleIndexFilter.cs" />
|
||||
|
||||
@@ -108,6 +108,7 @@
|
||||
<Compile Include="JsonValidatingReader.cs" />
|
||||
<Compile Include="JsonWriter.cs" />
|
||||
<Compile Include="JsonWriterException.cs" />
|
||||
<Compile Include="Linq\CommentHandling.cs" />
|
||||
<Compile Include="Linq\Extensions.cs" />
|
||||
<Compile Include="Linq\IJEnumerable.cs" />
|
||||
<Compile Include="Linq\JArray.cs" />
|
||||
@@ -136,6 +137,7 @@
|
||||
<Compile Include="Linq\JTokenType.cs" />
|
||||
<Compile Include="Linq\JTokenWriter.cs" />
|
||||
<Compile Include="Linq\JValue.cs" />
|
||||
<Compile Include="Linq\JsonLoadSettings.cs" />
|
||||
<Compile Include="Linq\MergeArrayHandling.cs" />
|
||||
<Compile Include="MemberSerialization.cs" />
|
||||
<Compile Include="MetadataPropertyHandling.cs" />
|
||||
|
||||
@@ -240,7 +240,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
continue;
|
||||
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WriteToken(reader, true, true);
|
||||
writer.WriteToken(reader, true, true, false);
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.Comment)
|
||||
{
|
||||
|
||||
@@ -32,42 +32,42 @@ namespace Newtonsoft.Json.Serialization
|
||||
public override bool Read()
|
||||
{
|
||||
var value = _innerReader.Read();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
public override int? ReadAsInt32()
|
||||
{
|
||||
var value = _innerReader.ReadAsInt32();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
public override string ReadAsString()
|
||||
{
|
||||
var value = _innerReader.ReadAsString();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
public override byte[] ReadAsBytes()
|
||||
{
|
||||
var value = _innerReader.ReadAsBytes();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
public override decimal? ReadAsDecimal()
|
||||
{
|
||||
var value = _innerReader.ReadAsDecimal();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
public override DateTime? ReadAsDateTime()
|
||||
{
|
||||
var value = _innerReader.ReadAsDateTime();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
public override DateTimeOffset? ReadAsDateTimeOffset()
|
||||
{
|
||||
var value = _innerReader.ReadAsDateTimeOffset();
|
||||
_textWriter.WriteToken(_innerReader, false, false);
|
||||
_textWriter.WriteToken(_innerReader, false, false, true);
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user