Merge branch 'master' of https://github.com/JamesNK/Newtonsoft.Json
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="CreatingLINQtoJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>As well as parsing JSON from existing JSON strings, LINQ to JSON objects can be created from scratch to create new JSON structures.</para>
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Manually Creating JSON</title>
|
||||
<content>
|
||||
<para>Setting values and creating objects and arrays one at a time gives you
|
||||
total control but it is more verbose than other options.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateNormal" title="Creating JSON" />
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Creating JSON with LINQ</title>
|
||||
<content>
|
||||
<para>Declaratively creating JSON objects using LINQ is a fast way to create JSON from collections of values.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateDeclaratively" title="Creating JSON Declaratively" />
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Creating JSON from an object</title>
|
||||
<content>
|
||||
<para>The last option is to create a JSON object from a non-JSON type using the
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.Linq.JObject.FromObject</codeEntityReference>
|
||||
method. Internally FromObject will use the JsonSerializer to serialize the object to LINQ to JSON objects instead of text.</para>
|
||||
|
||||
<para>The example below is creating a JSON object from an anonymous object but any
|
||||
.NET type can be used with FromObject to create JSON.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateFromObject" title="Creating JSON from an Object" />
|
||||
</content>
|
||||
</section>
|
||||
<relatedTopics>
|
||||
<externalLink>
|
||||
<linkText>LINQ to JSON</linkText>
|
||||
<linkUri>LINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.Linq.JObject.FromObject</codeEntityReference>
|
||||
</relatedTopics>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="JsonSchema" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>Json.NET supports the JSON Schema standard via the <codeEntityReference>T:Newtonsoft.Json.Schema.JsonSchema</codeEntityReference>
|
||||
and <codeEntityReference>T:Newtonsoft.Json.JsonValidatingReader</codeEntityReference> classes. It sits under
|
||||
the <codeEntityReference>N:Newtonsoft.Json.Schema</codeEntityReference> namespace.</para>
|
||||
<para>JSON Schema is used to validate the structure and
|
||||
data types of a piece of JSON, similar to XML Schema for XML. Read more about JSON Schema at
|
||||
<externalLink>
|
||||
<linkText>json-schema.org</linkText>
|
||||
<linkUri>http://json-schema.org/</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink></para>
|
||||
</introduction>
|
||||
<!-- Optional procedures followed by optional code example but must have
|
||||
at least one procedure or code example -->
|
||||
<section>
|
||||
<title>Validating with JSON Schema</title>
|
||||
<content>
|
||||
<para>The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then
|
||||
use the <codeEntityReference>M:Newtonsoft.Json.Schema.Extensions.IsValid(Newtonsoft.Json.Linq.JToken,Newtonsoft.Json.Schema.JsonSchema)</codeEntityReference>
|
||||
method with the JSON Schema.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\JsonSchemaTests.cs" region="IsValidBasic" title="Validate JSON with IsValid" />
|
||||
|
||||
<para>To get validation error messages use the
|
||||
<codeEntityReference>M:Newtonsoft.Json.Schema.Extensions.IsValid(Newtonsoft.Json.Linq.JToken,Newtonsoft.Json.Schema.JsonSchema,System.Collections.Generic.IList{System.String}@)</codeEntityReference>
|
||||
or
|
||||
<codeEntityReference>M:Newtonsoft.Json.Schema.Extensions.Validate(Newtonsoft.Json.Linq.JToken,Newtonsoft.Json.Schema.JsonSchema,Newtonsoft.Json.Schema.ValidationEventHandler)</codeEntityReference>
|
||||
overloads.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\JsonSchemaTests.cs" region="IsValidMessages" title="Validate JSON with IsValid" />
|
||||
|
||||
<para>Internally IsValid uses <codeEntityReference>T:Newtonsoft.Json.JsonValidatingReader</codeEntityReference>
|
||||
to perform the JSON Schema validation. To skip the overhead of loading JSON into a JObject/JArray, validating
|
||||
the JSON and then deserializing the JSON into a class, JsonValidatingReader can be used with JsonSerializer to validate JSON while the object is being deserialized.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\JsonSchemaTests.cs" region="JsonValidatingReader" title="Validate JSON with JsonValidatingReader" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Creating JSON Schemas</title>
|
||||
<content>
|
||||
|
||||
<para>The simplest way to get a <codeEntityReference>T:Newtonsoft.Json.Schema.JsonSchema</codeEntityReference> object is to load it from a string or a file.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\JsonSchemaTests.cs" region="LoadJsonSchema" title="Creating JSON Schemas from strings or files" />
|
||||
|
||||
<para>It is also possible to create JsonSchema objects in code.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\JsonSchemaTests.cs" region="ManuallyCreateJsonSchema" title="Create new JSON Schemas in code" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
<relatedTopics>
|
||||
<codeEntityReference>T:Newtonsoft.Json.Schema.JsonSchema</codeEntityReference>
|
||||
<codeEntityReference>T:Newtonsoft.Json.JsonValidatingReader</codeEntityReference>
|
||||
</relatedTopics>
|
||||
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
+47
-75
@@ -2,86 +2,58 @@
|
||||
<topic id="LINQtoJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>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
|
||||
<para>LINQ to JSON is an API for working with JSON objects.
|
||||
It has been designed with LINQ in mind to enable to quick querying
|
||||
and creation of JSON objects. LINQ to JSON sits under the
|
||||
<codeEntityReference>N:Newtonsoft.Json.Linq</codeEntityReference>
|
||||
namespace.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonBasic" title="Using LINQ for JSON" />
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Reading JSON</title>
|
||||
<content>
|
||||
<para>JSON values can be read from a string use the
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Parse(System.String)</codeEntityReference>
|
||||
method.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Parsing a JSON Object from Text" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from Text" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Creating JSON</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>There are a number of different options when it comes to creating
|
||||
JSON using LINQ to JSON.</para>
|
||||
|
||||
<para>The first option is to set values one at a time. You
|
||||
have total control but it is more verbose than other options.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateNormal" title="Creating JSON" />
|
||||
|
||||
<para>Another option is to create JSON objects declaratively using LINQ.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateDeclaratively" title="Creating JSON Declaratively" />
|
||||
|
||||
<para>Finally you can create a JSON object from a non-JSON type using the
|
||||
<codeEntityReference>Overload:Newtonsoft.Json.Linq.JObject.FromObject</codeEntityReference>
|
||||
method. The example below is creating a JSON object from an anonymous object but any
|
||||
.NET type can be used with FromObject to create JSON.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateFromObject" title="Creating JSON from an Object" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Querying JSON</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The properties and methods that are the most useful when querying JSON
|
||||
objects are
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
|
||||
and
|
||||
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference>.</para>
|
||||
|
||||
<para>Children returns all the children of that object. If it is a
|
||||
JObject it will return a collection of properties to work with and if
|
||||
it is a JArray you will get a collection of the array's values.</para>
|
||||
|
||||
<para>The index property is used to get a specific child, either by
|
||||
index position for JSON arrays or property name for JSON objects.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonQuerying" title="Querying JSON" />
|
||||
|
||||
<para>LINQ to JSON can also be used to manually convert from JSON to a .NET object.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeObject" title="Deserializing Using LINQ Objects" />
|
||||
|
||||
<para>Manually serializing and deserializing between .NET objects is most useful when
|
||||
working with JSON that doesn't closely match your .NET objects.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeExample" title="Deserializing Using LINQ Example" />
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Topics</title>
|
||||
<content>
|
||||
<para>Select a topic below for more information:</para>
|
||||
<list class="bullet">
|
||||
<listItem>
|
||||
<para>
|
||||
<externalLink>
|
||||
<linkText>Parsing JSON</linkText>
|
||||
<linkUri>ParsingLINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>
|
||||
<externalLink>
|
||||
<linkText>Creating JSON</linkText>
|
||||
<linkUri>CreatingLINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>
|
||||
<externalLink>
|
||||
<linkText>Querying JSON with LINQ</linkText>
|
||||
<linkUri>QueryingLINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
</para>
|
||||
</listItem>
|
||||
<listItem>
|
||||
<para>
|
||||
<externalLink>
|
||||
<linkText>Querying JSON with SelectToken</linkText>
|
||||
<linkUri>SelectToken.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
</para>
|
||||
</listItem>
|
||||
</list>
|
||||
</content>
|
||||
</section>
|
||||
<relatedTopics>
|
||||
<externalLink>
|
||||
<linkText>Querying LINQ to JSON with SelectToken</linkText>
|
||||
<linkUri>SelectToken.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
|
||||
<codeEntityReference>T:Newtonsoft.Json.Linq.JObject</codeEntityReference>
|
||||
<codeEntityReference>T:Newtonsoft.Json.Linq.JArray</codeEntityReference>
|
||||
<codeEntityReference>T:Newtonsoft.Json.Linq.JValue</codeEntityReference>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="ParsingLINQtoJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>LINQ to JSON has methods available for parsing JSON from a string or loading JSON directly from a file.</para>
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Parsing JSON text</title>
|
||||
<content>
|
||||
<para>JSON values can be read from a string using
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Parse(System.String)</codeEntityReference>.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Parsing a JSON Object from text" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from text" />
|
||||
</content>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Loading JSON from a file</title>
|
||||
<content>
|
||||
<para>JSON can also be loaded directly from a file using <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.ReadFrom(Newtonsoft.Json.JsonReader)</codeEntityReference>.</para>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonReadObject" title="Reading JSON from a file" />
|
||||
</content>
|
||||
</section>
|
||||
<relatedTopics>
|
||||
<externalLink>
|
||||
<linkText>LINQ to JSON</linkText>
|
||||
<linkUri>LINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Parse(System.String)</codeEntityReference>
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.ReadFrom(Newtonsoft.Json.JsonReader)</codeEntityReference>
|
||||
</relatedTopics>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="QueryingLINQtoJSON" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>LINQ to JSON provides a number of methods for getting data from its objects. The index methods on JObject/JArray let you quickly get data by its property name
|
||||
on an object or index in a collection, while <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference> lets you get ranges
|
||||
of data as <codeInline>IEnumerable<JToken></codeInline> to then query using LINQ.</para>
|
||||
</introduction>
|
||||
|
||||
<section>
|
||||
<title>Getting values by Property Name or Collection Index</title>
|
||||
<content>
|
||||
<!-- Uncomment this to create a sub-section outline
|
||||
<autoOutline /> -->
|
||||
<para>The simplest way to get a value from LINQ to JSON is to use the
|
||||
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference> index on
|
||||
JObject/JArray and then cast the returned <codeEntityReference>T:Newtonsoft.Json.Linq.JValue</codeEntityReference> to the type you want.
|
||||
</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonSimpleQuerying" title="Getting JSON Values" />
|
||||
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
<title>Querying with LINQ</title>
|
||||
<content>
|
||||
<para>JObject/JArray can also be queried using LINQ. <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
|
||||
returns the children values of a JObject/JArray
|
||||
as an <codeInline>IEnumerable<JToken></codeInline> that can then be queried with the standard Where/OrderBy/Select LINQ operators.</para>
|
||||
|
||||
<alert class="note">
|
||||
<para><codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference> returns all the children of a token. If it is a
|
||||
JObject it will return a collection of properties to work with and if
|
||||
it is a JArray you will get a collection of the array's values.</para>
|
||||
</alert>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonQuerying" title="Querying JSON" />
|
||||
|
||||
<para>LINQ to JSON can also be used to manually convert JSON to a .NET object.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeObject" title="Deserializing Using LINQ Objects" />
|
||||
|
||||
<para>Manually serializing and deserializing between .NET objects is useful when you are
|
||||
working with JSON that doesn't closely match your .NET objects.</para>
|
||||
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonDeserializeExample" title="Deserializing Using LINQ Example" />
|
||||
</content>
|
||||
</section>
|
||||
<relatedTopics>
|
||||
<externalLink>
|
||||
<linkText>LINQ to JSON</linkText>
|
||||
<linkUri>LINQtoJSON.htm</linkUri>
|
||||
<linkTarget>_self</linkTarget>
|
||||
</externalLink>
|
||||
|
||||
<codeEntityReference>P:Newtonsoft.Json.Linq.JToken.Item(System.Object)</codeEntityReference>
|
||||
<codeEntityReference>M:Newtonsoft.Json.Linq.JToken.Children</codeEntityReference>
|
||||
</relatedTopics>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
+8
-4
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Topics>
|
||||
<Topic id="Introduction" visible="True" isDefault="true" />
|
||||
<Topic id="SerializingJSON" visible="True" isExpanded="true" title="Serializing and Deserializing JSON">
|
||||
<Topic id="SerializingJSON" visible="True" title="Serializing and Deserializing JSON">
|
||||
<Topic id="SerializationSettings" visible="True" title="Serialization Settings" />
|
||||
<Topic id="SerializationGuide" visible="True" title="Serialization Guide" />
|
||||
<Topic id="SerializationAttributes" visible="True" title="Serialization Attributes" />
|
||||
@@ -15,9 +15,13 @@
|
||||
<Topic id="ReducingSerializedJSONSize" visible="True" title="Reducing Serialized JSON Size" />
|
||||
<Topic id="SerializingJSONFragments" visible="True" title="Serializing Partial JSON Fragments" />
|
||||
</Topic>
|
||||
<Topic id="LINQtoJSON" visible="True" isExpanded="true" title="LINQ to JSON">
|
||||
<Topic id="SelectToken" visible="True" title="Querying LINQ to JSON with SelectToken" />
|
||||
<Topic id="LINQtoJSON" visible="True" title="LINQ to JSON">
|
||||
<Topic id="ParsingLINQtoJSON" visible="True" title="Parsing JSON" />
|
||||
<Topic id="CreatingLINQtoJSON" visible="True" title="Creating JSON" />
|
||||
<Topic id="QueryingLINQtoJSON" visible="True" title="Querying JSON with LINQ" />
|
||||
<Topic id="SelectToken" visible="True" title="Querying JSON with SelectToken" />
|
||||
</Topic>
|
||||
<Topic id="ReadingWritingJSON" visible="True" isSelected="true" title="Basic Reading and Writing JSON" />
|
||||
<Topic id="JsonSchema" visible="True" isSelected="true" title="Validating JSON" />
|
||||
<Topic id="ReadingWritingJSON" visible="True" title="Basic Reading and Writing JSON" />
|
||||
<Topic id="ConvertingJSONandXML" visible="True" title="Converting between JSON and XML" />
|
||||
</Topics>
|
||||
@@ -140,6 +140,12 @@
|
||||
<None Include="CustomCreationConverter.aml" />
|
||||
<None Include="ContractResolver.aml" />
|
||||
<None Include="ConvertingJSONandXML.aml" />
|
||||
<None Include="CreatingLINQtoJSON.aml" />
|
||||
<None Include="Copy of ContractResolver.aml" />
|
||||
<None Include="JsonSchema.aml" />
|
||||
<None Include="JsonSchema.aml" />
|
||||
<None Include="ParsingLINQtoJSON.aml" />
|
||||
<None Include="QueryingLINQtoJSON.aml" />
|
||||
<None Include="ReadingWritingJSON.aml" />
|
||||
<None Include="SelectToken.aml" />
|
||||
<None Include="LINQtoJSON.aml" />
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !(NET35 || NET20 || PORTABLE)
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Dynamic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
|
||||
#endif
|
||||
using Newtonsoft.Json.Schema;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
using File = System.IO.File;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
public class JsonSchemaTests
|
||||
{
|
||||
public void IsValidBasic()
|
||||
{
|
||||
#region IsValidBasic
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties':
|
||||
{
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': 'James',
|
||||
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
|
||||
}");
|
||||
|
||||
bool valid = person.IsValid(schema);
|
||||
// true
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void IsValidMessages()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
'description': 'A person',
|
||||
'type': 'object',
|
||||
'properties':
|
||||
{
|
||||
'name': {'type':'string'},
|
||||
'hobbies': {
|
||||
'type': 'array',
|
||||
'items': {'type':'string'}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
#region IsValidMessages
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': null,
|
||||
'hobbies': ['Invalid content', 0.123456789]
|
||||
}");
|
||||
|
||||
IList<string> messages;
|
||||
bool valid = person.IsValid(schema, out messages);
|
||||
// false
|
||||
// Invalid type. Expected String but got Null. Line 2, position 21.
|
||||
// Invalid type. Expected String but got Float. Line 3, position 51.
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void JsonValidatingReader()
|
||||
{
|
||||
string schemaJson = "{}";
|
||||
|
||||
#region JsonValidatingReader
|
||||
string json = @"{
|
||||
'name': 'James',
|
||||
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
|
||||
}";
|
||||
|
||||
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||
|
||||
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
|
||||
validatingReader.Schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
IList<string> messages = new List<string>();
|
||||
validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
Person p = serializer.Deserialize<Person>(validatingReader);
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void LoadJsonSchema()
|
||||
{
|
||||
#region LoadJsonSchema
|
||||
// load from a string
|
||||
JsonSchema schema1 = JsonSchema.Parse(@"{'type':'object'}");
|
||||
|
||||
// load from a file
|
||||
using (TextReader reader = File.OpenText(@"c:\schema\Person.json"))
|
||||
{
|
||||
JsonSchema schema2 = JsonSchema.Read(new JsonTextReader(reader));
|
||||
|
||||
// do stuff
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void ManuallyCreateJsonSchema()
|
||||
{
|
||||
#region ManuallyCreateJsonSchema
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Type = JsonSchemaType.Object;
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{"name", new JsonSchema {Type = JsonSchemaType.String}},
|
||||
{
|
||||
"hobbies", new JsonSchema
|
||||
{
|
||||
Type = JsonSchemaType.Array,
|
||||
Items = new List<JsonSchema> { new JsonSchema {Type = JsonSchemaType.String} }
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
'name': 'James',
|
||||
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
|
||||
}");
|
||||
|
||||
bool valid = person.IsValid(schema);
|
||||
// true
|
||||
#endregion
|
||||
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -46,11 +46,43 @@ using Newtonsoft.Json.Tests.TestObjects;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
using File = System.IO.File;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation
|
||||
{
|
||||
public static class File
|
||||
{
|
||||
public static StreamReader OpenText(string path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class LinqToJsonTests
|
||||
{
|
||||
public void LinqToJsonBasic()
|
||||
{
|
||||
#region LinqToJsonBasic
|
||||
JObject o = JObject.Parse(@"{
|
||||
'CPU': 'Intel',
|
||||
'Drives': [
|
||||
'DVD read/writer',
|
||||
'500 gigabyte hard drive'
|
||||
]
|
||||
}");
|
||||
|
||||
string cpu = (string)o["CPU"];
|
||||
// Intel
|
||||
|
||||
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
|
||||
@@ -194,6 +226,65 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
#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 = @"{
|
||||
'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',
|
||||
'categories': [
|
||||
'Json.NET',
|
||||
'CodePlex'
|
||||
]
|
||||
},
|
||||
{
|
||||
'title': 'LINQ to JSON beta',
|
||||
'description': 'Annoucing LINQ to JSON',
|
||||
'link': 'http://james.newtonking.com/projects/json-net.aspx',
|
||||
'categories': [
|
||||
'Json.NET',
|
||||
'LINQ'
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}";
|
||||
|
||||
JObject rss = JObject.Parse(json);
|
||||
|
||||
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
|
||||
|
||||
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();
|
||||
|
||||
@@ -882,6 +882,30 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
// Url = http://www.imdb.com/name/nm0385296/
|
||||
#endregion
|
||||
}
|
||||
|
||||
public void SerializeMultidimensionalArrayExample()
|
||||
{
|
||||
string[,] famousCouples = new string[,]
|
||||
{
|
||||
{"Adam", "Eve"},
|
||||
{"Bonnie", "Clyde"},
|
||||
{"Donald", "Daisy"},
|
||||
{"Han", "Leia"}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);
|
||||
// [
|
||||
// ["Adam", "Eve"],
|
||||
// ["Bonnie", "Clyde"],
|
||||
// ["Donald", "Daisy"],
|
||||
// ["Han", "Leia"]
|
||||
// ]
|
||||
|
||||
string[,] deserialized = JsonConvert.DeserializeObject<string[,]>(json);
|
||||
|
||||
Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
|
||||
// Han, Leia
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -117,6 +117,7 @@
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="Converters\VersionConverterTests.cs" />
|
||||
<Compile Include="Documentation\ConvertingJsonAndXmlTests.cs" />
|
||||
<Compile Include="Documentation\JsonSchemaTests.cs" />
|
||||
<Compile Include="Documentation\ReadingAndWritingJsonTests.cs" />
|
||||
<Compile Include="Documentation\LinqToJsonTests.cs" />
|
||||
<Compile Include="Documentation\SerializationTests.cs" />
|
||||
|
||||
@@ -768,6 +768,27 @@ namespace Newtonsoft.Json.Tests
|
||||
BenchmarkSerializeMethod(SerializeMethod.JsonNet, s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseJObject()
|
||||
{
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.Start();
|
||||
for (int i = 0; i < 100000; i++)
|
||||
{
|
||||
JObject o = JObject.Parse(@"{
|
||||
""CPU"": ""Intel"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}");
|
||||
}
|
||||
timer.Stop();
|
||||
|
||||
string linq = timer.Elapsed.TotalSeconds.ToString();
|
||||
Console.WriteLine(linq);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JObjectToString()
|
||||
{
|
||||
@@ -778,7 +799,7 @@ namespace Newtonsoft.Json.Tests
|
||||
for (int i = 0; i < Iterations; i++)
|
||||
{
|
||||
test["dummy"] = new JValue(i);
|
||||
Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
|
||||
test.ToString(Formatting.None);
|
||||
}
|
||||
return null;
|
||||
}, "JObject.ToString");
|
||||
|
||||
Reference in New Issue
Block a user