-Documentation
This commit is contained in:
+9
-4
@@ -35,10 +35,15 @@
|
||||
older versions of Json.NET, then change the
|
||||
<codeEntityReference>T:Newtonsoft.Json.DateFormatHandling</codeEntityReference>
|
||||
setting to MicrosoftDateFormat.</para>
|
||||
<para>Json.NET also has the <codeEntityReference>T:Newtonsoft.Json.DateTimeZoneHandling</codeEntityReference>
|
||||
setting. This can be
|
||||
used to convert a DateTime's kind when serializing. For example set
|
||||
DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates.</para>
|
||||
<para>The <codeEntityReference>T:Newtonsoft.Json.DateTimeZoneHandling</codeEntityReference> setting can be
|
||||
used to convert a DateTime's <codeEntityReference>T:System.DateTimeKind</codeEntityReference> when serializing. For example set
|
||||
DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates. Note that this setting does not effect DateTimeOffsets.</para>
|
||||
<para>If your dates don't follow the ISO 8601 standard, then the DateFormatString setting can be used to customize the format of
|
||||
date strings that are read and written using .NET's <externalLink>
|
||||
<linkText>custom date and time format syntax</linkText>
|
||||
<linkUri>https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink>.</para>
|
||||
</content>
|
||||
</section>
|
||||
<section>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="JTokenAnnotation" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample uses annotations with LINQ to JSON objects.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Linq\JTokenAnnotation.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="MergeJson" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample merges LINQ to JSON objects.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Linq\MergeJson.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="QueryJsonSelectTokenJsonPath" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample loads JSON and then queries values from it using <codeEntityReference>M:Newtonsoft.Json.Linq.JToken.SelectToken(System.String)</codeEntityReference> with a <externalLink>
|
||||
<linkText>JSONPath</linkText>
|
||||
<linkUri>http://stackoverflow.com/tags/jsonpath</linkUri>
|
||||
<linkTarget>_blank</linkTarget>
|
||||
</externalLink> query.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Linq\QueryJsonSelectTokenJsonPath.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DefaultSettings" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample serializes and deserializes JSON using <codeEntityReference>P:Newtonsoft.Json.JsonConvert.DefaultSettings</codeEntityReference>.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DefaultSettings.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DeserializeDateFormatString" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample uses the DateFormatString
|
||||
setting to control how <codeEntityReference>T:System.DateTime</codeEntityReference> and <codeEntityReference>T:System.DateTimeOffset</codeEntityReference> are deserialized.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeDateFormatString.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DeserializeExtensionData" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample deserializes JSON to an object with extension data.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeExtensionData.cs" region="Types" title="Types" />
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeExtensionData.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DeserializeImmutableCollections" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample deserializes JSON into an immutable collection.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeImmutableCollections.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DeserializeMetadataPropertyHandling" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample deserializes JSON with <codeEntityReference>T:Newtonsoft.Json.MetadataPropertyHandling</codeEntityReference>
|
||||
set to ReadAhead so that metadata properties do not need to be at the start of an object.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeMetadataPropertyHandling.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="DeserializeWithDependencyInjection" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample deserializes JSON using dependency injection.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\DeserializeWithDependencyInjection.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializeDateFormatString" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample uses the DateFormatString
|
||||
setting to control how <codeEntityReference>T:System.DateTime</codeEntityReference> and <codeEntityReference>T:System.DateTimeOffset</codeEntityReference> are serialized.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\SerializeDateFormatString.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializeExtensionData" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample serializes an object with extension data into JSON.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\SerializeExtensionData.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<topic id="SerializeImmutableCollections" revisionNumber="1">
|
||||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<introduction>
|
||||
<para>This sample serializes an immutable collection into JSON.</para>
|
||||
</introduction>
|
||||
<section>
|
||||
<title>Sample</title>
|
||||
<content>
|
||||
<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\Samples\Serializer\SerializeImmutableCollections.cs" region="Usage" title="Usage" />
|
||||
</content>
|
||||
</section>
|
||||
</developerConceptualDocument>
|
||||
</topic>
|
||||
+17
-5
@@ -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" />
|
||||
@@ -12,7 +12,7 @@
|
||||
<Topic id="SerializingCollections" visible="True" title="Serializing Collections" />
|
||||
<Topic id="DatesInJSON" visible="True" title="Serializing Dates in JSON" />
|
||||
<Topic id="ReducingSerializedJSONSize" visible="True" title="Reducing Serialized JSON Size" />
|
||||
<Topic id="SerializingJSONFragments" visible="True" isSelected="true" title="Deserializing Partial JSON Fragments" />
|
||||
<Topic id="SerializingJSONFragments" visible="True" title="Deserializing Partial JSON Fragments" />
|
||||
<Topic id="ConditionalProperties" visible="True" title="Conditional Property Serialization" />
|
||||
<Topic id="ContractResolver" visible="True" title="Serialization using ContractResolver" />
|
||||
<Topic id="SerializationTracing" visible="True" title="Debugging with Serialization Tracing" />
|
||||
@@ -29,8 +29,8 @@
|
||||
<Topic id="ConvertingJSONandXML" visible="True" title="Converting between JSON and XML" />
|
||||
<Topic id="JsonNetVsDotNetSerializers" visible="True" title="Json.NET vs .NET Serializers" />
|
||||
<Topic id="JsonNetVsWindowsDataJson" visible="True" title="Json.NET vs Windows.Data.Json" />
|
||||
<Topic id="Samples" visible="True" title="Samples">
|
||||
<Topic id="d6e185d5-c992-44fc-8d8f-d9acc2bbec2f" visible="True" noFile="true" title="Serializing JSON">
|
||||
<Topic id="Samples" visible="True" isExpanded="true" title="Samples">
|
||||
<Topic id="d6e185d5-c992-44fc-8d8f-d9acc2bbec2f" visible="True" noFile="true" isExpanded="true" title="Serializing JSON">
|
||||
<Topic id="SerializeObject" visible="True" title="Serialize an Object" />
|
||||
<Topic id="SerializeCollection" visible="True" title="Serialize a Collection" />
|
||||
<Topic id="SerializeDictionary" visible="True" title="Serialize a Dictionary" />
|
||||
@@ -58,6 +58,7 @@
|
||||
<Topic id="SerializeDateFormatHandling" visible="True" title="DateFormatHandling setting" />
|
||||
<Topic id="SerializeDateTimeZoneHandling" visible="True" title="DateTimeZoneHandling setting" />
|
||||
<Topic id="SerializeTypeNameHandling" visible="True" title="TypeNameHandling setting" />
|
||||
<Topic id="DeserializeMetadataPropertyHandling" visible="True" title="MetadataPropertyHandling setting" />
|
||||
<Topic id="SerializeContractResolver" visible="True" title="ContractResolver setting" />
|
||||
<Topic id="TraceWriter" visible="True" title="TraceWriter setting" />
|
||||
<Topic id="ErrorHandlingEvent" visible="True" title="ErrorHandling setting" />
|
||||
@@ -80,8 +81,16 @@
|
||||
<Topic id="DefaultValueAttributeIgnore" visible="True" title="DefaultValueAttribute" />
|
||||
<Topic id="SerializationCallbackAttributes" visible="True" title="Serialization Callback Attributes" />
|
||||
<Topic id="DataContractAndDataMember" visible="True" title="DataContract and DataMember Attributes" />
|
||||
<Topic id="DeserializeWithDependencyInjection" visible="True" title="Deserialize with dependency injection" />
|
||||
<Topic id="DefaultSettings" visible="True" isSelected="true" title="Serialize with DefaultSettings" />
|
||||
<Topic id="SerializeImmutableCollections" visible="True" title="Serialize an immutable collection" />
|
||||
<Topic id="DeserializeImmutableCollections" visible="True" title="Deserialize an immutable collection" />
|
||||
<Topic id="SerializeExtensionData" visible="True" title="Serialize ExtensionData" />
|
||||
<Topic id="DeserializeExtensionData" visible="True" title="Deserialize ExtensionData" />
|
||||
<Topic id="DeserializeDateFormatString" visible="True" title="Deserialize DateFormatString" />
|
||||
<Topic id="SerializeDateFormatString" visible="True" title="Serialize DateFormatString" />
|
||||
</Topic>
|
||||
<Topic id="68b9fad2-5436-4e35-a734-9b9d751cc3e5" visible="True" noFile="true" title="LINQ to JSON">
|
||||
<Topic id="68b9fad2-5436-4e35-a734-9b9d751cc3e5" visible="True" noFile="true" isExpanded="true" title="LINQ to JSON">
|
||||
<Topic id="CreateJsonManually" visible="True" title="Create JSON manually" />
|
||||
<Topic id="CreateJsonCollectionInitializer" visible="True" title="Create JSON using Collection Initializers" />
|
||||
<Topic id="CreateJsonDeclaratively" visible="True" title="Create JSON declaratively with LINQ" />
|
||||
@@ -95,11 +104,13 @@
|
||||
<Topic id="DeserializeWithLinq" visible="True" title="Deserializing from JSON with LINQ" />
|
||||
<Topic id="SerializeWithLinq" visible="True" title="Serializing to JSON with LINQ" />
|
||||
<Topic id="ModifyJson" visible="True" title="Modifying JSON" />
|
||||
<Topic id="MergeJson" visible="True" title="Merging JSON" />
|
||||
<Topic id="QueryJson" visible="True" title="Querying JSON" />
|
||||
<Topic id="QueryJsonDynamic" visible="True" title="Querying JSON with dynamic" />
|
||||
<Topic id="QueryJsonLinq" visible="True" title="Querying JSON with LINQ" />
|
||||
<Topic id="QueryJsonSelectToken" visible="True" title="Querying JSON with SelectToken" />
|
||||
<Topic id="QueryJsonSelectTokenWithLinq" visible="True" title="Querying JSON with SelectToken and LINQ" />
|
||||
<Topic id="QueryJsonSelectTokenJsonPath" visible="True" title="Querying JSON with JSONPath" />
|
||||
<Topic id="ReadJson" visible="True" title="Read JSON from a file" />
|
||||
<Topic id="WriteToJsonFile" visible="True" title="Write JSON to a file" />
|
||||
<Topic id="ToObjectComplex" visible="True" title="Convert JSON to Collection" />
|
||||
@@ -108,6 +119,7 @@
|
||||
<Topic id="JValueCast" visible="True" title="Casting JValue" />
|
||||
<Topic id="JValueValue" visible="True" title="Using JValue.Value" />
|
||||
<Topic id="JObjectProperties" visible="True" title="Using JObject.Properties" />
|
||||
<Topic id="JTokenAnnotation" visible="True" title="Using LINQ to JSON Annotations" />
|
||||
<Topic id="DeepEquals" visible="True" title="Comparing JSON with JToken.DeepEquals" />
|
||||
<Topic id="Clone" visible="True" title="Cloning JSON with JToken.DeepClone" />
|
||||
<Topic id="ToString" visible="True" title="Write JSON text with JToken.ToString" />
|
||||
|
||||
@@ -100,6 +100,18 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="icons\favicon.ico" />
|
||||
<None Include="Samples\Serializer\SerializeImmutableCollections.aml" />
|
||||
<None Include="Samples\Serializer\SerializeExtensionData.aml" />
|
||||
<None Include="Samples\Serializer\SerializeDateFormatString.aml" />
|
||||
<None Include="Samples\Serializer\DeserializeWithDependencyInjection.aml" />
|
||||
<None Include="Samples\Serializer\DeserializeMetadataPropertyHandling.aml" />
|
||||
<None Include="Samples\Serializer\DeserializeImmutableCollections.aml" />
|
||||
<None Include="Samples\Serializer\DeserializeExtensionData.aml" />
|
||||
<None Include="Samples\Serializer\DeserializeDateFormatString.aml" />
|
||||
<None Include="Samples\Serializer\DefaultSettings.aml" />
|
||||
<None Include="Samples\Linq\QueryJsonSelectTokenJsonPath.aml" />
|
||||
<None Include="Samples\Linq\MergeJson.aml" />
|
||||
<None Include="Samples\Linq\JTokenAnnotation.aml" />
|
||||
<None Include="Samples\Json\CustomJsonWriter.aml" />
|
||||
<None Include="Samples\Json\CustomJsonReader.aml" />
|
||||
<None Include="Samples\Xml\ConvertXmlToJsonForceArray.aml" />
|
||||
|
||||
@@ -57,4 +57,4 @@ using System.Runtime.InteropServices;
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("7.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18131")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18216")]
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#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
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
|
||||
{
|
||||
[TestFixture]
|
||||
public class JTokenAnnotation : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
JObject o = JObject.Parse(@"{
|
||||
'name': 'Bill G',
|
||||
'age': 58,
|
||||
'country': 'United States',
|
||||
'employer': 'Microsoft'
|
||||
}");
|
||||
|
||||
o.AddAnnotation(new HashSet<string>());
|
||||
o.PropertyChanged += (sender, args) => o.Annotation<HashSet<string>>().Add(args.PropertyName);
|
||||
|
||||
o["age"] = 59;
|
||||
o["employer"] = "Bill & Melinda Gates Foundation";
|
||||
|
||||
|
||||
HashSet<string> changedProperties = o.Annotation<HashSet<string>>();
|
||||
// age
|
||||
// employer
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(true, changedProperties.Contains("age"));
|
||||
Assert.AreEqual(true, changedProperties.Contains("employer"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using LogService = Newtonsoft.Json.Tests.Serialization.LogManager;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class MergeJson : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
JObject o1 = JObject.Parse(@"{
|
||||
'FirstName': 'John',
|
||||
'LastName': 'Smith',
|
||||
'Enabled': false,
|
||||
'Roles': [ 'User' ]
|
||||
}");
|
||||
JObject o2 = JObject.Parse(@"{
|
||||
'Enabled': true,
|
||||
'Roles': [ 'User', 'Admin' ]
|
||||
}");
|
||||
|
||||
o1.Merge(o2, new JsonMergeSettings
|
||||
{
|
||||
// union array values together to avoid duplicates
|
||||
MergeArrayHandling = MergeArrayHandling.Union
|
||||
});
|
||||
|
||||
string json = o1.ToString();
|
||||
// {
|
||||
// "FirstName": "John",
|
||||
// "LastName": "Smith",
|
||||
// "Enabled": true,
|
||||
// "Roles": [
|
||||
// "User",
|
||||
// "Admin"
|
||||
// ]
|
||||
// }
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""FirstName"": ""John"",
|
||||
""LastName"": ""Smith"",
|
||||
""Enabled"": true,
|
||||
""Roles"": [
|
||||
""User"",
|
||||
""Admin""
|
||||
]
|
||||
}", json.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#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
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
|
||||
{
|
||||
[TestFixture]
|
||||
public class QueryJsonSelectTokenJsonPath : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
JObject o = JObject.Parse(@"{
|
||||
'Stores': [
|
||||
'Lambton Quay',
|
||||
'Willis Street'
|
||||
],
|
||||
'Manufacturers': [
|
||||
{
|
||||
'Name': 'Acme Co',
|
||||
'Products': [
|
||||
{
|
||||
'Name': 'Anvil',
|
||||
'Price': 50
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
'Name': 'Contoso',
|
||||
'Products': [
|
||||
{
|
||||
'Name': 'Elbow Grease',
|
||||
'Price': 99.95
|
||||
},
|
||||
{
|
||||
'Name': 'Headlight Fluid',
|
||||
'Price': 4
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}");
|
||||
|
||||
// manufacturer with the name 'Acme Co'
|
||||
JToken acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]");
|
||||
|
||||
Console.WriteLine(acme);
|
||||
// { "Name": "Acme Co", Products: [{ "Name": "Anvil", "Price": 50 }] }
|
||||
|
||||
// name of all products priced 50 and above
|
||||
IEnumerable<JToken> pricyProducts = o.SelectTokens("$..Products[?(@.Price >= 50)].Name");
|
||||
|
||||
foreach (JToken item in pricyProducts)
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
// Anvil
|
||||
// Elbow Grease
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Name"": ""Acme Co"",
|
||||
""Products"": [
|
||||
{
|
||||
""Name"": ""Anvil"",
|
||||
""Price"": 50
|
||||
}
|
||||
]
|
||||
}", acme.ToString());
|
||||
|
||||
Assert.AreEqual("Anvil", (string)pricyProducts.ElementAt(0));
|
||||
Assert.AreEqual("Elbow Grease", (string)pricyProducts.ElementAt(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
using Staff = Newtonsoft.Json.Tests.TestObjects.Employee;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DefaultSettings : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
// settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
Staff s = new Staff
|
||||
{
|
||||
FirstName = "Eric",
|
||||
LastName = "Example",
|
||||
BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
|
||||
Department = "IT",
|
||||
JobTitle = "Web Dude"
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(s);
|
||||
// {
|
||||
// "firstName": "Eric",
|
||||
// "lastName": "Example",
|
||||
// "birthDate": "1980-04-20T00:00:00Z",
|
||||
// "department": "IT",
|
||||
// "jobTitle": "Web Dude"
|
||||
// }
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""firstName"": ""Eric"",
|
||||
""lastName"": ""Example"",
|
||||
""birthDate"": ""1980-04-20T00:00:00Z"",
|
||||
""department"": ""IT"",
|
||||
""jobTitle"": ""Web Dude""
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeDateFormatString : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"[
|
||||
'7 December, 2009',
|
||||
'1 January, 2010',
|
||||
'10 February, 2010'
|
||||
]";
|
||||
|
||||
IList<DateTime> dateList = JsonConvert.DeserializeObject<IList<DateTime>>(json, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "d MMMM, yyyy"
|
||||
});
|
||||
|
||||
foreach (DateTime dateTime in dateList)
|
||||
{
|
||||
Console.WriteLine(dateTime.ToLongDateString());
|
||||
}
|
||||
// Monday, 07 December 2009
|
||||
// Friday, 01 January 2010
|
||||
// Wednesday, 10 February 2010
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(new DateTime(2009, 12, 7, 0, 0, 0, DateTimeKind.Utc), dateList[0]);
|
||||
Assert.AreEqual(new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Utc), dateList[1]);
|
||||
Assert.AreEqual(new DateTime(2010, 2, 10, 0, 0, 0, DateTimeKind.Utc), dateList[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeExtensionData : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class DirectoryAccount
|
||||
{
|
||||
// normal deserialization
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
// these properties are set in OnDeserialized
|
||||
public string UserName { get; set; }
|
||||
public string Domain { get; set; }
|
||||
|
||||
[JsonExtensionData]
|
||||
private IDictionary<string, JToken> _additionalData;
|
||||
|
||||
[OnDeserialized]
|
||||
private void OnDeserialized(StreamingContext context)
|
||||
{
|
||||
// SAMAccountName is not deserialized to any property
|
||||
// and so it is added to the extension data dictionary
|
||||
string samAccountName = (string)_additionalData["SAMAccountName"];
|
||||
|
||||
Domain = samAccountName.Split('\\')[0];
|
||||
UserName = samAccountName.Split('\\')[1];
|
||||
}
|
||||
|
||||
public DirectoryAccount()
|
||||
{
|
||||
_additionalData = new Dictionary<string, JToken>();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'DisplayName': 'John Smith',
|
||||
'SAMAccountName': 'contoso\\johns'
|
||||
}";
|
||||
|
||||
DirectoryAccount account = JsonConvert.DeserializeObject<DirectoryAccount>(json);
|
||||
|
||||
Console.WriteLine(account.DisplayName);
|
||||
// John Smith
|
||||
|
||||
Console.WriteLine(account.Domain);
|
||||
// contoso
|
||||
|
||||
Console.WriteLine(account.UserName);
|
||||
// johns
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual("John Smith", account.DisplayName);
|
||||
Assert.AreEqual("contoso", account.Domain);
|
||||
Assert.AreEqual("johns", account.UserName);
|
||||
}
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeImmutableCollections : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"[
|
||||
'One',
|
||||
'II',
|
||||
'3'
|
||||
]";
|
||||
|
||||
ImmutableList<string> l = JsonConvert.DeserializeObject<ImmutableList<string>>(json);
|
||||
|
||||
foreach (string s in l)
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
}
|
||||
// One
|
||||
// II
|
||||
// 3
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(3, l.Count);
|
||||
Assert.AreEqual("One", l[0]);
|
||||
Assert.AreEqual("II", l[1]);
|
||||
Assert.AreEqual("3", l[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
#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
|
||||
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using User = Newtonsoft.Json.Tests.Serialization.MetadataPropertyHandlingTests.User;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeMetadataPropertyHandling : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
try
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'Name': 'James',
|
||||
'Password': 'Password1',
|
||||
'$type': 'MyNamespace.User, MyAssembly'
|
||||
}";
|
||||
|
||||
object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
// $type no longer needs to be first
|
||||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
|
||||
});
|
||||
|
||||
User u = (User)o;
|
||||
|
||||
Console.WriteLine(u.Name);
|
||||
// James
|
||||
#endregion
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using LogService = Newtonsoft.Json.Tests.Serialization.LogManager;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeserializeWithDependencyInjection : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class AutofacContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly IContainer _container;
|
||||
|
||||
public AutofacContractResolver(IContainer container)
|
||||
{
|
||||
_container = container;
|
||||
}
|
||||
|
||||
protected override JsonObjectContract CreateObjectContract(Type objectType)
|
||||
{
|
||||
JsonObjectContract contract = base.CreateObjectContract(objectType);
|
||||
|
||||
// use Autofac to create types that have been registered with it
|
||||
if (_container.IsRegistered(objectType))
|
||||
contract.DefaultCreator = () => _container.Resolve(objectType);
|
||||
|
||||
return contract;
|
||||
}
|
||||
}
|
||||
|
||||
public class TaskController
|
||||
{
|
||||
private readonly ITaskRepository _repository;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public TaskController(ITaskRepository repository, ILogger logger)
|
||||
{
|
||||
_repository = repository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public ITaskRepository Repository
|
||||
{
|
||||
get { return _repository; }
|
||||
}
|
||||
|
||||
public ILogger Logger
|
||||
{
|
||||
get { return _logger; }
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
ContainerBuilder builder = new ContainerBuilder();
|
||||
builder.RegisterType<TaskRepository>().As<ITaskRepository>();
|
||||
builder.RegisterType<TaskController>();
|
||||
builder.Register(c => new LogService(new DateTime(2000, 12, 12))).As<ILogger>();
|
||||
|
||||
IContainer container = builder.Build();
|
||||
|
||||
AutofacContractResolver contractResolver = new AutofacContractResolver(container);
|
||||
|
||||
string json = @"{
|
||||
'Logger': {
|
||||
'Level':'Debug'
|
||||
}
|
||||
}";
|
||||
|
||||
// ITaskRespository and ILogger constructor parameters are injected by Autofac
|
||||
TaskController controller = JsonConvert.DeserializeObject<TaskController>(json, new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = contractResolver
|
||||
});
|
||||
|
||||
Console.WriteLine(controller.Repository.GetType().Name);
|
||||
// TaskRepository
|
||||
#endregion
|
||||
|
||||
Assert.IsNotNull(controller);
|
||||
Assert.IsNotNull(controller.Logger);
|
||||
|
||||
Assert.AreEqual(new DateTime(2000, 12, 12), controller.Logger.DateTime);
|
||||
Assert.AreEqual("Debug", controller.Logger.Level);
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeDateFormatString : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
IList<DateTime> dateList = new List<DateTime>
|
||||
{
|
||||
new DateTime(2009, 12, 7, 23, 10, 0, DateTimeKind.Utc),
|
||||
new DateTime(2010, 1, 1, 9, 0, 0, DateTimeKind.Utc),
|
||||
new DateTime(2010, 2, 10, 10, 0, 0, DateTimeKind.Utc)
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dateList, new JsonSerializerSettings
|
||||
{
|
||||
DateFormatString = "d MMMM, yyyy",
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
|
||||
Console.WriteLine(json);
|
||||
// [
|
||||
// "7 December, 2009",
|
||||
// "1 January, 2010",
|
||||
// "10 February, 2010"
|
||||
// ]
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
""7 December, 2009"",
|
||||
""1 January, 2010"",
|
||||
""10 February, 2010""
|
||||
]", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeExtensionData : TestFixtureBase
|
||||
{
|
||||
#region Types
|
||||
public class CustomerInvoice
|
||||
{
|
||||
// we're only modifing the tax rate
|
||||
public decimal TaxRate { get; set; }
|
||||
|
||||
// everything else gets stored here
|
||||
[JsonExtensionData]
|
||||
private IDictionary<string, JToken> _additionalData;
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Test]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
string json = @"{
|
||||
'HourlyRate': 150,
|
||||
'Hours': 40,
|
||||
'TaxRate': 0.125
|
||||
}";
|
||||
|
||||
CustomerInvoice invoice = JsonConvert.DeserializeObject<CustomerInvoice>(json);
|
||||
|
||||
// increase tax to 15%
|
||||
invoice.TaxRate = 0.15m;
|
||||
|
||||
string result = JsonConvert.SerializeObject(invoice);
|
||||
// {
|
||||
// "TaxRate": 0.15,
|
||||
// "HourlyRate": 150,
|
||||
// "Hours": 40
|
||||
// }
|
||||
#endregion
|
||||
|
||||
Assert.AreEqual(@"{""TaxRate"":0.15,""HourlyRate"":150,""Hours"":40}", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
#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
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Autofac;
|
||||
using Newtonsoft.Json.Tests.Serialization;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
|
||||
{
|
||||
[TestFixture]
|
||||
public class SerializeImmutableCollections : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void Example()
|
||||
{
|
||||
#region Usage
|
||||
ImmutableList<string> l = ImmutableList.CreateRange(new List<string>
|
||||
{
|
||||
"One",
|
||||
"II",
|
||||
"3"
|
||||
});
|
||||
|
||||
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
|
||||
// [
|
||||
// "One",
|
||||
// "II",
|
||||
// "3"
|
||||
// ]
|
||||
#endregion
|
||||
|
||||
StringAssert.AreEqual(@"[
|
||||
""One"",
|
||||
""II"",
|
||||
""3""
|
||||
]", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,8 +107,10 @@
|
||||
<Compile Include="Documentation\Samples\Linq\DeserializeWithLinq.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\FromObject.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\JObjectProperties.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\JTokenAnnotation.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\JValueCast.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\JValueValue.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\MergeJson.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\ModifyJson.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\ParseJsonAny.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\ParseJsonArray.cs" />
|
||||
@@ -116,7 +118,12 @@
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJson.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonDynamic.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonLinq.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonSelectToken.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonSelectToken.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonSelectTokenJsonPath.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Linq\QueryJsonSelectTokenWithLinq.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\ReadJson.cs" />
|
||||
<Compile Include="Documentation\Samples\Linq\ReadJTokenFromBson.cs" />
|
||||
@@ -139,11 +146,28 @@
|
||||
<Compile Include="Documentation\Samples\Schema\SaveJsonSchemaToFile.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\CustomContractResolver.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\CustomJsonConverter.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\CustomTraceWriter.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\CustomTraceWriter.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\DataContractAndDataMember.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DefaultSettings.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DefaultValueAttributeIgnore.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DefaultValueHandlingIgnore.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeAnonymousType.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeDateFormatString.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeExtensionData.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeImmutableCollections.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeWithDependencyInjection.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeMetadataPropertyHandling.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeDateFormatString.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeCollection.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeConstructorHandling.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\DeserializeCustomCreationConverter.cs" />
|
||||
@@ -178,6 +202,12 @@
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeDateFormatHandling.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeDateTimeZoneHandling.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeDictionary.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeExtensionData.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeImmutableCollections.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeObject.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeRawJson.cs" />
|
||||
<Compile Include="Documentation\Samples\Serializer\SerializeSerializationBinder.cs" />
|
||||
|
||||
@@ -76,4 +76,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("7.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18131")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18216")]
|
||||
|
||||
@@ -92,5 +92,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("7.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18131")]
|
||||
[assembly: AssemblyFileVersion("7.0.1.18216")]
|
||||
[assembly: CLSCompliant(true)]
|
||||
|
||||
Reference in New Issue
Block a user