diff --git a/Doc/CreatingLINQtoJSON.aml b/Doc/CreatingLINQtoJSON.aml new file mode 100644 index 00000000..55fe758e --- /dev/null +++ b/Doc/CreatingLINQtoJSON.aml @@ -0,0 +1,50 @@ + + + + + As well as parsing JSON from existing JSON strings, LINQ to JSON objects can be created from scratch to create new JSON structures. + + +
+ Manually Creating JSON + + Setting values and creating objects and arrays one at a time gives you + total control but it is more verbose than other options. + + + +
+ +
+ Creating JSON with LINQ + + Declaratively creating JSON objects using LINQ is a fast way to create JSON from collections of values. + + + +
+ +
+ Creating JSON from an object + + The last option is to create a JSON object from a non-JSON type using the + Overload:Newtonsoft.Json.Linq.JObject.FromObject + method. Internally FromObject will use the JsonSerializer to serialize the object to LINQ to JSON objects instead of text. + + The example below is creating a JSON object from an anonymous object but any + .NET type can be used with FromObject to create JSON. + + + +
+ + + LINQ to JSON + LINQtoJSON.htm + _self + + + Overload:Newtonsoft.Json.Linq.JObject.FromObject + +
+
\ No newline at end of file diff --git a/Doc/LINQtoJSON.aml b/Doc/LINQtoJSON.aml index faba2f45..806d1027 100644 --- a/Doc/LINQtoJSON.aml +++ b/Doc/LINQtoJSON.aml @@ -2,86 +2,58 @@ - LINQ to JSON is a programming API for working with JSON objects. + LINQ to JSON is an API for working with JSON objects. The API has been designed with LINQ in mind to enable to quick querying and creation of JSON objects. LINQ to JSON sits under the N:Newtonsoft.Json.Linq namespace. + - -
- Reading JSON - - JSON values can be read from a string use the - M:Newtonsoft.Json.Linq.JToken.Parse(System.String) - method. - - - - -
-
- Creating JSON - - - There are a number of different options when it comes to creating - JSON using LINQ to JSON. - - The first option is to set values one at a time. You - have total control but it is more verbose than other options. - - - - Another option is to create JSON objects declaratively using LINQ. - - - - Finally you can create a JSON object from a non-JSON type using the - Overload:Newtonsoft.Json.Linq.JObject.FromObject - method. The example below is creating a JSON object from an anonymous object but any - .NET type can be used with FromObject to create JSON. - - - -
-
- Querying JSON - - - The properties and methods that are the most useful when querying JSON - objects are - M:Newtonsoft.Json.Linq.JToken.Children - and - P:Newtonsoft.Json.Linq.JToken.Item(System.Object). - - Children returns all the children of that object. If it is a - JObject it will return a collection of properties to work with and if - it is a JArray you will get a collection of the array's values. - - The index property is used to get a specific child, either by - index position for JSON arrays or property name for JSON objects. - - - - LINQ to JSON can also be used to manually convert from JSON to a .NET object. - - - - Manually serializing and deserializing between .NET objects is most useful when - working with JSON that doesn't closely match your .NET objects. - - - -
+
+ Topics + + Select a topic below for more information: + + + + + Parsing JSON + ParsingLINQtoJSON.htm + _self + + + + + + + Creating JSON + CreatingLINQtoJSON.htm + _self + + + + + + + Querying JSON with LINQ + QueryingLINQtoJSON.htm + _self + + + + + + + Querying JSON with SelectToken + SelectToken.htm + _self + + + + + +
- - Querying LINQ to JSON with SelectToken - SelectToken.htm - _self - - T:Newtonsoft.Json.Linq.JObject T:Newtonsoft.Json.Linq.JArray T:Newtonsoft.Json.Linq.JValue diff --git a/Doc/ParsingLINQtoJSON.aml b/Doc/ParsingLINQtoJSON.aml new file mode 100644 index 00000000..8a2efc11 --- /dev/null +++ b/Doc/ParsingLINQtoJSON.aml @@ -0,0 +1,37 @@ + + + + + LINQ to JSON has methods available for parsing JSON from a string or loading JSON directly from a file. + + +
+ Parsing JSON text + + JSON values can be read from a string using + M:Newtonsoft.Json.Linq.JToken.Parse(System.String). + + + + +
+ +
+ Loading JSON from a file + + JSON can also be loaded directly from a file using M:Newtonsoft.Json.Linq.JToken.ReadFrom(Newtonsoft.Json.JsonReader). + + +
+ + + LINQ to JSON + LINQtoJSON.htm + _self + + + M:Newtonsoft.Json.Linq.JToken.Parse(System.String) + M:Newtonsoft.Json.Linq.JToken.ReadFrom(Newtonsoft.Json.JsonReader) + +
+
\ No newline at end of file diff --git a/Doc/QueryingLINQtoJSON.aml b/Doc/QueryingLINQtoJSON.aml new file mode 100644 index 00000000..bf2f8710 --- /dev/null +++ b/Doc/QueryingLINQtoJSON.aml @@ -0,0 +1,60 @@ + + + + + LINQ to JSON provides a number of methods for getting data from its objects. The index methods on JObject/JArray let you quickly get data by its property name + on an object or index in a collection, while M:Newtonsoft.Json.Linq.JToken.Children lets you get ranges + of data as IEnumerable<JToken> to then query using LINQ. + + +
+ Getting values by Property Name or Collection Index + + + The simplest way to get a value from LINQ to JSON is to use the + P:Newtonsoft.Json.Linq.JToken.Item(System.Object) index on + JObject/JArray and then cast the returned T:Newtonsoft.Json.Linq.JValue to the type you want. + + + + + +
+
+ Querying with LINQ + + JObject/JArray can also be queried using LINQ. M:Newtonsoft.Json.Linq.JToken.Children + returns the children values of a JObject/JArray + as an IEnumerable<JToken> that can then be queried with the standard Where/OrderBy/Select LINQ operators. + + + M:Newtonsoft.Json.Linq.JToken.Children returns all the children of a token. If it is a + JObject it will return a collection of properties to work with and if + it is a JArray you will get a collection of the array's values. + + + + + LINQ to JSON can also be used to manually convert JSON to a .NET object. + + + + Manually serializing and deserializing between .NET objects is useful when you are + working with JSON that doesn't closely match your .NET objects. + + + +
+ + + LINQ to JSON + LINQtoJSON.htm + _self + + + P:Newtonsoft.Json.Linq.JToken.Item(System.Object) + M:Newtonsoft.Json.Linq.JToken.Children + +
+
\ No newline at end of file diff --git a/Doc/doc.content b/Doc/doc.content index ce0c7870..b1d8418b 100644 --- a/Doc/doc.content +++ b/Doc/doc.content @@ -1,7 +1,7 @@  - + @@ -16,8 +16,11 @@ - + + + + - + \ No newline at end of file diff --git a/Doc/doc.shfbproj b/Doc/doc.shfbproj index 4a9ea64c..3cd7e6b9 100644 --- a/Doc/doc.shfbproj +++ b/Doc/doc.shfbproj @@ -140,6 +140,9 @@ + + + diff --git a/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs index 75bb7b6d..58170868 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs @@ -46,11 +46,35 @@ using Newtonsoft.Json.Tests.TestObjects; using Newtonsoft.Json.Utilities; using System.Globalization; using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; +using File = System.IO.File; namespace Newtonsoft.Json.Tests.Documentation { public class LinqToJsonTests { + public void LinqToJsonBasic() + { + #region LinqToJsonBasic + JObject o = JObject.Parse(@"{ + 'CPU': 'Intel', + 'Drives': [ + 'DVD read/writer', + '500 gigabyte hard drive' + ] + }"); + + string cpu = (string)o["CPU"]; + // Intel + + string firstDrive = (string)o["Drives"][0]; + // DVD read/writer + + IList allDrives = o["Drives"].Select(t => (string)t).ToList(); + // DVD read/writer + // 500 gigabyte hard drive + #endregion + } + public void LinqToJsonCreateNormal() { #region LinqToJsonCreateNormal @@ -194,6 +218,73 @@ namespace Newtonsoft.Json.Tests.Documentation #endregion } + public static class File + { + public static StreamReader OpenText(string path) + { + return null; + } + } + + public void LinqToJsonReadObject() + { + #region LinqToJsonReadObject + using (StreamReader reader = File.OpenText(@"c:\person.json")) + { + JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); + // do stuff + } + #endregion + } + + public void LinqToJsonSimpleQuerying() + { + #region LinqToJsonSimpleQuerying + string json = @"{ + 'channel': { + 'title': 'James Newton-King', + 'link': 'http://james.newtonking.com', + 'description': 'James Newton-King's blog.', + 'item': [ + { + 'title': 'Json.NET 1.3 + New license + Now on CodePlex', + 'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex', + 'link': 'http://james.newtonking.com/projects/json-net.aspx', + 'categories': [ + 'Json.NET', + 'CodePlex' + ] + }, + { + 'title': 'LINQ to JSON beta', + 'description': 'Annoucing LINQ to JSON', + 'link': 'http://james.newtonking.com/projects/json-net.aspx', + 'categories': [ + 'Json.NET', + 'LINQ' + ] + } + ] + } + }"; + + JObject rss = JObject.Parse(json); + + string rssTitle = (string)rss["channel"]["title"]; + // James Newton-King + + string itemTitle = (string)rss["channel"]["item"][0]["title"]; + // Json.NET 1.3 + New license + Now on CodePlex + + JArray categories = (JArray)rss["channel"]["item"][0]["categories"]; + // ["Json.NET", "CodePlex"] + + IList categoriesText = categories.Select(c => (string)c).ToList(); + // Json.NET + // CodePlex + #endregion + } + public void LinqToJsonQuerying() { JObject rss = new JObject(); diff --git a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs index 2964b6e7..2b82d61f 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs @@ -882,6 +882,30 @@ namespace Newtonsoft.Json.Tests.Documentation // Url = http://www.imdb.com/name/nm0385296/ #endregion } + + public void SerializeMultidimensionalArrayExample() + { + string[,] famousCouples = new string[,] + { + {"Adam", "Eve"}, + {"Bonnie", "Clyde"}, + {"Donald", "Daisy"}, + {"Han", "Leia"} + }; + + string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented); + // [ + // ["Adam", "Eve"], + // ["Bonnie", "Clyde"], + // ["Donald", "Daisy"], + // ["Han", "Leia"] + // ] + + string[,] deserialized = JsonConvert.DeserializeObject(json); + + Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]); + // Han, Leia + } } } #endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index d692480f..872d284b 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -65,8 +65,8 @@ namespace Newtonsoft.Json.Tests [TestFixture] public class PerformanceTests : TestFixtureBase { - private const int Iterations = 100; - //private const int Iterations = 5000; + //private const int Iterations = 100; + private const int Iterations = 5000; #region Data @@ -768,6 +768,27 @@ namespace Newtonsoft.Json.Tests BenchmarkSerializeMethod(SerializeMethod.JsonNet, s); } + [Test] + public void ParseJObject() + { + Stopwatch timer = new Stopwatch(); + timer.Start(); + for (int i = 0; i < 100000; i++) + { + JObject o = JObject.Parse(@"{ + ""CPU"": ""Intel"", + ""Drives"": [ + ""DVD read/writer"", + ""500 gigabyte hard drive"" + ] +}"); + } + timer.Stop(); + + string linq = timer.Elapsed.TotalSeconds.ToString(); + Console.WriteLine(linq); + } + [Test] public void JObjectToString() { @@ -778,7 +799,7 @@ namespace Newtonsoft.Json.Tests for (int i = 0; i < Iterations; i++) { test["dummy"] = new JValue(i); - Encoding.UTF8.GetBytes(test.ToString(Formatting.None)); + test.ToString(Formatting.None); } return null; }, "JObject.ToString"); diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index a43ab900..0bfd0208 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -72,5 +72,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.8.15205")] +[assembly: AssemblyFileVersion("4.5.8.15212")] #endif diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 157ea283..c577628e 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -58,7 +58,6 @@ namespace Newtonsoft.Json.Linq #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) internal ListChangedEventHandler _listChanged; internal AddingNewEventHandler _addingNew; - internal NotifyCollectionChangedEventHandler _collectionChanged; /// /// Occurs when the list changes or an item in the list changes. @@ -79,6 +78,8 @@ namespace Newtonsoft.Json.Linq } #endif #if SILVERLIGHT || !(NET20 || NET35 || PORTABLE) + internal NotifyCollectionChangedEventHandler _collectionChanged; + /// /// Occurs when the items list of the collection has changed, or the collection is reset. /// diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index bcfabf8c..06b63dfc 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -91,7 +91,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.8.15205")] +[assembly: AssemblyFileVersion("4.5.8.15212")] #endif [assembly: CLSCompliant(true)]