diff --git a/Src/Doc/LINQtoJSON.html b/Src/Doc/LINQtoJSON.html new file mode 100644 index 00000000..d6fed018 --- /dev/null +++ b/Src/Doc/LINQtoJSON.html @@ -0,0 +1,193 @@ + + + + Reading and Writing JSON + + + + + + +
+ Json.NET - Quick Starts & API Documentation
+ Reading and Writing JSON
+ +
+ + +

LINQ to JSON is a programming API for working with JSON objects. The API has been designed with LINQ in mind to enable to quick querying and creation of JSON objects. LINQ to JSON sits under the Newtonsoft.Json.Linq namespace.

+ +

Creating JSON

+

There are a number of different options when it comes to creating JSON using LINQ to JSON. The first to create objects imperatively. You have total control but it is more verbose than other options.

+ +
+ +
+
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",
+
//   "\/Date(958996800000+1200)\/"
+
// ]
+
+ +
+ +

Another option is to create JSON objects declaratively.

+ + +
+ +
+
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 the source 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"
+
//        ]
+
//      }
+
//    ]
+
//  }
+
//}
+
+ +
+ +

You can create a JSON object from a non-JSON type using the FromObject method.

+ +
+ +
+
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
+
        }
+
  }
+
});
+
+ +
+ +

Finally JSON objects can be created from a string use the Parse method.

+ +
+ +
+
string json = @"{
+
  CPU: 'Intel',
+
  Drives: [
+
    'DVD read/writer',
+
    ""500 gigabyte hard drive""
+
  ]
+
}";
+
 
+
JObject o = JObject.Parse(json);
+
+ +
+ +

Querying JSON

+ + +

The properties methods that are the most useful when querying JSON objects are Children, Values and the property index.

+

Children returns all the children of that object. Values is similar to Children but it allows you to specify a type you would like the values to be converted into. Finally the property index is used to get a specific child, either by index position for JSON arrays or property name for JSON objects.

+ +
+ +
+
var postTitles =
+
  from p in rss["channel"]["item"].Children()
+
  select p.Value<string>("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
+
+ +
+ + +
+ + + + \ No newline at end of file diff --git a/Src/Doc/ReadingWritingJSON.html b/Src/Doc/ReadingWritingJSON.html new file mode 100644 index 00000000..aec08a4f --- /dev/null +++ b/Src/Doc/ReadingWritingJSON.html @@ -0,0 +1,87 @@ + + + + LINQ to JSON + + + + + + +
+ Json.NET - Quick Starts & API Documentation
+ LINQ to JSON
+ +
+ + +

To manually read and write JSON Json.NET provides the JsonWriter classes.

+ +

JsonTextReader and JsonTextWriter

+

JsonTextReader and JsonTextWriter are used to read and write JSON text. The JsonTextWriter has a number of settings on it to control how JSON is formatted when it is written. These options include formatting, indention character, indent count and quote character.

+ +
+ +
+
StringBuilder sb = new StringBuilder();
+
StringWriter sw = new StringWriter(sb);
+
 
+
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
+
{
+
  jsonWriter.Formatting = Formatting.Indented;
+
 
+
  jsonWriter.WriteStartObject();
+
  jsonWriter.WritePropertyName("CPU");
+
  jsonWriter.WriteValue("Intel");
+
  jsonWriter.WritePropertyName("PSU");
+
  jsonWriter.WriteValue("500W");
+
  jsonWriter.WritePropertyName("Drives");
+
  jsonWriter.WriteStartArray();
+
  jsonWriter.WriteValue("DVD read/writer");
+
  jsonWriter.WriteComment("(broken)");
+
  jsonWriter.WriteValue("500 gigabyte hard drive");
+
  jsonWriter.WriteValue("200 gigabype hard drive");
+
  jsonWriter.WriteEnd();
+
  jsonWriter.WriteEndObject();
+
}
+
 
+
// {
+
//   "CPU": "Intel",
+
//   "PSU": "500W",
+
//   "Drives": [
+
//     "DVD read/writer"
+
//     /*(broken)*/,
+
//     "500 gigabyte hard drive",
+
//     "200 gigabype hard drive"
+
//   ]
+
// }
+
+ +
+ +

JsonTokenReader and JsonTokenWriter

+

JsonTokenReader and JsonTokenWriter read and write LINQ to JSON objects. They are located in the Newtonsoft.Json.Linq namespace. These objects allow you to use LINQ to JSON objects with objects that read and write JSON such as the JsonSerializer. For example you can deserialize from a LINQ to JSON object into a regular .NET object and vice versa.

+ +
+ +
+
JObject o = new JObject(
+
  new JProperty("Name", "John Smith"),
+
  new JProperty("BirthDate", new DateTime(1983, 3, 20))
+
  );
+
 
+
JsonSerializer serializer = new JsonSerializer();
+
Person p = (Person)serializer.Deserialize(new JsonTokenReader(o), typeof(Person));
+
 
+
// John Smith
+
Console.WriteLine(p.Name);
+
+ +
+ + +
+ + + + \ No newline at end of file diff --git a/Src/Doc/SerializingJSON.html b/Src/Doc/SerializingJSON.html new file mode 100644 index 00000000..1a5a746e --- /dev/null +++ b/Src/Doc/SerializingJSON.html @@ -0,0 +1,144 @@ + + + + Serializing and deserializing JSON + + + + + + +
+ Json.NET - Quick Starts & API Documentation
+ Serializing and deserializing JSON
+ +
+ + +

The quickest method of converting between JSON text and a .NET object is using + the JsonSerializer. The JsonSerializer converts .NET objects into their JSON + equivalent and back again.

+ + +

For simple scenarios the SerializeObject and DeserializeObject methods on JavaScriptConvert provide an easy to use wrapper over + JsonSerializer.

+ + +
+ +
+
Product product = new Product();
+
 
+
product.Name = "Apple";
+
product.Expiry = new DateTime(2008, 12, 28);
+
product.Price = 3.99M;
+
product.Sizes = new string[] { "Small", "Medium", "Large" };
+
 
+
string output = JavaScriptConvert.SerializeObject(product);
+
//{
+
//  "Name": "Apple",
+
//  "Expiry": "\/Date(1230375600000+1300)\/",
+
//  "Price": 3.99,
+
//  "Sizes": [
+
//    "Small",
+
//    "Medium",
+
//    "Large"
+
//  ]
+
//}
+
 
+
Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));
+
+ +
+ +

JsonSerializer

+ + +

For more control over how an object is serialized the JsonSerializer can be used + directly. The JsonSerializer has a number of properties to control its behaviour + when serializing and deserializing.

+ +
+ +
+
Product product = new Product();
+
product.Expiry = new DateTime(2008, 12, 28);
+
 
+
JsonSerializer serializer = new JsonSerializer();
+
serializer.Converters.Add(new JavaScriptDateTimeConverter());
+
serializer.NullValueHandling = NullValueHandling.Ignore;
+
 
+
using (StreamWriter sw = new StreamWriter(@"c:\json.txt"))
+
using (JsonWriter writer = new JsonTextWriter(sw))
+
{
+
  serializer.Serialize(writer, product);
+
  // {"Expiry":new Date(1230375600000),"Price":0}
+
}
+
+ +
+ +
ReferenceLoopHandling
+

Controls how circular referencing objects are serialized. Error, ignore or + serialize.

+
MissingMemberHandling
+

Controls how missing members (e.g. JSON contains a property that isn't a member + on the object) are handled during deserialization. Error or ignore.

+
NullValueHandling
+

Controls how null values are handled during serialization and deserialization. + Include or ignore.

+
ObjectCreationHandling
+

Controls how objects are created during deserialization. Auto, reuse, replace.

+
Converters
+

A collection of JsonConverters that will be used during serialization and + deserialization.

+

Serialization Attributes

+

There are a number of optional attributes that can be placed on a class to control how it is serialized.

+ +
+ +
+
[JsonObject(MemberSerialization.OptIn)]
+
public class Person
+
{
+
  [JsonProperty]
+
  public string Name { get; set; }
+
 
+
  [JsonProperty]
+
  public DateTime BirthDate { get; set; }
+
 
+
  // not serialized
+
  public string Department { get; set; }
+
}
+
+ +
+ +
JsonObjectAttribute
+

The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonPropertyAttribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute).

+
JsonPropertyAttribute
+

Allows the name of the serialized member to be customized. The attribute also indicates that a member should be serialized when member serialization is set to opt-in.

+
JsonIgnoreAttribute
+

Excludes a field or property from serialization.

+

JsonConverters

+

JsonConverters allows JSON to be manually be written during serialization and read during deserialization. This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.

+

To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:

+ + + +
IsoDateTimeConverter
+

Converts a DateTime to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).

+ +
JavaScriptDateTimeConverter
+

Converts a DateTime to and from a JavaScript date constructor (e.g. new Date(52231943)).

+ +
XmlNodeConverter
+

Converts an XmlNode to and from JSON. Note that to convert a JSON object it must have only a single property. This is required because properties are converted into nodes and XML can only have one root node.

+ + + +
+ + + + \ No newline at end of file diff --git a/Src/Doc/custom.css b/Src/Doc/custom.css new file mode 100644 index 00000000..7a04f5f3 --- /dev/null +++ b/Src/Doc/custom.css @@ -0,0 +1,19 @@ +div#content { + padding: 0 10px 10px 10px; +} + +.overflowpanel +{ + width: 98%; + border: solid 1px #ccc; + overflow: auto; + overflow-y: hidden; + background-color: #fcfcfc; + margin-bottom: 1em; +} + +.overflowpanel .code +{ + padding: 10px 4px 20px 10px; + display: block; +} \ No newline at end of file diff --git a/Src/Doc/doc.shfb b/Src/Doc/doc.shfb new file mode 100644 index 00000000..1466c63c --- /dev/null +++ b/Src/Doc/doc.shfb @@ -0,0 +1,56 @@ + + + + + + + The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework. + The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>. + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON. + + + + + + + + + + + Summary, Parameter, Returns, AutoDocumentCtors, Namespace + InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected + + + .\Output\ + + + True + True + Help1xAndWebsite + True + False + 3.5 + True + False + False + False + + Json.NET - Quick Starts & API Documentation + Documentation + en-US + + + + + + Local + Msdn + Blank + Prototype + MemberName + Standard + False + AboveNamespaces + + + \ No newline at end of file diff --git a/Src/Doc/doc.sitemap b/Src/Doc/doc.sitemap new file mode 100644 index 00000000..8fa6da4a --- /dev/null +++ b/Src/Doc/doc.sitemap @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Src/Doc/styles.css b/Src/Doc/styles.css new file mode 100644 index 00000000..2962e122 --- /dev/null +++ b/Src/Doc/styles.css @@ -0,0 +1,439 @@ +/* page style */ + +body { + margin: 0; + background-color: #FFFFFF; + padding: 0; + font-size: 8.5pt; + font-family: verdana, arial, sans-serif; + color: #000000; +} + +html>body { + margin: 0; + background-color: #FFFFFF; + padding: 0; + font-size: 8.5pt; + font-family: verdana, arial, sans-serif; + color: #000000; + overflow: auto; +} + +table { + /* this is a trick to force tables to inherit the body font size */ + font-size: 100%; +} + +/* non-scrolling (control) region style */ + +div#control { + margin: 0; + background-color: #D4DFFF; + padding: 4px; + width: 100%; + border-bottom-color: #C8CDDE; + border-bottom-style: solid; + border-bottom-width: 1px; + z-index: 2; +} + +span.productTitle { + font-size: 80%; +} + +span.topicTitle { + font-size: 140%; + font-weight: bold; + color: #003399; +} + +span#chickenFeet { + float: left; +} + +span#languageFilter { + float: right; + height: 1px; + max-height: 1px; + vertical-align: bottom; + overflow: visible; +} + +/* scrolling (content) region style */ + +div#main +{ + clear: both; + margin: 0; + padding: 1em; + width: 100%; + z-index: 1; + overflow: auto; +} + +/* sections */ + +div#header { + font-size: 80%; + color: #666666; + margin-bottom: 0.5em; +} + +div.summary { + margin-top: 0em; + margin-bottom: 1em; +} + +div.section { + margin-bottom: 1em; +} + +div.sectionTitle { + display: inline; + font-size: 120%; + font-weight: bold; + color: #003399; +} + +div.sectionContent { + margin-top: 0.2em; +} + +span.subsectionTitle { + font-weight: bold; +} + +div#footer { + margin-top: 1em; + border-top: thin solid #003399; + padding-top: 0.5em; +} + +div#footer p { + margin-top: 0.2em; + margin-bottom: 0.2em; +} + +/* authored content (block) */ + +p { + margin-top: 1em; + margin-bottom: 1em; +} + +dl, ul, ol { + margin-top: 0.5em; + margin-bottom: 0.5em; +} + +pre { + margin: 0; + padding: 0; + font-family: "Andale Mono", "Courier New", Courier, monospace; +} + +table.authoredTable { + table-layout: fixed; + width: 100%; + margin-bottom: 1em; +} + +table.authoredTable th { + border-bottom-color: #C8CDDE; + border-bottom-style: solid; + border-bottom-width: 1px; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; +} + +table.authoredTable td { + border-bottom-style: solid; + border-bottom-color: #C8CDDE; + border-bottom-width: 1px; + background: #F7F7FF; + padding: 0.2em; + vertical-align: top; +} + +div.alert { + border: 1px solid #C8CDDE; + background: #F7F7FF; +} + +div.media { + text-align: center; + margin-bottom: 1em; +} + + +/* authored content (inline) */ + +span.keyword { + font-weight: bold; +} + +span.code { + font-family: "Andale Mono", "Courier New", Courier, monospace; + font-size: 110%; + color: #000066; +} + +/* auto-generated controls */ + +div.langTabs { + /*width: 100%;*/ +} + +div.langTab { + float: left; + width: 15%; + border-top: 1px solid #C8CDDE; + border-left: 1px solid #C8CDDE; + border-right: 1px solid #C8CDDE; + background: #F7F7FF; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: normal; +} + +div.activeLangTab { + float: left; + width: 15%; + border-top: 1px solid #C8CDDE; + border-left: 1px solid #C8CDDE; + border-right: 1px solid #C8CDDE; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; +} + +table.members { + /* table-layout: fixed; */ + width: 100%; +} + +table.members th.iconColumn { + width: 60px; +} + +table.members th.nameColumn { + width: 40%; +} + +table.members th.descriptionColumn { + width: 60%; +} + +table.members th { + border-bottom-color: #C8CDDE; + border-bottom-style: solid; + border-bottom-width: 1px; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; +} + +table.members td { + border-bottom-style: solid; + border-bottom-color: #C8CDDE; + border-bottom-width: 1px; + background: #F7F7FF; + padding: 0.2em; + vertical-align: top; + overflow: hidden; +} + +table.exceptions { + table-layout: fixed; + width: 100%; +} + + +table.exceptions th.exceptionNameColumn { + width: 40%; +} + +table.exceptions th.exceptionConditionColumn { + width: 60%; +} + +table.exceptions th { + border-bottom-color: #C8CDDE; + border-bottom-style: solid; + border-bottom-width: 1px; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; +} + +table.exceptions td { + border-bottom-style: solid; + border-bottom-color: #C8CDDE; + border-bottom-width: 1px; + background: #F7F7FF; + padding: 0.2em; + vertical-align: top; +} + +table.permissions { + table-layout: fixed; + width: 100%; +} + + +table.permissions th.permissionNameColumn { + width: 40%; +} + +table.permissions th.permissionConditionColumn { + width: 60%; +} + +table.permissions th { + border-bottom-color: #C8CDDE; + border-bottom-style: solid; + border-bottom-width: 1px; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; +} + +table.permissions td { + border-bottom-style: solid; + border-bottom-color: #C8CDDE; + border-bottom-width: 1px; + background: #F7F7FF; + padding: 0.2em; + vertical-align: top; +} + +span.obsolete { + color: red; +} + +span.cs { + display: inline; +} + +span.vb { + display: none; +} + +span.cpp { + display: none; +} + +span.nu +{ + display: none; +} + +/* syntax styling */ + +div.code span.identifier { + font-weight: bold; +} + +div.code span.keyword { + color: green; +} + +div.code span.parameter { + font-style: italic; + color: purple; +} + +div.code span.literal { + color: purple; +} + +div.code span.comment { + color: red; +} + +span.foreignPhrase { + font-style: italic; +} + +span.placeholder { + font-style: italic; +} + +span.parameter +{ + font-style: italic; +} + +span.typeparameter +{ + font-style: italic; +} + +a { + color: blue; + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} + +MSHelp\:link { + color: blue; + hoverColor: #3366ff; +} + +span.nolink { + font-weight: bold; +} + +span.selflink { + font-weight: bold; +} + +table.filter { + table-layout: fixed; +} + +tr.tabs td.tab { + width: 10em; + background: #F7F7FF; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: normal; + overflow: hidden; + cursor: pointer; +} + +tr.tabs td.activeTab { + width: 10em; + background: #EFEFF7; + padding: 0.2em; + text-align: left; + color: #000066; + font-weight: bold; + overflow: hidden; +} + +td.line { + background: #EFEFF7; +} + +dt { + font-weight: bold; + margin-top: 10px; + margin-left: 10px; +} diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 943a6b9d..38d4c025 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -38,6 +38,7 @@ using System.Net.Mail; using System.Web.Script.Serialization; using Newtonsoft.Json.Linq; using System.Linq; +using Newtonsoft.Json.Converters; namespace Newtonsoft.Json.Tests { @@ -231,7 +232,7 @@ namespace Newtonsoft.Json.Tests string output = JavaScriptConvert.SerializeObject(product); //{ // "Name": "Apple", - // "Expiry": new Date(1230422400000), + // "Expiry": "\/Date(1230375600000+1300)\/", // "Price": 3.99, // "Sizes": [ // "Small", @@ -250,6 +251,24 @@ namespace Newtonsoft.Json.Tests Assert.AreEqual("Large", deserializedProduct.Sizes[2]); } + //[Test] + //public void Advanced() + //{ + // Product product = new Product(); + // product.Expiry = new DateTime(2008, 12, 28); + + // JsonSerializer serializer = new JsonSerializer(); + // serializer.Converters.Add(new JavaScriptDateTimeConverter()); + // serializer.NullValueHandling = NullValueHandling.Ignore; + + // using (StreamWriter sw = new StreamWriter(@"c:\json.txt")) + // using (JsonWriter writer = new JsonTextWriter(sw)) + // { + // serializer.Serialize(writer, product); + // // {"Expiry":new Date(1230375600000),"Price":0} + // } + //} + [Test] public void JavaScriptConvertSerializer() { diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index 953323dd..948599d6 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -119,6 +119,17 @@ namespace Newtonsoft.Json.Tests jsonWriter.WriteEndObject(); } + // { + // "CPU": "Intel", + // "PSU": "500W", + // "Drives": [ + // "DVD read/writer" + // /*(broken)*/, + // "500 gigabyte hard drive", + // "200 gigabype hard drive" + // ] + // } + string expected = @"{ ""CPU"": ""Intel"", ""PSU"": ""500W"", diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 74d860ae..af976d61 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -37,16 +37,48 @@ namespace Newtonsoft.Json.Tests.Linq { public class LinqToJsonTest : TestFixtureBase { + [Test] + public void Manual() + { + 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", + // "\/Date(958996800000+1200)\/" + // ] + } + + [Test] + public void LinqToJsonDeserialize() + { + JObject o = new JObject( + new JProperty("Name", "John Smith"), + new JProperty("BirthDate", new DateTime(1983, 3, 20)) + ); + + JsonSerializer serializer = new JsonSerializer(); + Person p = (Person)serializer.Deserialize(new JsonTokenReader(o), typeof(Person)); + + // John Smith + Console.WriteLine(p.Name); + } + [Test] public void ObjectParse() { 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); IList properties = o.Properties().ToList(); diff --git a/Src/Newtonsoft.Json.sln b/Src/Newtonsoft.Json.sln index 91464170..49e3a00f 100644 --- a/Src/Newtonsoft.Json.sln +++ b/Src/Newtonsoft.Json.sln @@ -11,6 +11,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json", "Newtonso EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Tests", "Newtonsoft.Json.Tests\Newtonsoft.Json.Tests.csproj", "{3E6E2335-B079-4B5B-A65A-9D586914BCBB}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-4181-4BA6-8802-EB5B3EB9445A}" + ProjectSection(SolutionItems) = preProject + Doc\custom.css = Doc\custom.css + Doc\doc.shfb = Doc\doc.shfb + Doc\doc.sitemap = Doc\doc.sitemap + Doc\LINQtoJSON.html = Doc\LINQtoJSON.html + Doc\ReadingWritingJSON.html = Doc\ReadingWritingJSON.html + Doc\SerializingJSON.html = Doc\SerializingJSON.html + Doc\styles.css = Doc\styles.css + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs b/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs index 4b30f82d..25359f48 100644 --- a/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs @@ -8,7 +8,7 @@ using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Converters { /// - /// Converts text in the ISO 8601 date format to and from JSON. + /// Converts a to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z). /// public class IsoDateTimeConverter : JsonConverter { diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index ffe23cea..88c05316 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -34,7 +34,7 @@ using System.Linq; namespace Newtonsoft.Json.Converters { /// - /// Converts an XmlNode to and from JSON. + /// Converts an to and from JSON. /// public class XmlNodeConverter : JsonConverter { diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 00898abd..f2814548 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -203,7 +203,7 @@ namespace Newtonsoft.Json /// /// Reads the next Json token from the stream. /// - /// + /// true if the next token was read successfully; false if there are no more tokens to read. public abstract bool Read(); /// diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 62bcf659..745593df 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -68,7 +68,7 @@ namespace Newtonsoft.Json } /// - /// Get or set how missing members are handled during deserialization. + /// Get or set how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization. /// public MissingMemberHandling MissingMemberHandling { @@ -83,7 +83,7 @@ namespace Newtonsoft.Json } /// - /// Get or set how null values are handled. + /// Get or set how null values are handled during serialization and deserialization. /// public NullValueHandling NullValueHandling { @@ -98,7 +98,7 @@ namespace Newtonsoft.Json } /// - /// Gets or sets how objects are created. + /// Gets or sets how objects are created during deserialization. /// /// The object creation handling. public ObjectCreationHandling ObjectCreationHandling @@ -513,10 +513,9 @@ namespace Newtonsoft.Json ConstructorInfo c = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).SingleOrDefault(); if (c == null) - throw new Exception("sdsdf"); + throw new JsonSerializationException("Could not find a public constructor for type {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); IDictionary constructorParameters = c.GetParameters().ToDictionary(p => p, p => (object)null); - //IList constructorValues bool exit = false; while (!exit && reader.Read()) diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 6e533925..ec49dae4 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -262,7 +262,9 @@ namespace Newtonsoft.Json /// /// Reads the next Json token from the stream. /// - /// + /// + /// true if the next token was read successfully; false if there are no more tokens to read. + /// public override bool Read() { while (true) @@ -792,7 +794,7 @@ namespace Newtonsoft.Json } /// - /// Changes the to Closed. + /// Changes the to Closed. /// public override void Close() { diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 19c04088..3a261169 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -179,11 +179,11 @@ namespace Newtonsoft.Json throw new JsonWriterException("Invalid JsonToken: " + token); } } - + /// /// Writes the property name of a name/value pair on a Json object. /// - /// + /// The name of the property. public override void WritePropertyName(string name) { base.WritePropertyName(name); diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 3367d0ee..53ca7526 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -287,7 +287,7 @@ namespace Newtonsoft.Json /// /// Writes the property name of a name/value pair on a Json object. /// - /// + /// The name of the property. public virtual void WritePropertyName(string name) { AutoComplete(JsonToken.PropertyName); diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index 9af09fd2..b49d97b3 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -95,7 +95,7 @@ namespace Newtonsoft.Json.Linq /// /// Returns a count of this token's child tokens. /// - /// + /// A count of this token's child tokens. public int Count() { return Children().Count(); diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index de2469db..930c0b1d 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -172,7 +172,7 @@ namespace Newtonsoft.Json.Linq /// Creates a comment with the given value. /// /// The value. - /// + /// A comment with the given value. public static JValue CreateComment(string value) { return new JValue(value, JsonTokenType.Comment); @@ -182,7 +182,7 @@ namespace Newtonsoft.Json.Linq /// Creates a string with the given value. /// /// The value. - /// + /// A string with the given value. public static JValue CreateString(string value) { return new JValue(value, JsonTokenType.String); diff --git a/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs b/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs index 13bd7293..921b7f97 100644 --- a/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs +++ b/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs @@ -30,7 +30,9 @@ namespace Newtonsoft.Json.Linq /// /// Reads the next Json token from the stream. /// - /// + /// + /// true if the next token was read successfully; false if there are no more tokens to read. + /// public override bool Read() { if (CurrentState != State.Start) diff --git a/Src/Newtonsoft.Json/Linq/JsonTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JsonTokenWriter.cs index a7273e4b..79c27863 100644 --- a/Src/Newtonsoft.Json/Linq/JsonTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JsonTokenWriter.cs @@ -118,7 +118,7 @@ namespace Newtonsoft.Json.Linq /// /// Writes the property name of a name/value pair on a Json object. /// - /// + /// The name of the property. public override void WritePropertyName(string name) { base.WritePropertyName(name);