diff --git a/Doc/DatesInJSON.aml b/Doc/DatesInJSON.aml index f0e97cfd..84699f69 100644 --- a/Doc/DatesInJSON.aml +++ b/Doc/DatesInJSON.aml @@ -35,10 +35,15 @@ older versions of Json.NET, then change the T:Newtonsoft.Json.DateFormatHandling setting to MicrosoftDateFormat. - Json.NET also has the T:Newtonsoft.Json.DateTimeZoneHandling - 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. + The T:Newtonsoft.Json.DateTimeZoneHandling setting can be + used to convert a DateTime's T:System.DateTimeKind when serializing. For example set + DateTimeZoneHandling to Utc to serialize all DateTimes as UTC dates. Note that this setting does not effect DateTimeOffsets. + 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 +custom date and time format syntax +https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx +_blank +.
diff --git a/Doc/Samples/Linq/JTokenAnnotation.aml b/Doc/Samples/Linq/JTokenAnnotation.aml new file mode 100644 index 00000000..b6df59a5 --- /dev/null +++ b/Doc/Samples/Linq/JTokenAnnotation.aml @@ -0,0 +1,14 @@ + + + + + This sample uses annotations with LINQ to JSON objects. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Linq/MergeJson.aml b/Doc/Samples/Linq/MergeJson.aml new file mode 100644 index 00000000..a76643e6 --- /dev/null +++ b/Doc/Samples/Linq/MergeJson.aml @@ -0,0 +1,14 @@ + + + + + This sample merges LINQ to JSON objects. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Linq/QueryJsonSelectTokenJsonPath.aml b/Doc/Samples/Linq/QueryJsonSelectTokenJsonPath.aml new file mode 100644 index 00000000..f0b78163 --- /dev/null +++ b/Doc/Samples/Linq/QueryJsonSelectTokenJsonPath.aml @@ -0,0 +1,18 @@ + + + + + This sample loads JSON and then queries values from it using M:Newtonsoft.Json.Linq.JToken.SelectToken(System.String) with a +JSONPath +http://stackoverflow.com/tags/jsonpath +_blank + query. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DefaultSettings.aml b/Doc/Samples/Serializer/DefaultSettings.aml new file mode 100644 index 00000000..9f6c9ea8 --- /dev/null +++ b/Doc/Samples/Serializer/DefaultSettings.aml @@ -0,0 +1,14 @@ + + + + + This sample serializes and deserializes JSON using P:Newtonsoft.Json.JsonConvert.DefaultSettings. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DeserializeDateFormatString.aml b/Doc/Samples/Serializer/DeserializeDateFormatString.aml new file mode 100644 index 00000000..07f063c4 --- /dev/null +++ b/Doc/Samples/Serializer/DeserializeDateFormatString.aml @@ -0,0 +1,15 @@ + + + + + This sample uses the DateFormatString + setting to control how T:System.DateTime and T:System.DateTimeOffset are deserialized. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DeserializeExtensionData.aml b/Doc/Samples/Serializer/DeserializeExtensionData.aml new file mode 100644 index 00000000..5c527bc3 --- /dev/null +++ b/Doc/Samples/Serializer/DeserializeExtensionData.aml @@ -0,0 +1,15 @@ + + + + + This sample deserializes JSON to an object with extension data. + +
+ Sample + + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DeserializeImmutableCollections.aml b/Doc/Samples/Serializer/DeserializeImmutableCollections.aml new file mode 100644 index 00000000..1048ff40 --- /dev/null +++ b/Doc/Samples/Serializer/DeserializeImmutableCollections.aml @@ -0,0 +1,14 @@ + + + + + This sample deserializes JSON into an immutable collection. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DeserializeMetadataPropertyHandling.aml b/Doc/Samples/Serializer/DeserializeMetadataPropertyHandling.aml new file mode 100644 index 00000000..16f6f80d --- /dev/null +++ b/Doc/Samples/Serializer/DeserializeMetadataPropertyHandling.aml @@ -0,0 +1,15 @@ + + + + + This sample deserializes JSON with T:Newtonsoft.Json.MetadataPropertyHandling + set to ReadAhead so that metadata properties do not need to be at the start of an object. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/DeserializeWithDependencyInjection.aml b/Doc/Samples/Serializer/DeserializeWithDependencyInjection.aml new file mode 100644 index 00000000..1495d96c --- /dev/null +++ b/Doc/Samples/Serializer/DeserializeWithDependencyInjection.aml @@ -0,0 +1,14 @@ + + + + + This sample deserializes JSON using dependency injection. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/SerializeDateFormatString.aml b/Doc/Samples/Serializer/SerializeDateFormatString.aml new file mode 100644 index 00000000..6b969895 --- /dev/null +++ b/Doc/Samples/Serializer/SerializeDateFormatString.aml @@ -0,0 +1,15 @@ + + + + + This sample uses the DateFormatString + setting to control how T:System.DateTime and T:System.DateTimeOffset are serialized. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/SerializeExtensionData.aml b/Doc/Samples/Serializer/SerializeExtensionData.aml new file mode 100644 index 00000000..65ac8365 --- /dev/null +++ b/Doc/Samples/Serializer/SerializeExtensionData.aml @@ -0,0 +1,14 @@ + + + + + This sample serializes an object with extension data into JSON. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/Samples/Serializer/SerializeImmutableCollections.aml b/Doc/Samples/Serializer/SerializeImmutableCollections.aml new file mode 100644 index 00000000..73c8673e --- /dev/null +++ b/Doc/Samples/Serializer/SerializeImmutableCollections.aml @@ -0,0 +1,14 @@ + + + + + This sample serializes an immutable collection into JSON. + +
+ Sample + + + +
+
+
\ No newline at end of file diff --git a/Doc/doc.content b/Doc/doc.content index 34f5d768..901ae13e 100644 --- a/Doc/doc.content +++ b/Doc/doc.content @@ -1,7 +1,7 @@  - + @@ -12,7 +12,7 @@ - + @@ -29,8 +29,8 @@ - - + + @@ -58,6 +58,7 @@ + @@ -80,8 +81,16 @@ + + + + + + + + - + @@ -95,11 +104,13 @@ + + @@ -108,6 +119,7 @@ + diff --git a/Doc/doc.shfbproj b/Doc/doc.shfbproj index 839e6b70..1f39bf5d 100644 --- a/Doc/doc.shfbproj +++ b/Doc/doc.shfbproj @@ -100,6 +100,18 @@ + + + + + + + + + + + + diff --git a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs index b798f917..b63ecd14 100644 --- a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs @@ -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")] diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/JTokenAnnotation.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/JTokenAnnotation.cs new file mode 100644 index 00000000..813b3823 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/JTokenAnnotation.cs @@ -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()); + o.PropertyChanged += (sender, args) => o.Annotation>().Add(args.PropertyName); + + o["age"] = 59; + o["employer"] = "Bill & Melinda Gates Foundation"; + + + HashSet changedProperties = o.Annotation>(); + // age + // employer + #endregion + + Assert.AreEqual(true, changedProperties.Contains("age")); + Assert.AreEqual(true, changedProperties.Contains("employer")); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/MergeJson.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/MergeJson.cs new file mode 100644 index 00000000..cd06abc0 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/MergeJson.cs @@ -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()); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/QueryJsonSelectTokenJsonPath.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/QueryJsonSelectTokenJsonPath.cs new file mode 100644 index 00000000..0c8a021e --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Linq/QueryJsonSelectTokenJsonPath.cs @@ -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 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)); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DefaultSettings.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DefaultSettings.cs new file mode 100644 index 00000000..6b833964 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DefaultSettings.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeDateFormatString.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeDateFormatString.cs new file mode 100644 index 00000000..b19b3ed2 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeDateFormatString.cs @@ -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 dateList = JsonConvert.DeserializeObject>(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]); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeExtensionData.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeExtensionData.cs new file mode 100644 index 00000000..f0c8f6fa --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeExtensionData.cs @@ -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 _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(); + } + } + #endregion + + [Test] + public void Example() + { + #region Usage + string json = @"{ + 'DisplayName': 'John Smith', + 'SAMAccountName': 'contoso\\johns' + }"; + + DirectoryAccount account = JsonConvert.DeserializeObject(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); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeImmutableCollections.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeImmutableCollections.cs new file mode 100644 index 00000000..e68b1851 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeImmutableCollections.cs @@ -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 l = JsonConvert.DeserializeObject>(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]); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeMetadataPropertyHandling.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeMetadataPropertyHandling.cs new file mode 100644 index 00000000..91fd0a94 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeMetadataPropertyHandling.cs @@ -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 + { + } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeWithDependencyInjection.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeWithDependencyInjection.cs new file mode 100644 index 00000000..3bc0812c --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/DeserializeWithDependencyInjection.cs @@ -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().As(); + builder.RegisterType(); + builder.Register(c => new LogService(new DateTime(2000, 12, 12))).As(); + + 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(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); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeDateFormatString.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeDateFormatString.cs new file mode 100644 index 00000000..53040f5b --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeDateFormatString.cs @@ -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 dateList = new List + { + 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); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeExtensionData.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeExtensionData.cs new file mode 100644 index 00000000..50f443bd --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeExtensionData.cs @@ -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 _additionalData; + } + #endregion + + [Test] + public void Example() + { + #region Usage + string json = @"{ + 'HourlyRate': 150, + 'Hours': 40, + 'TaxRate': 0.125 + }"; + + CustomerInvoice invoice = JsonConvert.DeserializeObject(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); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeImmutableCollections.cs b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeImmutableCollections.cs new file mode 100644 index 00000000..a44408b7 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/Samples/Serializer/SerializeImmutableCollections.cs @@ -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 l = ImmutableList.CreateRange(new List + { + "One", + "II", + "3" + }); + + string json = JsonConvert.SerializeObject(l, Formatting.Indented); + // [ + // "One", + // "II", + // "3" + // ] + #endregion + + StringAssert.AreEqual(@"[ + ""One"", + ""II"", + ""3"" +]", json); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index fe518cc8..f14664cd 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -107,8 +107,10 @@ + + @@ -116,7 +118,12 @@ - + + Code + + + Code + @@ -139,11 +146,28 @@ - + + Code + + + + Code + + + Code + + + Code + + + Code + + + @@ -178,6 +202,12 @@ + + Code + + + Code + diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 4ac7f2eb..4fd54f38 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.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")] diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 094c5d45..665d9ff3 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -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)]