-Examples!

This commit is contained in:
James Newton-King
2013-01-26 14:46:05 +13:00
parent 406762b187
commit fd7970a3c3
113 changed files with 1279 additions and 101 deletions
@@ -22,14 +22,14 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
string json = @"[
{
""Title"": ""Json.NET is awesome!"",
""Author"": {
""Name"": ""James Newton-King"",
""Twitter"": ""@JamesNK"",
""Picture"": ""/jamesnk.png""
'Title': 'Json.NET is awesome!',
'Author': {
'Name': 'James Newton-King',
'Twitter': '@JamesNK',
'Picture': '/jamesnk.png'
},
""Date"": ""2013-01-23T19:30:00"",
""BodyHtml"": ""<h3>Title!</h3>\r\n<p>Content!</p>""
'Date': '2013-01-23T19:30:00',
'BodyHtml': '<h3>Title!</h3>\r\n<p>Content!</p>'
}
]";
@@ -11,12 +11,12 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public void Example()
{
string json = @"{
""channel"": {
""title"": ""Star Wars"",
""link"": ""http://www.starwars.com"",
""description"": ""Star Wars blog."",
""obsolete"": ""Obsolete value"",
""item"": []
'channel': {
'title': 'Star Wars',
'link': 'http://www.starwars.com',
'description': 'Star Wars blog.',
'obsolete': 'Obsolete value',
'item': []
}
}";
@@ -25,7 +25,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
Console.WriteLine(t3.Type);
// Null
JToken t4 = JToken.Parse(@"""A string!""");
JToken t4 = JToken.Parse(@"'A string!'");
Console.WriteLine(t4.Type);
// String
@@ -13,14 +13,14 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
{
string json = @"[
{
""Title"": ""Json.NET is awesome!"",
""Author"": {
""Name"": ""James Newton-King"",
""Twitter"": ""@JamesNK"",
""Picture"": ""/jamesnk.png""
'Title': 'Json.NET is awesome!',
'Author': {
'Name': 'James Newton-King',
'Twitter': '@JamesNK',
'Picture': '/jamesnk.png'
},
""Date"": ""2013-01-23T19:30:00"",
""BodyHtml"": ""<h3>Title!</h3>\r\n<p>Content!</p>""
'Date': '2013-01-23T19:30:00',
'BodyHtml': '<h3>Title!</h3>\r\n<p>Content!</p>'
}
]";
@@ -16,12 +16,12 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Linq
public void Example()
{
string json = @"{
""d"": [
'd': [
{
""Name"": ""John Smith""
'Name': 'John Smith'
},
{
""Name"": ""Mike Smith""
'Name': 'Mike Smith'
}
]
}";
@@ -15,33 +15,33 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Schema
JsonSchemaResolver resolver = new JsonSchemaResolver();
schemaJson = @"{
""id"": ""person"",
""type"": ""object"",
""properties"": {
""name"": {""type"":""string""},
""age"": {""type"":""integer""}
'id': 'person',
'type': 'object',
'properties': {
'name': {'type':'string'},
'age': {'type':'integer'}
}
}";
JsonSchema personSchema = JsonSchema.Parse(schemaJson, resolver);
schemaJson = @"{
""id"": ""employee"",
""type"": ""object"",
""extends"": {""$ref"":""person""},
""properties"": {
""salary"": {""type"":""number""},
""jobTitle"": {""type"":""string""}
'id': 'employee',
'type': 'object',
'extends': {'$ref':'person'},
'properties': {
'salary': {'type':'number'},
'jobTitle': {'type':'string'}
}
}";
JsonSchema employeeSchema = JsonSchema.Parse(schemaJson, resolver);
string json = @"{
""name"": ""James"",
""age"": 29,
""salary"": 9000.01,
""jobTitle"": ""Junior Vice President""
'name': 'James',
'age': 29,
'salary': 9000.01,
'jobTitle': 'Junior Vice President'
}";
JObject employee = JObject.Parse(json);
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class CustomContractResolver
{
#region Types
public class DynamicContractResolver : DefaultContractResolver
{
private readonly char _startingWithChar;
@@ -38,9 +39,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
get { return FirstName + " " + LastName; }
}
}
#endregion
public void Example()
{
#region Usage
Person person = new Person
{
FirstName = "Dennis",
@@ -63,6 +66,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// {
// "LastName": "Deepwater-Diver"
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class CustomJsonConverter
{
#region Types
public class KeysJsonConverter : JsonConverter
{
private Type[] _types;
@@ -58,9 +59,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public string LastName { get; set; }
public IList<string> Roles { get; set; }
}
#endregion
public void Example()
{
#region Usage
Employee employee = new Employee
{
FirstName = "James",
@@ -91,6 +94,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(newEmployee.FirstName);
// James
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class CustomTraceWriter
{
#region Types
public class NLogTraceWriter : ITraceWriter
{
private static readonly Logger Logger = LogManager.GetLogger("NLogTraceWriter");
@@ -49,9 +50,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
}
}
}
#endregion
public void Example()
{
#region Usage
IList<string> countries = new List<string>
{
"New Zealand",
@@ -72,6 +75,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Denmark",
// "China"
// ]
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DataContractAndDataMember
{
#region Usage
[DataContract]
public class File
{
@@ -20,9 +21,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[DataMember]
public int Size { get; set; }
}
#endregion
public void Example()
{
#region Usage
File file = new File
{
Id = Guid.NewGuid(),
@@ -37,6 +40,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "ImportantLegalDocuments.docx",
// "Size": 51200
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DefaultValueAttributeIgnore
{
#region Types
public class Customer
{
public string FirstName { get; set; }
@@ -19,9 +20,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
get { return FirstName + " " + LastName; }
}
}
#endregion
public void Example()
{
#region Usage
Customer customer = new Customer();
string jsonIncludeDefaultValues = JsonConvert.SerializeObject(customer, Formatting.Indented);
@@ -40,6 +43,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(jsonIgnoreDefaultValues);
// {}
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DefaultValueHandlingIgnore
{
#region Types
public class Person
{
public string Name { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public Person Partner { get; set; }
public decimal? Salary { get; set; }
}
#endregion
public void Example()
{
#region Usage
Person person = new Person();
string jsonIncludeDefaultValues = JsonConvert.SerializeObject(person, Formatting.Indented);
@@ -36,6 +39,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(jsonIgnoreDefaultValues);
// {}
#endregion
}
}
}
@@ -9,7 +9,8 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
string json = @"{""Name"":""James""}";
#region Usage
string json = @"{'Name':'James'}";
var customerDefinition = new {Name = ""};
@@ -17,6 +18,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(customer.Name);
// James
#endregion
}
}
}
@@ -9,12 +9,14 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
string json = @"[""Starcraft"",""Halo"",""Legend of Zelda""]";
#region Usage
string json = @"['Starcraft','Halo','Legend of Zelda']";
List<string> videogames = JsonConvert.DeserializeObject<List<string>>(json);
Console.WriteLine(string.Join(", ", videogames));
// Starcraft, Halo, Legend of Zelda
#endregion
}
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeConstructorHandling
{
#region Types
public class Website
{
public string Url { get; set; }
@@ -24,10 +25,12 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Url = website.Url;
}
}
#endregion
public void Example()
{
string json = @"{""Url"":""http://www.google.com""}";
#region Usage
string json = @"{'Url':'http://www.google.com'}";
try
{
@@ -46,6 +49,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(website.Url);
// http://www.google.com
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeCustomCreationConverter
{
#region Types
public class Person
{
public string FirstName { get; set; }
@@ -28,15 +29,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
return new Employee();
}
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""Department"": ""Furniture"",
""JobTitle"": ""Carpenter"",
""FirstName"": ""John"",
""LastName"": ""Joinery"",
""BirthDate"": ""1983-02-02T00:00:00""
'Department': 'Furniture',
'JobTitle': 'Carpenter',
'FirstName': 'John',
'LastName': 'Joinery',
'BirthDate': '1983-02-02T00:00:00'
}";
Person person = JsonConvert.DeserializeObject<Person>(json, new PersonConverter());
@@ -48,6 +51,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(employee.JobTitle);
// Capenter
#endregion
}
}
}
@@ -11,15 +11,16 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
string json = @"{
""Table1"": [
'Table1': [
{
""id"": 0,
""item"": ""item 0""
'id': 0,
'item': 'item 0'
},
{
""id"": 1,
""item"": ""item 1""
'id': 1,
'item': 'item 1'
}
]
}";
@@ -37,6 +38,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
}
// 0 - item 0
// 1 - item 1
#endregion
}
}
}
@@ -9,9 +9,10 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
string json = @"{
""href"": ""/account/login.aspx"",
""target"": ""_blank""
'href': '/account/login.aspx',
'target': '_blank'
}";
Dictionary<string, string> htmlAttributes = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
@@ -21,6 +22,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(htmlAttributes["target"]);
// _blank
#endregion
}
}
}
@@ -7,18 +7,21 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeMissingMemberHandling
{
#region Types
public class Account
{
public string FullName { get; set; }
public bool Deleted { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""FullName"": ""Dan Deleted"",
""Deleted"": true,
""DeletedDate"": ""2013-01-20T00:00:00""
'FullName': 'Dan Deleted',
'Deleted': true,
'DeletedDate': '2013-01-20T00:00:00'
}";
try
@@ -28,10 +31,12 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
MissingMemberHandling = MissingMemberHandling.Error
});
}
catch (JsonSerializationException)
catch (JsonSerializationException ex)
{
Console.WriteLine(ex.Message);
// Could not find member 'DeletedDate' on object of type 'Account'. Path 'DeletedDate', line 4, position 23.
}
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeObject
{
#region Types
public class Account
{
public string Email { get; set; }
@@ -14,16 +15,18 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""Email"": ""james@example.com"",
""Active"": true,
""CreatedDate"": ""2013-01-20T00:00:00Z"",
""Roles"": [
""User"",
""Admin""
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
]
}";
@@ -31,6 +34,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(account.Email);
// james@example.com
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeObjectCreationHandling
{
#region Types
public class UserViewModel
{
public string Name { get; set; }
@@ -22,15 +23,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
};
}
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""Name"": ""James"",
""Offices"": [
""Auckland"",
""Wellington"",
""Christchurch""
'Name': 'James',
'Offices': [
'Auckland',
'Wellington',
'Christchurch'
]
}";
@@ -59,6 +62,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// Auckland
// Wellington
// Christchurch
#endregion
}
}
}
@@ -9,14 +9,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class DeserializeWithJsonSerializerFromFile
{
#region Types
public class Movie
{
public string Name { get; set; }
public int Year { get; set; }
}
#endregion
public void Example()
{
#region Usage
// read file into a string and deserialize JSON to a type
Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\movie.json"));
@@ -26,6 +29,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
JsonSerializer serializer = new JsonSerializer();
Movie movie2 = (Movie)serializer.Deserialize(file, typeof (Movie));
}
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class ErrorHandlingAttribute
{
#region Types
public class Employee
{
private List<string> _roles;
@@ -34,9 +35,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
errorContext.Handled = true;
}
}
#endregion
public void Example()
{
#region Usage
Employee person = new Employee
{
Name = "George Michael Bluth",
@@ -53,6 +56,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Age": 16,
// "Title": "Mister Manager"
// }
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
List<string> errors = new List<string>();
List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
@@ -40,6 +41,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
// Unexpected token parsing date. Expected String, got StartArray.
// Cannot convert null value to System.DateTime.
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonConverterAttributeClass
{
#region Types
public class UserConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
@@ -36,9 +37,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public string UserName { get; set; }
}
#endregion
public void Example()
{
#region Usage
User user = new User
{
UserName = @"domain\username"
@@ -48,6 +51,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(json);
// "domain\\username"
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonConverterAttributeProperty
{
#region Types
public enum UserStatus
{
NotConfirmed,
@@ -22,9 +23,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonConverter(typeof(StringEnumConverter))]
public UserStatus Status { get; set; }
}
#endregion
public void Example()
{
#region Usage
User user = new User
{
UserName = @"domain\username",
@@ -38,6 +41,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "UserName": "domain\\username",
// "Status": "Deleted"
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonObjectAttributeOptIn
{
#region Types
[JsonObject(MemberSerialization.OptIn)]
public class File
{
@@ -19,9 +20,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty]
public int Size { get; set; }
}
#endregion
public void Example()
{
#region Usage
File file = new File
{
Id = Guid.NewGuid(),
@@ -36,6 +39,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "ImportantLegalDocuments.docx",
// "Size": 51200
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonObjectAttributeOverrideIEnumerable
{
#region Types
[JsonObject]
public class Directory : IEnumerable<string>
{
@@ -29,9 +30,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
return GetEnumerator();
}
}
#endregion
public void Example()
{
#region Usage
Directory directory = new Directory
{
Name = "My Documents",
@@ -52,6 +55,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "WiseFinancalAdvice.xlsx"
// ]
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonPropertyItemLevelSetting
{
#region Types
public class Business
{
public string Name { get; set; }
@@ -22,9 +23,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty(IsReference = true)]
public Employee Manager { get; set; }
}
#endregion
public void Example()
{
#region Usage
Employee manager = new Employee
{
Name = "George-Michael"
@@ -65,6 +68,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// }
// ]
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonPropertyName
{
#region Types
public class Videogame
{
[JsonProperty("name")]
@@ -15,9 +16,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty("release_date")]
public DateTime ReleaseDate { get; set; }
}
#endregion
public void Example()
{
#region Usage
Videogame starcraft = new Videogame
{
Name = "Starcraft",
@@ -31,6 +34,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "name": "Starcraft",
// "release_date": "1998-01-01T00:00:00"
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonPropertyOrder
{
#region Types
public class Account
{
public string EmailAddress { get; set; }
@@ -24,9 +25,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty(Order = -2)]
public string FullName { get; set; }
}
#endregion
public void Example()
{
#region Usage
Account account = new Account
{
FullName = "Aaron Account",
@@ -48,6 +51,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Deleted": true,
// "DeletedDate": "2013-01-25T00:00:00"
// }
#endregion
}
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonPropertyPropertyLevelSetting
{
#region Types
public class Vessel
{
public string Name { get; set; }
@@ -15,9 +16,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? LaunchDate { get; set; }
}
#endregion
public void Example()
{
#region Usage
Vessel vessel = new Vessel
{
Name = "Red October",
@@ -31,6 +34,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "Red October",
// "Class": "Typhoon"
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class JsonPropertyRequired
{
#region Types
public class Videogame
{
[JsonProperty(Required = Required.Always)]
@@ -15,12 +16,14 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonProperty(Required = Required.AllowNull)]
public DateTime? ReleaseDate { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""Name"": ""Starcraft III"",
""ReleaseDate"": null
'Name': 'Starcraft III',
'ReleaseDate': null
}";
Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json);
@@ -30,6 +33,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(starcraft.ReleaseDate);
// null
#endregion
}
}
}
@@ -9,12 +9,13 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
string json = @"[
[
[
""1"",
""Two"",
""III""
'1',
'Two',
'III'
]
]
]";
@@ -31,6 +32,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(ex.Message);
// The reader's MaxDepth of 2 has been exceeded. Path '[0][0]', line 3, position 12.
}
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class NullValueHandlingIgnore
{
#region Types
public class Person
{
public string Name { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public Person Partner { get; set; }
public decimal? Salary { get; set; }
}
#endregion
public void Example()
{
#region Usage
Person person = new Person
{
Name = "Nigal Newborn",
@@ -43,6 +46,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "Nigal Newborn",
// "Age": 1
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class PopulateObject
{
#region Types
public class Account
{
public string Email { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
#endregion
public void Example()
{
#region Usage
Account account = new Account
{
Email = "james@example.com",
@@ -30,9 +33,9 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
};
string json = @"{
""Active"": false,
""Roles"": [
""Expired""
'Active': false,
'Roles': [
'Expired'
]
}";
@@ -46,6 +49,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(string.Join(", ", account.Roles));
// User, Admin, Expired
#endregion
}
}
}
@@ -5,8 +5,9 @@ using System.Text;
namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class ReferenceLoopHandlingObject
public class PreserveReferencesHandlingObject
{
#region Types
public class Directory
{
public string Name { get; set; }
@@ -19,9 +20,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public string Name { get; set; }
public Directory Parent { get; set; }
}
#endregion
public void Example()
{
#region Usage
Directory root = new Directory { Name = "Root" };
Directory documents = new Directory { Name = "My Documents", Parent = root };
@@ -93,6 +96,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// }
// ]
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class PropertyJsonIgnore
{
#region Types
public class Account
{
public string FullName { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
[JsonIgnore]
public string PasswordHash { get; set; }
}
#endregion
public void Example()
{
#region Usage
Account account = new Account
{
FullName = "Joe User",
@@ -28,6 +31,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(json);
// {"FullName":"Joe User","EmailAddress":"joe@example.com"}
#endregion
}
}
}
@@ -7,14 +7,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class ReferenceLoopHandlingIgnore
{
#region Types
public class Employee
{
public string Name { get; set; }
public Employee Manager { get; set; }
}
#endregion
public void Example()
{
#region Usage
Employee joe = new Employee { Name = "Joe User" };
Employee mike = new Employee { Name = "Mike Manager" };
joe.Manager = mike;
@@ -32,6 +35,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "Mike Manager"
// }
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializationCallbackAttributes
{
#region Types
public class SerializationEventTestObject
{
// 2222
@@ -58,9 +59,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Member4 = "This value was set after deserialization.";
}
}
#endregion
public void Example()
{
#region Usage
SerializationEventTestObject obj = new SerializationEventTestObject();
Console.WriteLine(obj.Member1);
@@ -98,6 +101,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// This value was set during deserialization
Console.WriteLine(obj.Member4);
// This value was set after deserialization.
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
List<string> videogames = new List<string>
{
"Starcraft",
@@ -20,6 +21,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(json);
// ["Starcraft","Halo","Legend of Zelda"]
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeConditionalProperty
{
#region Types
public class Employee
{
public string Name { get; set; }
@@ -18,9 +19,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
return (Manager != this);
}
}
#endregion
public void Example()
{
#region Usage
Employee joe = new Employee();
joe.Name = "Joe Employee";
Employee mike = new Employee();
@@ -33,6 +36,8 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
mike.Manager = mike;
string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented);
Console.WriteLine(json);
// [
// {
// "Name": "Joe Employee",
@@ -44,6 +49,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Name": "Mike Manager"
// }
// ]
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeContractResolver
{
#region Types
public class Person
{
public string FirstName { get; set; }
@@ -18,9 +19,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
get { return FirstName + " " + LastName; }
}
}
#endregion
public void Example()
{
#region Usage
Person person = new Person
{
FirstName = "Sarah",
@@ -38,6 +41,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "lastName": "Security",
// "fullName": "Sarah Security"
// }
#endregion
}
}
}
@@ -11,6 +11,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
DataSet dataSet = new DataSet("dataSet");
dataSet.Namespace = "NetFrameWork";
DataTable table = new DataTable();
@@ -46,6 +47,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// }
// ]
// }
#endregion
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
DateTime mayanEndOfTheWorld = new DateTime(2012, 12, 21);
string jsonIsoDate = JsonConvert.SerializeObject(mayanEndOfTheWorld);
@@ -23,6 +24,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(jsonMsDate);
// "\/Date(1356044400000+0100)\/"
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeDateTimeZoneHandling
{
#region Types
public class Flight
{
public string Destination { get; set; }
@@ -15,9 +16,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public DateTime DepartureDateLocal { get; set; }
public TimeSpan Duration { get; set; }
}
#endregion
public void Example()
{
#region Usage
Flight flight = new Flight
{
Destination = "Dubai",
@@ -82,6 +85,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "DepartureDateLocal": "2013-01-21T00:00:00",
// "Duration": "05:30:00"
// }
#endregion
}
}
}
}
@@ -9,6 +9,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
Dictionary<string, int> points = new Dictionary<string, int>
{
{ "James", 9001 },
@@ -24,6 +25,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "Jo": 3474,
// "Jess": 11926
// }
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeObject
{
#region Types
public class Account
{
public string Email { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
#endregion
public void Example()
{
#region Usage
Account account = new Account
{
Email = "james@example.com",
@@ -41,6 +44,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// }
Console.WriteLine(json);
#endregion
}
}
}
@@ -8,14 +8,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeRawJson
{
#region Types
public class JavaScriptSettings
{
public JRaw OnLoadFunction { get; set; }
public JRaw OnUnloadFunction { get; set; }
}
#endregion
public void Example()
{
#region Usage
JavaScriptSettings settings = new JavaScriptSettings
{
OnLoadFunction = new JRaw("OnLoad"),
@@ -29,6 +32,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// "OnLoadFunction": OnLoad,
// "OnUnloadFunction": function(e) { alert(e); }
// }
#endregion
}
}
}
@@ -8,6 +8,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeSerializationBinder
{
#region Types
public class KnownTypesBinder : SerializationBinder
{
public IList<Type> KnownTypes { get; set; }
@@ -29,9 +30,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public string Maker { get; set; }
public string Model { get; set; }
}
#endregion
public void Example()
{
#region Usage
KnownTypesBinder knownTypesBinder = new KnownTypesBinder
{
KnownTypes = new List<Type> {typeof (Car)}
@@ -64,6 +67,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(newValue.GetType().Name);
// Car
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeTypeNameHandling
{
#region Types
public abstract class Business
{
public string Name { get; set; }
@@ -22,9 +23,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public string FullName { get; set; }
public IList<Business> Businesses { get; set; }
}
#endregion
public void Example()
{
#region Usage
Stockholder stockholder = new Stockholder
{
FullName = "Steve Stockholder",
@@ -84,6 +87,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(newStockholder.Businesses[0].GetType().Name);
// Hotel
#endregion
}
}
}
@@ -7,6 +7,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeUnindentedJson
{
#region Types
public class Account
{
public string Email { get; set; }
@@ -14,9 +15,11 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
#endregion
public void Example()
{
#region Usage
Account account = new Account
{
Email = "james@example.com",
@@ -33,6 +36,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// {"Email":"james@example.com","Active":true,"CreatedDate":"2013-01-20T00:00:00Z","Roles":["User","Admin"]}
Console.WriteLine(json);
#endregion
}
}
}
@@ -10,6 +10,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public void Example()
{
#region Usage
List<StringComparison> stringComparisons = new List<StringComparison>
{
StringComparison.CurrentCulture,
@@ -30,6 +31,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
Console.WriteLine(string.Join(", ", newStringComparsions.Select(c => c.ToString())));
// CurrentCulture, InvariantCulture
#endregion
}
}
}
@@ -9,14 +9,17 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class SerializeWithJsonSerializerToFile
{
#region Types
public class Movie
{
public string Name { get; set; }
public int Year { get; set; }
}
#endregion
public void Example()
{
#region Usage
Movie movie = new Movie
{
Name = "Bad Boys",
@@ -32,6 +35,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, movie);
}
#endregion
}
}
}
@@ -8,18 +8,21 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
{
public class TraceWriter
{
#region Types
public class Account
{
public string FullName { get; set; }
public bool Deleted { get; set; }
}
#endregion
public void Example()
{
#region Usage
string json = @"{
""FullName"": ""Dan Deleted"",
""Deleted"": true,
""DeletedDate"": ""2013-01-20T00:00:00""
'FullName': 'Dan Deleted',
'Deleted': true,
'DeletedDate': '2013-01-20T00:00:00'
}";
MemoryTraceWriter traceWriter = new MemoryTraceWriter();
@@ -33,6 +36,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Serializer
// 2013-01-21T01:36:24.422 Info Started deserializing Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path 'FullName', line 2, position 20.
// 2013-01-21T01:36:24.442 Verbose Could not find member 'DeletedDate' on Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path 'DeletedDate', line 4, position 23.
// 2013-01-21T01:36:24.447 Info Finished deserializing Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path '', line 5, position 8.
#endregion
}
}
}
@@ -11,18 +11,18 @@ namespace Newtonsoft.Json.Tests.Documentation.Examples.Xml
public void Example()
{
string json = @"{
""@Id"": 1,
""Email"": ""james@example.com"",
""Active"": true,
""CreatedDate"": ""2013-01-20T00:00:00Z"",
""Roles"": [
""User"",
""Admin""
'@Id': 1,
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
],
""Team"": {
""@Id"": 2,
""Name"": ""Software Developers"",
""Description"": ""Creators of fine software products and services.""
'Team': {
'@Id': 2,
'Name': 'Software Developers',
'Description': 'Creators of fine software products and services.'
}
}";
@@ -200,7 +200,7 @@
<Compile Include="Documentation\Examples\Serializer\PopulateObject.cs" />
<Compile Include="Documentation\Examples\Serializer\PropertyJsonIgnore.cs" />
<Compile Include="Documentation\Examples\Serializer\ReferenceLoopHandlingIgnore.cs" />
<Compile Include="Documentation\Examples\Serializer\ReferenceLoopHandlingObject.cs" />
<Compile Include="Documentation\Examples\Serializer\PreserveReferencesHandlingObject.cs" />
<Compile Include="Documentation\Examples\Serializer\SerializationCallbackAttributes.cs" />
<Compile Include="Documentation\Examples\Serializer\SerializeCollection.cs" />
<Compile Include="Documentation\Examples\Serializer\SerializeConditionalProperty.cs" />
@@ -76,5 +76,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.11.15725")]
[assembly: AssemblyFileVersion("4.5.11.15726")]
#endif
@@ -90,7 +90,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.11.15725")]
[assembly: AssemblyFileVersion("4.5.11.15726")]
#endif
[assembly: CLSCompliant(true)]