-Examples!

This commit is contained in:
James Newton-King
2013-01-28 00:07:21 +13:00
parent fd7970a3c3
commit 70ace8d527
115 changed files with 1120 additions and 210 deletions
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o1 = new JObject
{
{"String", "A string!"},
@@ -41,6 +42,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(Object.ReferenceEquals(o1, o2));
// false
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class CreateJsonAnonymousObject
{
#region Types
public class Post
{
public string Title { get; set; }
@@ -15,9 +16,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public string Link { get; set; }
public IList<string> Categories { get; set; }
}
#endregion
public void Example()
{
#region Usage
List<Post> posts = new List<Post>
{
new Post
@@ -72,6 +75,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// ]
// }
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = new JObject
{
{"Cpu", "Intel"},
@@ -32,6 +33,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "SSD"
// ]
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class CreateJsonDeclaratively
{
#region Types
public class Post
{
public string Title { get; set; }
@@ -15,13 +16,16 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public string Link { get; set; }
public IList<string> Categories { get; set; }
}
#endregion
private List<Post> GetPosts()
{
return null;
}
public void Example()
{
#region Usage
List<Post> posts = GetPosts();
JObject rss =
@@ -73,6 +77,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// ]
// }
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
@@ -30,6 +31,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "OnSale"
// ]
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JTokenWriter writer = new JTokenWriter();
writer.WriteStartObject();
writer.WritePropertyName("name1");
@@ -31,6 +32,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// 2
// ]
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JArray array = new JArray();
JValue text = new JValue("Manual text");
JValue date = new JValue(new DateTime(2000, 5, 23));
@@ -17,13 +18,19 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
array.Add(text);
array.Add(date);
string json = array.ToString();
JObject o = new JObject();
o["MyArray"] = array;
string json = o.ToString();
Console.WriteLine(json);
// [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// {
// "MyArray": [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = new JObject
{
{"Cpu", "Intel"},
@@ -44,6 +45,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// String - SSD
// EndArray
// EndObject
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = new JObject
{
{"name1", "value1"},
@@ -32,6 +33,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// 2
// ]
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JValue s1 = new JValue("A string");
JValue s2 = new JValue("A string");
JValue s3 = new JValue("A STRING");
@@ -39,6 +40,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(JToken.DeepEquals(s1, o1["String"]));
// true
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class DeserializeWithLinq
{
#region Types
public class BlogPost
{
public string Title { get; set; }
@@ -17,9 +18,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public string Body { get; set; }
public DateTime PostedDate { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"[
{
'Title': 'Json.NET is awesome!',
@@ -47,6 +50,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(blogPosts[0].Body);
// <h3>Title!</h3>
// <p>Content!</p>
#endregion
}
}
}
@@ -8,15 +8,18 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class FromObject
{
#region Types
public class Computer
{
public string Cpu { get; set; }
public int Memory { get; set; }
public IList<string> Drives { get; set; }
}
#endregion
public void Example()
{
#region Usage
JValue i = (JValue)JToken.FromObject(12345);
Console.WriteLine(i.Type);
@@ -61,6 +64,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "DVD",
// "SSD"
// ]
#endregion
}
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = new JObject
{
{ "name1", "value1" },
@@ -29,6 +30,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
}
// name1 - value1
// name2 - value2
#endregion
}
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JValue v1 = new JValue("1");
int i = (int)v1;
@@ -50,6 +51,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine((dt != null) ? dt.ToString() : "{null}");
// {null}
#endregion
}
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JValue s = new JValue("A string value");
Console.WriteLine(s.Value.GetType().Name);
@@ -23,6 +24,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// Uri
Console.WriteLine(u.Value);
// http://www.google.com/
#endregion
}
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"{
'channel': {
'title': 'Star Wars',
@@ -48,6 +49,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// ]
// }
// }
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JToken t1 = JToken.Parse("{}");
Console.WriteLine(t1.Type);
@@ -29,6 +30,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(t4.Type);
// String
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"[
'Small',
'Medium',
@@ -24,6 +25,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "Medium",
// "Large"
// ]
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"{
CPU: 'Intel',
Drives: [
@@ -28,6 +29,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "500 gigabyte hard drive"
// ]
// }
#endregion
}
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"{
'channel': {
'title': 'James Newton-King',
@@ -62,6 +63,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(string.Join(", ", categoriesText));
// Json.NET, CodePlex
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"[
{
'Title': 'Json.NET is awesome!',
@@ -29,16 +30,20 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
dynamic blogPost = blogPosts[0];
string title = blogPost.Title;
Console.WriteLine(title);
// Json.NET is awesome!
string author = blogPost.Author.Name;
Console.WriteLine(author);
// James Newton-King
DateTime postDate = blogPost.Date;
Console.WriteLine(postDate);
// 23/01/2013 7:30:00 p.m.
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
string json = @"{
'channel': {
'title': 'James Newton-King',
@@ -48,7 +49,6 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
Console.WriteLine(item);
}
//LINQ to JSON beta
//Json.NET 1.3 + New license + Now on CodePlex
@@ -62,10 +62,10 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
Console.WriteLine(c.Category + " - Count: " + c.Count);
}
//Json.NET - Count: 2
//LINQ - Count: 1
//CodePlex - Count: 1
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
@@ -55,6 +56,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(productName);
// Elbow Grease
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = JObject.Parse(@"{
'Stores': [
'Lambton Quay',
@@ -55,6 +56,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(totalPrice);
// 149.95
#endregion
}
}
}
}
@@ -12,6 +12,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
byte[] data = Convert.FromBase64String("KQAAAAJuYW1lMQAHAAAAdmFsdWUxAAJuYW1lMgAHAAAAdmFsdWUyAAA=");
MemoryStream ms = new MemoryStream(data);
@@ -25,6 +26,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(value);
// value1
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o1 = JObject.Parse(File.ReadAllText(@"c:\videogames.json"));
// read JSON directly from a file
@@ -19,6 +20,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
JObject o2 = (JObject) JToken.ReadFrom(reader);
}
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class SerializeWithLinq
{
#region Types
public class BlogPost
{
public string Title { get; set; }
@@ -17,9 +18,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public string Body { get; set; }
public DateTime PostedDate { get; set; }
}
#endregion
public void Example()
{
#region Usage
IList<BlogPost> blogPosts = new List<BlogPost>
{
new BlogPost
@@ -29,7 +32,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
AuthorTwitter = "JamesNK",
PostedDate = new DateTime(2013, 1, 23, 19, 30, 0),
Body = @"<h3>Title!</h3>
<p>Content!</p>"
<p>Content!</p>"
}
};
@@ -61,6 +64,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
// "BodyHtml": "&lt;h3&gt;Title!&lt;/h3&gt;\r\n&lt;p&gt;Content!&lt;/p&gt;"
// }
// ]
#endregion
}
}
}
}
@@ -8,13 +8,16 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public class ToObjectComplex
{
#region Types
public class Person
{
public string Name { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"{
'd': [
{
@@ -37,6 +40,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(person[1].Name);
// Mike Smith
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JValue v1 = new JValue(true);
bool b = v1.ToObject<bool>();
@@ -26,6 +27,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(s);
// "True"
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JValue v1 = new JValue(true);
bool b = (bool)v1.ToObject(typeof(bool));
@@ -26,6 +27,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(s);
// "True"
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = JObject.Parse(@"{'string1':'value','integer2':99,'datetime3':'2000-05-23T00:00:00'}");
Console.WriteLine(o.ToString());
@@ -25,6 +26,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(o.ToString(Formatting.None, new JavaScriptDateTimeConverter()));
// {"string1":"value","integer2":99,"datetime3":new Date(959032800000)}
#endregion
}
}
}
@@ -11,10 +11,12 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = JObject.Parse(@"{'string1':'value','integer2':99,'datetime3':'2000-05-23T00:00:00'}");
Console.WriteLine(o.ToString(Formatting.None, new JavaScriptDateTimeConverter()));
// {"string1":"value","integer2":99,"datetime3":new Date(959032800000)}
#endregion
}
}
}
@@ -12,6 +12,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject o = new JObject
{
{"name1","value1"},
@@ -28,6 +29,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(data);
// KQAAAAJuYW1lMQAHAAAAdmFsdWUxAAJuYW1lMgAHAAAAdmFsdWUyAAA=
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
public void Example()
{
#region Usage
JObject videogameRatings = new JObject(
new JProperty("Halo", 9),
new JProperty("Starcraft", 9),
@@ -24,6 +25,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
videogameRatings.WriteTo(writer);
}
#endregion
}
}
}