-Documentation

This commit is contained in:
James Newton-King
2014-03-31 16:01:47 +13:00
parent 6965fcfa46
commit 69d16ddfa1
6 changed files with 278 additions and 280 deletions
@@ -56,16 +56,16 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region SerializeXmlNode #region SerializeXmlNode
string xml = @"<?xml version='1.0' standalone='no'?> string xml = @"<?xml version='1.0' standalone='no'?>
<root> <root>
<person id='1'> <person id='1'>
<name>Alan</name> <name>Alan</name>
<url>http://www.google.com</url> <url>http://www.google.com</url>
</person> </person>
<person id='2'> <person id='2'>
<name>Louis</name> <name>Louis</name>
<url>http://www.yahoo.com</url> <url>http://www.yahoo.com</url>
</person> </person>
</root>"; </root>";
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
doc.LoadXml(xml); doc.LoadXml(xml);
@@ -98,25 +98,25 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region DeserializeXmlNode #region DeserializeXmlNode
string json = @"{ string json = @"{
'?xml': { '?xml': {
'@version': '1.0', '@version': '1.0',
'@standalone': 'no' '@standalone': 'no'
}, },
'root': { 'root': {
'person': [ 'person': [
{ {
'@id': '1', '@id': '1',
'name': 'Alan', 'name': 'Alan',
'url': 'http://www.google.com' 'url': 'http://www.google.com'
}, },
{ {
'@id': '2', '@id': '2',
'name': 'Louis', 'name': 'Louis',
'url': 'http://www.yahoo.com' 'url': 'http://www.yahoo.com'
} }
] ]
} }
}"; }";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?> // <?xml version="1.0" standalone="no"?>
@@ -179,5 +179,4 @@ namespace Newtonsoft.Json.Tests.Documentation
} }
} }
} }
#endif #endif
@@ -11,97 +11,97 @@ namespace Newtonsoft.Json.Tests.Documentation
public void CreatingJson() public void CreatingJson()
{ {
#if NETFX_CORE #if NETFX_CORE
#region CreatingJSON #region CreatingJSON
// Windows.Data.Json // Windows.Data.Json
// ----------------- // -----------------
JsonObject jsonObject = new JsonObject JsonObject jsonObject = new JsonObject
{ {
{"CPU", JsonValue.CreateStringValue("Intel")}, {"CPU", JsonValue.CreateStringValue("Intel")},
{ {
"Drives", new JsonArray "Drives", new JsonArray
{ {
JsonValue.CreateStringValue("DVD read/writer"), JsonValue.CreateStringValue("DVD read/writer"),
JsonValue.CreateStringValue("500 gigabyte hard drive") JsonValue.CreateStringValue("500 gigabyte hard drive")
} }
} }
}; };
string json1 = jsonObject.Stringify(); string json1 = jsonObject.Stringify();
// LINQ to JSON // LINQ to JSON
// ------------ // ------------
JObject jObject = new JObject JObject jObject = new JObject
{ {
{"CPU", "Intel"}, {"CPU", "Intel"},
{ {
"Drives", new JArray "Drives", new JArray
{ {
"DVD read/writer", "DVD read/writer",
"500 gigabyte hard drive" "500 gigabyte hard drive"
} }
} }
}; };
string json2 = jObject.ToString(); string json2 = jObject.ToString();
#endregion #endregion
#endif #endif
} }
public void QueryingJson() public void QueryingJson()
{ {
#if NETFX_CORE #if NETFX_CORE
#region QueryingJSON #region QueryingJSON
string json = @"{ string json = @"{
'channel': { 'channel': {
'title': 'James Newton-King', 'title': 'James Newton-King',
'link': 'http://james.newtonking.com', 'link': 'http://james.newtonking.com',
'description': 'James Newton-King's blog.', 'description': 'James Newton-King's blog.',
'item': [ 'item': [
{ {
'title': 'Json.NET 1.3 + New license + Now on CodePlex', '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', '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', 'link': 'http://james.newtonking.com/projects/json-net.aspx',
'category': [ 'category': [
'Json.NET', 'Json.NET',
'CodePlex' 'CodePlex'
] ]
} }
] ]
} }
}"; }";
// Windows.Data.Json // Windows.Data.Json
// ----------------- // -----------------
JsonObject jsonObject = JsonObject.Parse(json); JsonObject jsonObject = JsonObject.Parse(json);
string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString(); string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
// LINQ to JSON // LINQ to JSON
// ------------ // ------------
JObject jObject = JObject.Parse(json); JObject jObject = JObject.Parse(json);
string itemTitle2 = (string)jObject["channel"]["item"][0]["title"]; string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
#endregion #endregion
#endif #endif
} }
public void Converting() public void Converting()
{ {
#if NETFX_CORE #if NETFX_CORE
#region Converting #region Converting
JsonObject jsonObject = new JsonObject JsonObject jsonObject = new JsonObject
{ {
{"CPU", JsonValue.CreateStringValue("Intel")}, {"CPU", JsonValue.CreateStringValue("Intel")},
{"Drives", new JsonArray { {"Drives", new JsonArray {
JsonValue.CreateStringValue("DVD read/writer"), JsonValue.CreateStringValue("DVD read/writer"),
JsonValue.CreateStringValue("500 gigabyte hard drive") JsonValue.CreateStringValue("500 gigabyte hard drive")
} }
} }
}; };
// convert Windows.Data.Json to LINQ to JSON // convert Windows.Data.Json to LINQ to JSON
JObject o = JObject.FromObject(jsonObject); JObject o = JObject.FromObject(jsonObject);
// convert LINQ to JSON to Windows.Data.Json // convert LINQ to JSON to Windows.Data.Json
JArray a = (JArray)o["Drives"]; JArray a = (JArray)o["Drives"];
JsonArray jsonArray = a.ToObject<JsonArray>(); JsonArray jsonArray = a.ToObject<JsonArray>();
#endregion #endregion
#endif #endif
} }
} }
@@ -84,25 +84,25 @@ namespace Newtonsoft.Json.Tests.Documentation
public void IsValidMessages() public void IsValidMessages()
{ {
string schemaJson = @"{ string schemaJson = @"{
'description': 'A person', 'description': 'A person',
'type': 'object', 'type': 'object',
'properties': 'properties':
{ {
'name': {'type':'string'}, 'name': {'type':'string'},
'hobbies': { 'hobbies': {
'type': 'array', 'type': 'array',
'items': {'type':'string'} 'items': {'type':'string'}
} }
} }
}"; }";
#region IsValidMessages #region IsValidMessages
JsonSchema schema = JsonSchema.Parse(schemaJson); JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(@"{ JObject person = JObject.Parse(@"{
'name': null, 'name': null,
'hobbies': ['Invalid content', 0.123456789] 'hobbies': ['Invalid content', 0.123456789]
}"); }");
IList<string> messages; IList<string> messages;
bool valid = person.IsValid(schema, out messages); bool valid = person.IsValid(schema, out messages);
@@ -118,9 +118,9 @@ namespace Newtonsoft.Json.Tests.Documentation
#region JsonValidatingReader #region JsonValidatingReader
string json = @"{ string json = @"{
'name': 'James', 'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}"; }";
JsonTextReader reader = new JsonTextReader(new StringReader(json)); JsonTextReader reader = new JsonTextReader(new StringReader(json));
@@ -169,9 +169,9 @@ namespace Newtonsoft.Json.Tests.Documentation
}; };
JObject person = JObject.Parse(@"{ JObject person = JObject.Parse(@"{
'name': 'James', 'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}"); }");
bool valid = person.IsValid(schema); bool valid = person.IsValid(schema);
// true // true
@@ -79,12 +79,12 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region LinqToJsonBasic #region LinqToJsonBasic
JObject o = JObject.Parse(@"{ JObject o = JObject.Parse(@"{
'CPU': 'Intel', 'CPU': 'Intel',
'Drives': [ 'Drives': [
'DVD read/writer', 'DVD read/writer',
'500 gigabyte hard drive' '500 gigabyte hard drive'
] ]
}"); }");
string cpu = (string)o["CPU"]; string cpu = (string)o["CPU"];
// Intel // Intel
@@ -217,12 +217,12 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region LinqToJsonCreateParse #region LinqToJsonCreateParse
string json = @"{ string json = @"{
CPU: 'Intel', CPU: 'Intel',
Drives: [ Drives: [
'DVD read/writer', 'DVD read/writer',
'500 gigabyte hard drive' '500 gigabyte hard drive'
] ]
}"; }";
JObject o = JObject.Parse(json); JObject o = JObject.Parse(json);
#endregion #endregion
@@ -232,10 +232,10 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region LinqToJsonCreateParseArray #region LinqToJsonCreateParseArray
string json = @"[ string json = @"[
'Small', 'Small',
'Medium', 'Medium',
'Large' 'Large'
]"; ]";
JArray a = JArray.Parse(json); JArray a = JArray.Parse(json);
#endregion #endregion
@@ -256,32 +256,32 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region LinqToJsonSimpleQuerying #region LinqToJsonSimpleQuerying
string json = @"{ string json = @"{
'channel': { 'channel': {
'title': 'James Newton-King', 'title': 'James Newton-King',
'link': 'http://james.newtonking.com', 'link': 'http://james.newtonking.com',
'description': 'James Newton-King's blog.', 'description': 'James Newton-King's blog.',
'item': [ 'item': [
{ {
'title': 'Json.NET 1.3 + New license + Now on CodePlex', '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', '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', 'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [ 'categories': [
'Json.NET', 'Json.NET',
'CodePlex' 'CodePlex'
] ]
}, },
{ {
'title': 'LINQ to JSON beta', 'title': 'LINQ to JSON beta',
'description': 'Annoucing LINQ to JSON', 'description': 'Annoucing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx', 'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [ 'categories': [
'Json.NET', 'Json.NET',
'LINQ' 'LINQ'
] ]
} }
] ]
} }
}"; }";
JObject rss = JObject.Parse(json); JObject rss = JObject.Parse(json);
@@ -398,35 +398,35 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region SelectTokenComplex #region SelectTokenComplex
JObject o = JObject.Parse(@"{ JObject o = JObject.Parse(@"{
'Stores': [ 'Stores': [
'Lambton Quay', 'Lambton Quay',
'Willis Street' 'Willis Street'
], ],
'Manufacturers': [ 'Manufacturers': [
{ {
'Name': 'Acme Co', 'Name': 'Acme Co',
'Products': [ 'Products': [
{ {
'Name': 'Anvil', 'Name': 'Anvil',
'Price': 50 'Price': 50
} }
] ]
}, },
{ {
'Name': 'Contoso', 'Name': 'Contoso',
'Products': [ 'Products': [
{ {
'Name': 'Elbow Grease', 'Name': 'Elbow Grease',
'Price': 99.95 'Price': 99.95
}, },
{ {
'Name': 'Headlight Fluid', 'Name': 'Headlight Fluid',
'Price': 4 'Price': 4
} }
] ]
} }
] ]
}"); }");
string name = (string)o.SelectToken("Manufacturers[0].Name"); string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co // Acme Co
@@ -95,15 +95,15 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region ReadingJsonText #region ReadingJsonText
string json = @"{ string json = @"{
'CPU': 'Intel', 'CPU': 'Intel',
'PSU': '500W', 'PSU': '500W',
'Drives': [ 'Drives': [
'DVD read/writer' 'DVD read/writer'
/*(broken)*/, /*(broken)*/,
'500 gigabyte hard drive', '500 gigabyte hard drive',
'200 gigabype hard drive' '200 gigabype hard drive'
] ]
}"; }";
JsonTextReader reader = new JsonTextReader(new StringReader(json)); JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read()) while (reader.Read())
@@ -220,15 +220,15 @@ namespace Newtonsoft.Json.Tests.Documentation
List<string> errors = new List<string>(); List<string> errors = new List<string>();
List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[ List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
'2009-09-09T00:00:00Z', '2009-09-09T00:00:00Z',
'I am not a date and will error!', 'I am not a date and will error!',
[ [
1 1
], ],
'1977-02-20T00:00:00Z', '1977-02-20T00:00:00Z',
null, null,
'2000-12-01T00:00:00Z' '2000-12-01T00:00:00Z'
]", ]",
new JsonSerializerSettings new JsonSerializerSettings
{ {
Error = delegate(object sender, ErrorEventArgs args) Error = delegate(object sender, ErrorEventArgs args)
@@ -533,19 +533,19 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region SerializingCollectionsDeserializing #region SerializingCollectionsDeserializing
string json = @"[ string json = @"[
{ {
'Name': 'Product 1', 'Name': 'Product 1',
'ExpiryDate': '2000-12-29T00:00Z', 'ExpiryDate': '2000-12-29T00:00Z',
'Price': 99.95, 'Price': 99.95,
'Sizes': null 'Sizes': null
}, },
{ {
'Name': 'Product 2', 'Name': 'Product 2',
'ExpiryDate': '2009-07-31T00:00Z', 'ExpiryDate': '2009-07-31T00:00Z',
'Price': 12.50, 'Price': 12.50,
'Sizes': null 'Sizes': null
} }
]"; ]";
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json); List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);
@@ -814,56 +814,56 @@ namespace Newtonsoft.Json.Tests.Documentation
{ {
#region SerializingPartialJsonFragmentsExample #region SerializingPartialJsonFragmentsExample
string googleSearchText = @"{ string googleSearchText = @"{
'responseData': { 'responseData': {
'results': [ 'results': [
{ {
'GsearchResultClass': 'GwebSearch', 'GsearchResultClass': 'GwebSearch',
'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton', 'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton',
'url': 'http://en.wikipedia.org/wiki/Paris_Hilton', 'url': 'http://en.wikipedia.org/wiki/Paris_Hilton',
'visibleUrl': 'en.wikipedia.org', 'visibleUrl': 'en.wikipedia.org',
'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org', 'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org',
'title': '<b>Paris Hilton</b> - Wikipedia, the free encyclopedia', 'title': '<b>Paris Hilton</b> - Wikipedia, the free encyclopedia',
'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia', 'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia',
'content': '[1] In 2006, she released her debut album...' 'content': '[1] In 2006, she released her debut album...'
}, },
{ {
'GsearchResultClass': 'GwebSearch', 'GsearchResultClass': 'GwebSearch',
'unescapedUrl': 'http://www.imdb.com/name/nm0385296/', 'unescapedUrl': 'http://www.imdb.com/name/nm0385296/',
'url': 'http://www.imdb.com/name/nm0385296/', 'url': 'http://www.imdb.com/name/nm0385296/',
'visibleUrl': 'www.imdb.com', 'visibleUrl': 'www.imdb.com',
'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com', 'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com',
'title': '<b>Paris Hilton</b>', 'title': '<b>Paris Hilton</b>',
'titleNoFormatting': 'Paris Hilton', 'titleNoFormatting': 'Paris Hilton',
'content': 'Self: Zoolander. Socialite <b>Paris Hilton</b>...' 'content': 'Self: Zoolander. Socialite <b>Paris Hilton</b>...'
} }
], ],
'cursor': { 'cursor': {
'pages': [ 'pages': [
{ {
'start': '0', 'start': '0',
'label': 1 '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...'
}
}, },
{ 'responseDetails': null,
'start': '4', 'responseStatus': 200
'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
}";
JObject googleSearch = JObject.Parse(googleSearchText); JObject googleSearch = JObject.Parse(googleSearchText);
@@ -913,5 +913,4 @@ namespace Newtonsoft.Json.Tests.Documentation
} }
} }
} }
#endif #endif