-Documentation
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>
|
||||
+46
-74
@@ -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.
|
||||
<para>LINQ to JSON is an 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
|
||||
<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>
|
||||
+6
-3
@@ -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" />
|
||||
@@ -16,8 +16,11 @@
|
||||
<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="ParsingLINQtoJSON" visible="True" isSelected="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="ReadingWritingJSON" visible="True" title="Basic Reading and Writing JSON" />
|
||||
<Topic id="ConvertingJSONandXML" visible="True" title="Converting between JSON and XML" />
|
||||
</Topics>
|
||||
@@ -140,6 +140,9 @@
|
||||
<None Include="CustomCreationConverter.aml" />
|
||||
<None Include="ContractResolver.aml" />
|
||||
<None Include="ConvertingJSONandXML.aml" />
|
||||
<None Include="CreatingLINQtoJSON.aml" />
|
||||
<None Include="ParsingLINQtoJSON.aml" />
|
||||
<None Include="QueryingLINQtoJSON.aml" />
|
||||
<None Include="ReadingWritingJSON.aml" />
|
||||
<None Include="SelectToken.aml" />
|
||||
<None Include="LINQtoJSON.aml" />
|
||||
|
||||
Reference in New Issue
Block a user