From 2bf31b960510614cca27937b91bd9e707f5a101a Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 6 Oct 2015 22:17:25 +1300 Subject: [PATCH] -Added JsonLoadSettings -Added CommentsHandling -Changed LINQ to JSON load to ignore array comments by default --- Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs | 16 +++ .../Linq/JObjectTests.cs | 16 +++ Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs | 16 +++ .../Linq/LinqToJsonTest.cs | 53 +++++++- .../Serialization/JsonSerializerTest.cs | 7 +- Src/Newtonsoft.Json/JsonWriter.cs | 19 ++- Src/Newtonsoft.Json/Linq/CommentHandling.cs | 17 +++ Src/Newtonsoft.Json/Linq/JArray.cs | 31 ++++- Src/Newtonsoft.Json/Linq/JConstructor.cs | 14 +- Src/Newtonsoft.Json/Linq/JContainer.cs | 15 ++- Src/Newtonsoft.Json/Linq/JObject.cs | 31 ++++- Src/Newtonsoft.Json/Linq/JProperty.cs | 14 +- Src/Newtonsoft.Json/Linq/JToken.cs | 127 +++++++++++++----- Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs | 30 +++++ .../Newtonsoft.Json.Net20.csproj | 2 + .../Newtonsoft.Json.Net35.csproj | 2 + .../Newtonsoft.Json.Net40.csproj | 2 + .../Newtonsoft.Json.Portable.csproj | 2 + .../Newtonsoft.Json.Portable40.csproj | 2 + Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 2 + .../JsonSerializerInternalReader.cs | 2 +- .../Serialization/TraceJsonReader.cs | 14 +- 22 files changed, 366 insertions(+), 68 deletions(-) create mode 100644 Src/Newtonsoft.Json/Linq/CommentHandling.cs create mode 100644 Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index 59b65bf0..f88846c8 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -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]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index fc918d7e..10a0ad9e 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -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]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index c7f70af0..eafe0c8d 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -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]); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 2817d6d1..c9dfffa2 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -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() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 371dda65..4e52ea21 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -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 */}"; diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index b8706efc..80a32cd3 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -434,7 +434,7 @@ namespace Newtonsoft.Json /// The to read the token from. public void WriteToken(JsonReader reader) { - WriteToken(reader, true, true); + WriteToken(reader, true, true, true); } /// @@ -446,7 +446,7 @@ namespace Newtonsoft.Json { ValidationUtils.ArgumentNotNull(reader, "reader"); - WriteToken(reader, writeChildren, true); + WriteToken(reader, writeChildren, true, true); } /// @@ -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) diff --git a/Src/Newtonsoft.Json/Linq/CommentHandling.cs b/Src/Newtonsoft.Json/Linq/CommentHandling.cs new file mode 100644 index 00000000..26baee20 --- /dev/null +++ b/Src/Newtonsoft.Json/Linq/CommentHandling.cs @@ -0,0 +1,17 @@ +namespace Newtonsoft.Json.Linq +{ + /// + /// Specifies how JSON comments are handled when loading JSON. + /// + public enum CommentHandling + { + /// + /// Ignore comments. + /// + Ignore = 0, + /// + /// Load comments as a with type . + /// + Load = 1 + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index b347eab9..07b364ba 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -111,6 +111,18 @@ namespace Newtonsoft.Json.Linq /// A that will be read for the content of the . /// A that contains the JSON that was read from the specified . public new static JArray Load(JsonReader reader) + { + return Load(reader, null); + } + + /// + /// Loads an from a . + /// + /// A that will be read for the content of the . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A that contains the JSON that was read from the specified . + 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 /// /// public new static JArray Parse(string json) + { + return Parse(json, null); + } + + /// + /// Load a from a string that contains JSON. + /// + /// A that contains JSON. + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A populated from the string that contains JSON. + /// + /// + /// + 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."); diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index b267393a..112b14a0 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -192,6 +192,18 @@ namespace Newtonsoft.Json.Linq /// A that will be read for the content of the . /// A that contains the JSON that was read from the specified . public new static JConstructor Load(JsonReader reader) + { + return Load(reader, null); + } + + /// + /// Loads an from a . + /// + /// A that will be read for the content of the . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A that contains the JSON that was read from the specified . + 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; } diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index f99d88d3..9345c81d 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -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(); diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index a7b354cd..2d903ba2 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -322,6 +322,18 @@ namespace Newtonsoft.Json.Linq /// A that will be read for the content of the . /// A that contains the JSON that was read from the specified . public new static JObject Load(JsonReader reader) + { + return Load(reader, null); + } + + /// + /// Loads an from a . + /// + /// A that will be read for the content of the . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A that contains the JSON that was read from the specified . + 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 /// /// public new static JObject Parse(string json) + { + return Parse(json, null); + } + + /// + /// Load a from a string that contains JSON. + /// + /// A that contains JSON. + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A populated from the string that contains JSON. + /// + /// + /// + 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."); diff --git a/Src/Newtonsoft.Json/Linq/JProperty.cs b/Src/Newtonsoft.Json/Linq/JProperty.cs index ab277605..a6168b68 100644 --- a/Src/Newtonsoft.Json/Linq/JProperty.cs +++ b/Src/Newtonsoft.Json/Linq/JProperty.cs @@ -329,6 +329,18 @@ namespace Newtonsoft.Json.Linq /// A that will be read for the content of the . /// A that contains the JSON that was read from the specified . public new static JProperty Load(JsonReader reader) + { + return Load(reader, null); + } + + /// + /// Loads an from a . + /// + /// A that will be read for the content of the . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A that contains the JSON that was read from the specified . + 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; } diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 61abfa91..2b5b4e15 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -1848,6 +1848,22 @@ namespace Newtonsoft.Json.Linq /// by the token type of the first token encountered in the reader. /// public static JToken ReadFrom(JsonReader reader) + { + return ReadFrom(reader, null); + } + + /// + /// Creates a from a . + /// + /// An positioned at the token to read into this . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// + /// An 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. + /// + 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 /// A that contains JSON. /// A populated from the string that contains JSON. public static JToken Parse(string json) + { + return Parse(json, null); + } + + /// + /// Load a from a string that contains JSON. + /// + /// A that contains JSON. + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// A populated from the string that contains JSON. + 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 } } + /// + /// Creates a from a . + /// + /// An positioned at the token to read into this . + /// The used to load the JSON. + /// If this is null, default load settings will be used. + /// + /// An 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. + /// + public static JToken Load(JsonReader reader, JsonLoadSettings settings) + { + return ReadFrom(reader, settings); + } + /// /// Creates a from a . /// @@ -1924,7 +1979,7 @@ namespace Newtonsoft.Json.Linq /// public static JToken Load(JsonReader reader) { - return ReadFrom(reader); + return Load(reader, null); } internal void SetLineInfo(IJsonLineInfo lineInfo) diff --git a/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs b/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs new file mode 100644 index 00000000..b3170260 --- /dev/null +++ b/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs @@ -0,0 +1,30 @@ +using System; + +namespace Newtonsoft.Json.Linq +{ + /// + /// Specifies the settings used when loading JSON. + /// + public class JsonLoadSettings + { + private CommentHandling _commentHandling; + + /// + /// Gets or sets how JSON comments are handled when loading JSON. + /// + /// The JSON comment handling. + public CommentHandling CommentHandling + { + get { return _commentHandling; } + set + { + if (value < CommentHandling.Ignore || value > CommentHandling.Load) + { + throw new ArgumentOutOfRangeException("value"); + } + + _commentHandling = value; + } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj index c06f2f41..6cb2e837 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj @@ -109,7 +109,9 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 598a763a..5cad145e 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -122,8 +122,10 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj index 6da9dd65..6faff383 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj @@ -123,7 +123,9 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj index 13cd89d0..8cec877a 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj @@ -104,6 +104,7 @@ + @@ -115,6 +116,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj index 2bb9aa2e..8119592e 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj @@ -109,6 +109,7 @@ + @@ -120,6 +121,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index e6cbf0a8..422963fe 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -108,6 +108,7 @@ + @@ -136,6 +137,7 @@ + diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 3487ea12..28124a7e 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.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) { diff --git a/Src/Newtonsoft.Json/Serialization/TraceJsonReader.cs b/Src/Newtonsoft.Json/Serialization/TraceJsonReader.cs index 79a49c9f..e363540e 100644 --- a/Src/Newtonsoft.Json/Serialization/TraceJsonReader.cs +++ b/Src/Newtonsoft.Json/Serialization/TraceJsonReader.cs @@ -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