From 69d16ddfa1ae3afef85ab0c3cdec40bab24ca47a Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 31 Mar 2014 16:01:47 +1300 Subject: [PATCH] -Documentation --- .../ConvertingJsonAndXmlTests.cs | 59 ++++--- .../JsonNetVsWindowsDataJsonTests.cs | 156 +++++++++--------- .../Documentation/JsonSchemaTests.cs | 40 ++--- .../Documentation/LinqToJsonTests.cs | 142 ++++++++-------- .../ReadingAndWritingJsonTests.cs | 18 +- .../Documentation/SerializationTests.cs | 143 ++++++++-------- 6 files changed, 278 insertions(+), 280 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Documentation/ConvertingJsonAndXmlTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/ConvertingJsonAndXmlTests.cs index 9104cb3d..f4beae83 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/ConvertingJsonAndXmlTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/ConvertingJsonAndXmlTests.cs @@ -56,16 +56,16 @@ namespace Newtonsoft.Json.Tests.Documentation { #region SerializeXmlNode string xml = @" - - - Alan - http://www.google.com - - - Louis - http://www.yahoo.com - - "; + + + Alan + http://www.google.com + + + Louis + http://www.yahoo.com + + "; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); @@ -98,25 +98,25 @@ namespace Newtonsoft.Json.Tests.Documentation { #region DeserializeXmlNode string json = @"{ - '?xml': { - '@version': '1.0', - '@standalone': 'no' - }, - 'root': { - 'person': [ - { - '@id': '1', - 'name': 'Alan', - 'url': 'http://www.google.com' - }, - { - '@id': '2', - 'name': 'Louis', - 'url': 'http://www.yahoo.com' - } - ] - } - }"; + '?xml': { + '@version': '1.0', + '@standalone': 'no' + }, + 'root': { + 'person': [ + { + '@id': '1', + 'name': 'Alan', + 'url': 'http://www.google.com' + }, + { + '@id': '2', + 'name': 'Louis', + 'url': 'http://www.yahoo.com' + } + ] + } + }"; XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); // @@ -179,5 +179,4 @@ namespace Newtonsoft.Json.Tests.Documentation } } } - #endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/JsonNetVsWindowsDataJsonTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/JsonNetVsWindowsDataJsonTests.cs index eb70e0e9..2a262cac 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/JsonNetVsWindowsDataJsonTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/JsonNetVsWindowsDataJsonTests.cs @@ -11,97 +11,97 @@ namespace Newtonsoft.Json.Tests.Documentation public void CreatingJson() { #if NETFX_CORE - #region CreatingJSON - // Windows.Data.Json - // ----------------- - JsonObject jsonObject = new JsonObject - { - {"CPU", JsonValue.CreateStringValue("Intel")}, - { - "Drives", new JsonArray - { - JsonValue.CreateStringValue("DVD read/writer"), - JsonValue.CreateStringValue("500 gigabyte hard drive") - } - } - }; - string json1 = jsonObject.Stringify(); + #region CreatingJSON + // Windows.Data.Json + // ----------------- + JsonObject jsonObject = new JsonObject + { + {"CPU", JsonValue.CreateStringValue("Intel")}, + { + "Drives", new JsonArray + { + JsonValue.CreateStringValue("DVD read/writer"), + JsonValue.CreateStringValue("500 gigabyte hard drive") + } + } + }; + string json1 = jsonObject.Stringify(); - // LINQ to JSON - // ------------ - JObject jObject = new JObject - { - {"CPU", "Intel"}, - { - "Drives", new JArray - { - "DVD read/writer", - "500 gigabyte hard drive" - } - } - }; - string json2 = jObject.ToString(); - #endregion + // LINQ to JSON + // ------------ + JObject jObject = new JObject + { + {"CPU", "Intel"}, + { + "Drives", new JArray + { + "DVD read/writer", + "500 gigabyte hard drive" + } + } + }; + string json2 = jObject.ToString(); + #endregion #endif } public void QueryingJson() { #if NETFX_CORE - #region QueryingJSON - 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 on CodePlex', - 'link': 'http://james.newtonking.com/projects/json-net.aspx', - 'category': [ - 'Json.NET', - 'CodePlex' - ] - } - ] - } - }"; - - // Windows.Data.Json - // ----------------- - JsonObject jsonObject = JsonObject.Parse(json); - string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString(); - - // LINQ to JSON - // ------------ - JObject jObject = JObject.Parse(json); - string itemTitle2 = (string)jObject["channel"]["item"][0]["title"]; - #endregion + #region QueryingJSON + 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 on CodePlex', + 'link': 'http://james.newtonking.com/projects/json-net.aspx', + 'category': [ + 'Json.NET', + 'CodePlex' + ] + } + ] + } + }"; + + // Windows.Data.Json + // ----------------- + JsonObject jsonObject = JsonObject.Parse(json); + string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString(); + + // LINQ to JSON + // ------------ + JObject jObject = JObject.Parse(json); + string itemTitle2 = (string)jObject["channel"]["item"][0]["title"]; + #endregion #endif } public void Converting() { #if NETFX_CORE - #region Converting - JsonObject jsonObject = new JsonObject - { - {"CPU", JsonValue.CreateStringValue("Intel")}, - {"Drives", new JsonArray { - JsonValue.CreateStringValue("DVD read/writer"), - JsonValue.CreateStringValue("500 gigabyte hard drive") - } - } - }; - - // convert Windows.Data.Json to LINQ to JSON - JObject o = JObject.FromObject(jsonObject); - - // convert LINQ to JSON to Windows.Data.Json - JArray a = (JArray)o["Drives"]; - JsonArray jsonArray = a.ToObject(); - #endregion + #region Converting + JsonObject jsonObject = new JsonObject + { + {"CPU", JsonValue.CreateStringValue("Intel")}, + {"Drives", new JsonArray { + JsonValue.CreateStringValue("DVD read/writer"), + JsonValue.CreateStringValue("500 gigabyte hard drive") + } + } + }; + + // convert Windows.Data.Json to LINQ to JSON + JObject o = JObject.FromObject(jsonObject); + + // convert LINQ to JSON to Windows.Data.Json + JArray a = (JArray)o["Drives"]; + JsonArray jsonArray = a.ToObject(); + #endregion #endif } } diff --git a/Src/Newtonsoft.Json.Tests/Documentation/JsonSchemaTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/JsonSchemaTests.cs index 29b50e6c..e86bceb8 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/JsonSchemaTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/JsonSchemaTests.cs @@ -84,25 +84,25 @@ namespace Newtonsoft.Json.Tests.Documentation public void IsValidMessages() { string schemaJson = @"{ - 'description': 'A person', - 'type': 'object', - 'properties': - { - 'name': {'type':'string'}, - 'hobbies': { - 'type': 'array', - 'items': {'type':'string'} - } - } - }"; + 'description': 'A person', + 'type': 'object', + 'properties': + { + 'name': {'type':'string'}, + 'hobbies': { + 'type': 'array', + 'items': {'type':'string'} + } + } + }"; #region IsValidMessages JsonSchema schema = JsonSchema.Parse(schemaJson); JObject person = JObject.Parse(@"{ - 'name': null, - 'hobbies': ['Invalid content', 0.123456789] - }"); + 'name': null, + 'hobbies': ['Invalid content', 0.123456789] + }"); IList messages; bool valid = person.IsValid(schema, out messages); @@ -118,9 +118,9 @@ namespace Newtonsoft.Json.Tests.Documentation #region JsonValidatingReader string json = @"{ - 'name': 'James', - 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] - }"; + 'name': 'James', + 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + }"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); @@ -169,9 +169,9 @@ namespace Newtonsoft.Json.Tests.Documentation }; JObject person = JObject.Parse(@"{ - 'name': 'James', - 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] - }"); + 'name': 'James', + 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + }"); bool valid = person.IsValid(schema); // true diff --git a/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs index ce4926ba..bbd6893c 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/LinqToJsonTests.cs @@ -79,12 +79,12 @@ namespace Newtonsoft.Json.Tests.Documentation { #region LinqToJsonBasic JObject o = JObject.Parse(@"{ - 'CPU': 'Intel', - 'Drives': [ - 'DVD read/writer', - '500 gigabyte hard drive' - ] - }"); + 'CPU': 'Intel', + 'Drives': [ + 'DVD read/writer', + '500 gigabyte hard drive' + ] + }"); string cpu = (string)o["CPU"]; // Intel @@ -217,12 +217,12 @@ namespace Newtonsoft.Json.Tests.Documentation { #region LinqToJsonCreateParse string json = @"{ - CPU: 'Intel', - Drives: [ - 'DVD read/writer', - '500 gigabyte hard drive' - ] - }"; + CPU: 'Intel', + Drives: [ + 'DVD read/writer', + '500 gigabyte hard drive' + ] + }"; JObject o = JObject.Parse(json); #endregion @@ -232,10 +232,10 @@ namespace Newtonsoft.Json.Tests.Documentation { #region LinqToJsonCreateParseArray string json = @"[ - 'Small', - 'Medium', - 'Large' - ]"; + 'Small', + 'Medium', + 'Large' + ]"; JArray a = JArray.Parse(json); #endregion @@ -256,32 +256,32 @@ namespace Newtonsoft.Json.Tests.Documentation { #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 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' - ] - } - ] - } - }"; + '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 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); @@ -398,35 +398,35 @@ namespace Newtonsoft.Json.Tests.Documentation { #region SelectTokenComplex JObject o = JObject.Parse(@"{ - 'Stores': [ - 'Lambton Quay', - 'Willis Street' - ], - 'Manufacturers': [ - { - 'Name': 'Acme Co', - 'Products': [ - { - 'Name': 'Anvil', - 'Price': 50 - } - ] - }, - { - 'Name': 'Contoso', - 'Products': [ - { - 'Name': 'Elbow Grease', - 'Price': 99.95 - }, - { - 'Name': 'Headlight Fluid', - 'Price': 4 - } - ] - } - ] - }"); + 'Stores': [ + 'Lambton Quay', + 'Willis Street' + ], + 'Manufacturers': [ + { + 'Name': 'Acme Co', + 'Products': [ + { + 'Name': 'Anvil', + 'Price': 50 + } + ] + }, + { + 'Name': 'Contoso', + 'Products': [ + { + 'Name': 'Elbow Grease', + 'Price': 99.95 + }, + { + 'Name': 'Headlight Fluid', + 'Price': 4 + } + ] + } + ] + }"); string name = (string)o.SelectToken("Manufacturers[0].Name"); // Acme Co diff --git a/Src/Newtonsoft.Json.Tests/Documentation/ReadingAndWritingJsonTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/ReadingAndWritingJsonTests.cs index acda4b68..664aa0a6 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/ReadingAndWritingJsonTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/ReadingAndWritingJsonTests.cs @@ -95,15 +95,15 @@ namespace Newtonsoft.Json.Tests.Documentation { #region ReadingJsonText string json = @"{ - 'CPU': 'Intel', - 'PSU': '500W', - 'Drives': [ - 'DVD read/writer' - /*(broken)*/, - '500 gigabyte hard drive', - '200 gigabype hard drive' - ] - }"; + 'CPU': 'Intel', + 'PSU': '500W', + 'Drives': [ + 'DVD read/writer' + /*(broken)*/, + '500 gigabyte hard drive', + '200 gigabype hard drive' + ] + }"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) diff --git a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs index 31d4f25b..c728f0ad 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs @@ -220,15 +220,15 @@ namespace Newtonsoft.Json.Tests.Documentation List errors = new List(); List c = JsonConvert.DeserializeObject>(@"[ - '2009-09-09T00:00:00Z', - 'I am not a date and will error!', - [ - 1 - ], - '1977-02-20T00:00:00Z', - null, - '2000-12-01T00:00:00Z' - ]", + '2009-09-09T00:00:00Z', + 'I am not a date and will error!', + [ + 1 + ], + '1977-02-20T00:00:00Z', + null, + '2000-12-01T00:00:00Z' + ]", new JsonSerializerSettings { Error = delegate(object sender, ErrorEventArgs args) @@ -533,19 +533,19 @@ namespace Newtonsoft.Json.Tests.Documentation { #region SerializingCollectionsDeserializing string json = @"[ - { - 'Name': 'Product 1', - 'ExpiryDate': '2000-12-29T00:00Z', - 'Price': 99.95, - 'Sizes': null - }, - { - 'Name': 'Product 2', - 'ExpiryDate': '2009-07-31T00:00Z', - 'Price': 12.50, - 'Sizes': null - } - ]"; + { + 'Name': 'Product 1', + 'ExpiryDate': '2000-12-29T00:00Z', + 'Price': 99.95, + 'Sizes': null + }, + { + 'Name': 'Product 2', + 'ExpiryDate': '2009-07-31T00:00Z', + 'Price': 12.50, + 'Sizes': null + } + ]"; List products = JsonConvert.DeserializeObject>(json); @@ -814,56 +814,56 @@ namespace Newtonsoft.Json.Tests.Documentation { #region SerializingPartialJsonFragmentsExample string googleSearchText = @"{ - 'responseData': { - 'results': [ - { - 'GsearchResultClass': 'GwebSearch', - 'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton', - 'url': 'http://en.wikipedia.org/wiki/Paris_Hilton', - 'visibleUrl': 'en.wikipedia.org', - 'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org', - 'title': 'Paris Hilton - Wikipedia, the free encyclopedia', - 'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia', - 'content': '[1] In 2006, she released her debut album...' - }, - { - 'GsearchResultClass': 'GwebSearch', - 'unescapedUrl': 'http://www.imdb.com/name/nm0385296/', - 'url': 'http://www.imdb.com/name/nm0385296/', - 'visibleUrl': 'www.imdb.com', - 'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com', - 'title': 'Paris Hilton', - 'titleNoFormatting': 'Paris Hilton', - 'content': 'Self: Zoolander. Socialite Paris Hilton...' - } - ], - 'cursor': { - 'pages': [ - { - 'start': '0', - 'label': 1 + 'responseData': { + 'results': [ + { + 'GsearchResultClass': 'GwebSearch', + 'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton', + 'url': 'http://en.wikipedia.org/wiki/Paris_Hilton', + 'visibleUrl': 'en.wikipedia.org', + 'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org', + 'title': 'Paris Hilton - Wikipedia, the free encyclopedia', + 'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia', + 'content': '[1] In 2006, she released her debut album...' + }, + { + 'GsearchResultClass': 'GwebSearch', + 'unescapedUrl': 'http://www.imdb.com/name/nm0385296/', + 'url': 'http://www.imdb.com/name/nm0385296/', + 'visibleUrl': 'www.imdb.com', + 'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com', + 'title': 'Paris Hilton', + 'titleNoFormatting': 'Paris Hilton', + 'content': 'Self: Zoolander. Socialite Paris Hilton...' + } + ], + 'cursor': { + 'pages': [ + { + 'start': '0', + 'label': 1 + }, + { + 'start': '4', + 'label': 2 + }, + { + 'start': '8', + 'label': 3 + }, + { + 'start': '12', + 'label': 4 + } + ], + 'estimatedResultCount': '59600000', + 'currentPageIndex': 0, + 'moreResultsUrl': 'http://www.google.com/search?oe=utf8&ie=utf8...' + } }, - { - 'start': '4', - 'label': 2 - }, - { - 'start': '8', - 'label': 3 - }, - { - 'start': '12', - 'label': 4 - } - ], - 'estimatedResultCount': '59600000', - 'currentPageIndex': 0, - 'moreResultsUrl': 'http://www.google.com/search?oe=utf8&ie=utf8...' - } - }, - 'responseDetails': null, - 'responseStatus': 200 - }"; + 'responseDetails': null, + 'responseStatus': 200 + }"; JObject googleSearch = JObject.Parse(googleSearchText); @@ -913,5 +913,4 @@ namespace Newtonsoft.Json.Tests.Documentation } } } - #endif \ No newline at end of file