-Code reformat
This commit is contained in:
@@ -50,35 +50,35 @@ using File = System.IO.File;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
public static class File
|
||||
{
|
||||
public static StreamReader OpenText(string path)
|
||||
public static class File
|
||||
{
|
||||
return null;
|
||||
public static StreamReader OpenText(string path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static StreamWriter CreateText(string path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void WriteAllText(string path, string contents)
|
||||
{
|
||||
Console.WriteLine(contents);
|
||||
}
|
||||
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static StreamWriter CreateText(string path)
|
||||
public class LinqToJsonTests
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void WriteAllText(string path, string contents)
|
||||
{
|
||||
Console.WriteLine(contents);
|
||||
}
|
||||
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class LinqToJsonTests
|
||||
{
|
||||
public void LinqToJsonBasic()
|
||||
{
|
||||
#region LinqToJsonBasic
|
||||
JObject o = JObject.Parse(@"{
|
||||
public void LinqToJsonBasic()
|
||||
{
|
||||
#region LinqToJsonBasic
|
||||
JObject o = JObject.Parse(@"{
|
||||
'CPU': 'Intel',
|
||||
'Drives': [
|
||||
'DVD read/writer',
|
||||
@@ -86,137 +86,137 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
]
|
||||
}");
|
||||
|
||||
string cpu = (string)o["CPU"];
|
||||
// Intel
|
||||
string cpu = (string)o["CPU"];
|
||||
// Intel
|
||||
|
||||
string firstDrive = (string)o["Drives"][0];
|
||||
// DVD read/writer
|
||||
string firstDrive = (string)o["Drives"][0];
|
||||
// DVD read/writer
|
||||
|
||||
IList<string> allDrives = o["Drives"].Select(t => (string)t).ToList();
|
||||
// DVD read/writer
|
||||
// 500 gigabyte hard drive
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateNormal()
|
||||
{
|
||||
#region LinqToJsonCreateNormal
|
||||
JArray array = new JArray();
|
||||
JValue text = new JValue("Manual text");
|
||||
JValue date = new JValue(new DateTime(2000, 5, 23));
|
||||
|
||||
array.Add(text);
|
||||
array.Add(date);
|
||||
|
||||
string json = array.ToString();
|
||||
// [
|
||||
// "Manual text",
|
||||
// "2000-05-23T00:00:00"
|
||||
// ]
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class Post
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Link { get; set; }
|
||||
public IList<string> Categories { get; set; }
|
||||
}
|
||||
|
||||
private List<Post> GetPosts()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateDeclaratively()
|
||||
{
|
||||
#region LinqToJsonCreateDeclaratively
|
||||
List<Post> posts = GetPosts();
|
||||
|
||||
JObject rss =
|
||||
new JObject(
|
||||
new JProperty("channel",
|
||||
new JObject(
|
||||
new JProperty("title", "James Newton-King"),
|
||||
new JProperty("link", "http://james.newtonking.com"),
|
||||
new JProperty("description", "James Newton-King's blog."),
|
||||
new JProperty("item",
|
||||
new JArray(
|
||||
from p in posts
|
||||
orderby p.Title
|
||||
select new JObject(
|
||||
new JProperty("title", p.Title),
|
||||
new JProperty("description", p.Description),
|
||||
new JProperty("link", p.Link),
|
||||
new JProperty("category",
|
||||
new JArray(
|
||||
from c in p.Categories
|
||||
select new JValue(c)))))))));
|
||||
|
||||
Console.WriteLine(rss.ToString());
|
||||
|
||||
//{
|
||||
// "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 being available on CodePlex",
|
||||
// "link": "http://james.newtonking.com/projects/json-net.aspx",
|
||||
// "category": [
|
||||
// "Json.NET",
|
||||
// "CodePlex"
|
||||
// ]
|
||||
// },
|
||||
// {
|
||||
// "title": "LINQ to JSON beta",
|
||||
// "description": "Annoucing LINQ to JSON",
|
||||
// "link": "http://james.newtonking.com/projects/json-net.aspx",
|
||||
// "category": [
|
||||
// "Json.NET",
|
||||
// "LINQ"
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateFromObject()
|
||||
{
|
||||
List<Post> posts = null;
|
||||
|
||||
#region LinqToJsonCreateFromObject
|
||||
JObject o = JObject.FromObject(new
|
||||
{
|
||||
channel = new
|
||||
{
|
||||
title = "James Newton-King",
|
||||
link = "http://james.newtonking.com",
|
||||
description = "James Newton-King's blog.",
|
||||
item =
|
||||
from p in posts
|
||||
orderby p.Title
|
||||
select new
|
||||
{
|
||||
title = p.Title,
|
||||
description = p.Description,
|
||||
link = p.Link,
|
||||
category = p.Categories
|
||||
}
|
||||
IList<string> allDrives = o["Drives"].Select(t => (string)t).ToList();
|
||||
// DVD read/writer
|
||||
// 500 gigabyte hard drive
|
||||
#endregion
|
||||
}
|
||||
});
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateParse()
|
||||
{
|
||||
#region LinqToJsonCreateParse
|
||||
string json = @"{
|
||||
public void LinqToJsonCreateNormal()
|
||||
{
|
||||
#region LinqToJsonCreateNormal
|
||||
JArray array = new JArray();
|
||||
JValue text = new JValue("Manual text");
|
||||
JValue date = new JValue(new DateTime(2000, 5, 23));
|
||||
|
||||
array.Add(text);
|
||||
array.Add(date);
|
||||
|
||||
string json = array.ToString();
|
||||
// [
|
||||
// "Manual text",
|
||||
// "2000-05-23T00:00:00"
|
||||
// ]
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class Post
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Link { get; set; }
|
||||
public IList<string> Categories { get; set; }
|
||||
}
|
||||
|
||||
private List<Post> GetPosts()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateDeclaratively()
|
||||
{
|
||||
#region LinqToJsonCreateDeclaratively
|
||||
List<Post> posts = GetPosts();
|
||||
|
||||
JObject rss =
|
||||
new JObject(
|
||||
new JProperty("channel",
|
||||
new JObject(
|
||||
new JProperty("title", "James Newton-King"),
|
||||
new JProperty("link", "http://james.newtonking.com"),
|
||||
new JProperty("description", "James Newton-King's blog."),
|
||||
new JProperty("item",
|
||||
new JArray(
|
||||
from p in posts
|
||||
orderby p.Title
|
||||
select new JObject(
|
||||
new JProperty("title", p.Title),
|
||||
new JProperty("description", p.Description),
|
||||
new JProperty("link", p.Link),
|
||||
new JProperty("category",
|
||||
new JArray(
|
||||
from c in p.Categories
|
||||
select new JValue(c)))))))));
|
||||
|
||||
Console.WriteLine(rss.ToString());
|
||||
|
||||
//{
|
||||
// "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 being available on CodePlex",
|
||||
// "link": "http://james.newtonking.com/projects/json-net.aspx",
|
||||
// "category": [
|
||||
// "Json.NET",
|
||||
// "CodePlex"
|
||||
// ]
|
||||
// },
|
||||
// {
|
||||
// "title": "LINQ to JSON beta",
|
||||
// "description": "Annoucing LINQ to JSON",
|
||||
// "link": "http://james.newtonking.com/projects/json-net.aspx",
|
||||
// "category": [
|
||||
// "Json.NET",
|
||||
// "LINQ"
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateFromObject()
|
||||
{
|
||||
List<Post> posts = null;
|
||||
|
||||
#region LinqToJsonCreateFromObject
|
||||
JObject o = JObject.FromObject(new
|
||||
{
|
||||
channel = new
|
||||
{
|
||||
title = "James Newton-King",
|
||||
link = "http://james.newtonking.com",
|
||||
description = "James Newton-King's blog.",
|
||||
item =
|
||||
from p in posts
|
||||
orderby p.Title
|
||||
select new
|
||||
{
|
||||
title = p.Title,
|
||||
description = p.Description,
|
||||
link = p.Link,
|
||||
category = p.Categories
|
||||
}
|
||||
}
|
||||
});
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateParse()
|
||||
{
|
||||
#region LinqToJsonCreateParse
|
||||
string json = @"{
|
||||
CPU: 'Intel',
|
||||
Drives: [
|
||||
'DVD read/writer',
|
||||
@@ -224,38 +224,38 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
]
|
||||
}";
|
||||
|
||||
JObject o = JObject.Parse(json);
|
||||
#endregion
|
||||
}
|
||||
JObject o = JObject.Parse(json);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonCreateParseArray()
|
||||
{
|
||||
#region LinqToJsonCreateParseArray
|
||||
string json = @"[
|
||||
public void LinqToJsonCreateParseArray()
|
||||
{
|
||||
#region LinqToJsonCreateParseArray
|
||||
string json = @"[
|
||||
'Small',
|
||||
'Medium',
|
||||
'Large'
|
||||
]";
|
||||
|
||||
JArray a = JArray.Parse(json);
|
||||
#endregion
|
||||
}
|
||||
JArray a = JArray.Parse(json);
|
||||
#endregion
|
||||
}
|
||||
|
||||
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 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 = @"{
|
||||
public void LinqToJsonSimpleQuerying()
|
||||
{
|
||||
#region LinqToJsonSimpleQuerying
|
||||
string json = @"{
|
||||
'channel': {
|
||||
'title': 'James Newton-King',
|
||||
'link': 'http://james.newtonking.com',
|
||||
@@ -283,120 +283,121 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
}
|
||||
}";
|
||||
|
||||
JObject rss = JObject.Parse(json);
|
||||
JObject rss = JObject.Parse(json);
|
||||
|
||||
string rssTitle = (string)rss["channel"]["title"];
|
||||
// James Newton-King
|
||||
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
|
||||
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"]
|
||||
JArray categories = (JArray)rss["channel"]["item"][0]["categories"];
|
||||
// ["Json.NET", "CodePlex"]
|
||||
|
||||
IList<string> categoriesText = categories.Select(c => (string)c).ToList();
|
||||
// Json.NET
|
||||
// CodePlex
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LinqToJsonQuerying()
|
||||
{
|
||||
JObject rss = new JObject();
|
||||
|
||||
#region LinqToJsonQuerying
|
||||
var postTitles =
|
||||
from p in rss["channel"]["item"]
|
||||
select (string)p["title"];
|
||||
|
||||
foreach (var item in postTitles)
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
|
||||
//LINQ to JSON beta
|
||||
//Json.NET 1.3 + New license + Now on CodePlex
|
||||
|
||||
var categories =
|
||||
from c in rss["channel"]["item"].Children()["category"].Values<string>()
|
||||
group c by c into g
|
||||
orderby g.Count() descending
|
||||
select new { Category = g.Key, Count = g.Count() };
|
||||
|
||||
foreach (var c in categories)
|
||||
{
|
||||
Console.WriteLine(c.Category + " - Count: " + c.Count);
|
||||
}
|
||||
|
||||
//Json.NET - Count: 2
|
||||
//LINQ - Count: 1
|
||||
//CodePlex - Count: 1
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region LinqToJsonDeserializeObject
|
||||
public class Shortie
|
||||
{
|
||||
public string Original { get; set; }
|
||||
public string Shortened { get; set; }
|
||||
public string Short { get; set; }
|
||||
public ShortieException Error { get; set; }
|
||||
}
|
||||
|
||||
public class ShortieException
|
||||
{
|
||||
public int Code { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void LinqToJsonDeserializeExample()
|
||||
{
|
||||
#region LinqToJsonDeserializeExample
|
||||
string jsonText = @"{
|
||||
'short': {
|
||||
'original': 'http://www.foo.com/',
|
||||
'short': 'krehqk',
|
||||
'error': {
|
||||
'code':0,
|
||||
'msg':'No action taken'
|
||||
}
|
||||
}";
|
||||
|
||||
JObject json = JObject.Parse(jsonText);
|
||||
|
||||
Shortie shortie = new Shortie
|
||||
{
|
||||
Original = (string)json["short"]["original"],
|
||||
Short = (string)json["short"]["short"],
|
||||
Error = new ShortieException
|
||||
{
|
||||
Code = (int)json["short"]["error"]["code"],
|
||||
ErrorMessage = (string)json["short"]["error"]["msg"]
|
||||
IList<string> categoriesText = categories.Select(c => (string)c).ToList();
|
||||
// Json.NET
|
||||
// CodePlex
|
||||
#endregion
|
||||
}
|
||||
};
|
||||
|
||||
Console.WriteLine(shortie.Original);
|
||||
// http://www.foo.com/
|
||||
public void LinqToJsonQuerying()
|
||||
{
|
||||
JObject rss = new JObject();
|
||||
|
||||
Console.WriteLine(shortie.Error.ErrorMessage);
|
||||
// No action taken
|
||||
#endregion
|
||||
}
|
||||
#region LinqToJsonQuerying
|
||||
var postTitles =
|
||||
from p in rss["channel"]["item"]
|
||||
select (string)p["title"];
|
||||
|
||||
public void SelectTokenSimple()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
foreach (var item in postTitles)
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
|
||||
#region SelectTokenSimple
|
||||
string name = (string)o.SelectToken("Manufacturers[0].Name");
|
||||
#endregion
|
||||
}
|
||||
//LINQ to JSON beta
|
||||
//Json.NET 1.3 + New license + Now on CodePlex
|
||||
|
||||
public void SelectTokenComplex()
|
||||
{
|
||||
#region SelectTokenComplex
|
||||
JObject o = JObject.Parse(@"{
|
||||
var categories =
|
||||
from c in rss["channel"]["item"].Children()["category"].Values<string>()
|
||||
group c by c
|
||||
into g
|
||||
orderby g.Count() descending
|
||||
select new { Category = g.Key, Count = g.Count() };
|
||||
|
||||
foreach (var c in categories)
|
||||
{
|
||||
Console.WriteLine(c.Category + " - Count: " + c.Count);
|
||||
}
|
||||
|
||||
//Json.NET - Count: 2
|
||||
//LINQ - Count: 1
|
||||
//CodePlex - Count: 1
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region LinqToJsonDeserializeObject
|
||||
public class Shortie
|
||||
{
|
||||
public string Original { get; set; }
|
||||
public string Shortened { get; set; }
|
||||
public string Short { get; set; }
|
||||
public ShortieException Error { get; set; }
|
||||
}
|
||||
|
||||
public class ShortieException
|
||||
{
|
||||
public int Code { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void LinqToJsonDeserializeExample()
|
||||
{
|
||||
#region LinqToJsonDeserializeExample
|
||||
string jsonText = @"{
|
||||
'short': {
|
||||
'original': 'http://www.foo.com/',
|
||||
'short': 'krehqk',
|
||||
'error': {
|
||||
'code':0,
|
||||
'msg':'No action taken'
|
||||
}
|
||||
}";
|
||||
|
||||
JObject json = JObject.Parse(jsonText);
|
||||
|
||||
Shortie shortie = new Shortie
|
||||
{
|
||||
Original = (string)json["short"]["original"],
|
||||
Short = (string)json["short"]["short"],
|
||||
Error = new ShortieException
|
||||
{
|
||||
Code = (int)json["short"]["error"]["code"],
|
||||
ErrorMessage = (string)json["short"]["error"]["msg"]
|
||||
}
|
||||
};
|
||||
|
||||
Console.WriteLine(shortie.Original);
|
||||
// http://www.foo.com/
|
||||
|
||||
Console.WriteLine(shortie.Error.ErrorMessage);
|
||||
// No action taken
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void SelectTokenSimple()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
#region SelectTokenSimple
|
||||
string name = (string)o.SelectToken("Manufacturers[0].Name");
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void SelectTokenComplex()
|
||||
{
|
||||
#region SelectTokenComplex
|
||||
JObject o = JObject.Parse(@"{
|
||||
'Stores': [
|
||||
'Lambton Quay',
|
||||
'Willis Street'
|
||||
@@ -427,34 +428,35 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
]
|
||||
}");
|
||||
|
||||
string name = (string)o.SelectToken("Manufacturers[0].Name");
|
||||
// Acme Co
|
||||
string name = (string)o.SelectToken("Manufacturers[0].Name");
|
||||
// Acme Co
|
||||
|
||||
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
|
||||
// 50
|
||||
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
|
||||
// 50
|
||||
|
||||
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
|
||||
// Elbow Grease
|
||||
#endregion
|
||||
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
|
||||
// Elbow Grease
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void SelectTokenLinq()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
#region SelectTokenLinq
|
||||
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
|
||||
// Lambton Quay
|
||||
// Willis Street
|
||||
|
||||
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
|
||||
// null
|
||||
// Headlight Fluid
|
||||
|
||||
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
|
||||
// 149.95
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectTokenLinq()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
#region SelectTokenLinq
|
||||
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
|
||||
// Lambton Quay
|
||||
// Willis Street
|
||||
|
||||
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
|
||||
// null
|
||||
// Headlight Fluid
|
||||
|
||||
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
|
||||
// 149.95
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user