From 6365a9f555b4e63e69d3af77c119818ca4801a84 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Oct 2012 10:38:51 +1300 Subject: [PATCH 1/3] -Fixed documentation build --- Doc/doc.shfbproj | 358 +++++++++++++++++++++++------------------------ 1 file changed, 179 insertions(+), 179 deletions(-) diff --git a/Doc/doc.shfbproj b/Doc/doc.shfbproj index 581fb1a9..467a9577 100644 --- a/Doc/doc.shfbproj +++ b/Doc/doc.shfbproj @@ -1,180 +1,180 @@ - - - - - Debug - AnyCPU - 2.0 - {fd19f68e-72c8-4576-9775-b4480b0db686} - 1.9.3.0 - - Documentation - Documentation - Documentation - - ..\Working\Documentation\ - Documentation - - - Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter, AutoDocumentDispose - InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected - - - - - - - - - - - Website - .NET 4.0.30319 - Json.NET - Documentation - vs2005 - MemberName - ..\Src\Newtonsoft.Json\bin\Release - - - - - - - The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework. - The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>. - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON. - The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding. - The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema. - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON. - - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement BSON. - - True - API Reference - - - - - - - - {@HelpFormatOutputPaths} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {@CachedFrameworkCommentList} - {@CommentFileList} - - - - - - False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - performance - performance - - - logo - logo - - - - + + + + + Debug + AnyCPU + 2.0 + {fd19f68e-72c8-4576-9775-b4480b0db686} + 1.9.3.0 + + Documentation + Documentation + Documentation + + ..\Working\Documentation\ + Documentation + + + Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter, AutoDocumentDispose + InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected + + + + + + + + + + + Website + .NET 4.0.30319 + Json.NET - Documentation + vs2005 + MemberName + ..\Src\Newtonsoft.Json\bin\Release + + + + + + + The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework. + The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>. + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON. + The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding. + The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema. + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON. + + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement BSON. + + True + API Reference + + + + + + + + {@HelpFormatOutputPaths} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {@CachedFrameworkCommentList} + {@CommentFileList} + + + + + + False + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + performance + performance + + + logo + logo + + + + \ No newline at end of file From d92b8ceeb56d938887e1a12b03f8b393b32ecbc8 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Oct 2012 13:24:47 +1300 Subject: [PATCH 2/3] -Documentation --- .gitignore | 5 +- Doc/ConditionalProperties.aml | 41 ++++ Doc/ContractResolver.aml | 123 ++++++------ Doc/Performance.aml | 166 ++++++++-------- Doc/doc.content | 55 +++--- Doc/doc.shfbproj | 5 +- .../ConditionalPropertiesTests.cs | 177 ++++++++++++++++++ .../Documentation/PerformanceTests.cs | 5 +- .../Newtonsoft.Json.Tests.csproj | 1 + .../Properties/AssemblyInfo.cs | 2 +- .../Properties/AssemblyInfo.cs | 2 +- 11 files changed, 413 insertions(+), 169 deletions(-) create mode 100644 Doc/ConditionalProperties.aml create mode 100644 Src/Newtonsoft.Json.Tests/Documentation/ConditionalPropertiesTests.cs diff --git a/.gitignore b/.gitignore index cd628f1a..646c93b1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ TestResults/ AppPackages/ *.suo -*.user \ No newline at end of file +*.user +_ReSharper.* +*.ReSharper.user +*.resharper.user \ No newline at end of file diff --git a/Doc/ConditionalProperties.aml b/Doc/ConditionalProperties.aml new file mode 100644 index 00000000..ba904a51 --- /dev/null +++ b/Doc/ConditionalProperties.aml @@ -0,0 +1,41 @@ + + + + + Json.NET has the ability to conditionally serialize properties by placing a ShouldSerialize method on a class. + This funtionality is similar to the +XmlSerializer ShouldSerialize feature +http://msdn.microsoft.com/en-us/library/53b8022e.aspx +_blank +. + +
+ ShouldSerialize + + To conditionally serialize a property add a boolean method with the same name as the property and then prefixed the method name + with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the + property will be serialized, if it returns false and the property will be skipped. + + + + + +
+
+ IContractResolver + + ShouldSerialize can also be set using an T:Newtonsoft.Json.Serialization.IContractResolver. + Conditionally serializing a property using an IContractResolver is useful if you don't want to place a ShouldSerialize method on a class + or you didn't declare the class and are unable to. + + + + +
+ + T:Newtonsoft.Json.JsonSerializer + T:Newtonsoft.Json.Serialization.IContractResolver + M:Newtonsoft.Json.Serialization.JsonProperty.ShouldSerialize + +
+
\ No newline at end of file diff --git a/Doc/ContractResolver.aml b/Doc/ContractResolver.aml index cd31ee1d..4589c6d5 100644 --- a/Doc/ContractResolver.aml +++ b/Doc/ContractResolver.aml @@ -1,54 +1,71 @@ - - - - - - The T:Newtonsoft.Json.Serialization.IContractResolver - interface provides a way to customize how the - JsonSerializer serializes and deserializes .NET objects to JSON. - Implementing the IContractResolver interface and then assigning an - instance to a JsonSerializer lets you control whether the object is - serialized as a JSON object or JSON array, what object members should be - serialized, how they are serialized and what they are called. - - -
- DefaultContractResolver - - - The T:Newtonsoft.Json.Serialization.DefaultContractResolver - is the default resolver used by the - serializer. It provides many avenues of extensibility in the form of - virtual methods that can be overriden. - -
-
- CamelCasePropertyNamesContractResolver - - - T:Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver - inherits from DefaultContractResolver and simply overrides the JSON - property name to be written in -camelcase -http://en.wikipedia.org/wiki/CamelCase -_blank -. - - - -
- - T:Newtonsoft.Json.Serialization.IContractResolver - T:Newtonsoft.Json.Serialization.DefaultContractResolver - T:Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver - -
+ + + + + + The T:Newtonsoft.Json.Serialization.IContractResolver + interface provides a way to customize how the + JsonSerializer serializes and deserializes .NET objects to JSON. + Implementing the IContractResolver interface and then assigning an + instance to a JsonSerializer lets you control whether the object is + serialized as a JSON object or JSON array, what object members should be + serialized, how they are serialized and what they are called. + + +
+ DefaultContractResolver + + + The T:Newtonsoft.Json.Serialization.DefaultContractResolver + is the default resolver used by the + serializer. It provides many avenues of extensibility in the form of + virtual methods that can be overriden. + +
+
+ CamelCasePropertyNamesContractResolver + + + T:Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver + inherits from DefaultContractResolver and simply overrides the JSON + property name to be written in +camelcase +http://en.wikipedia.org/wiki/CamelCase +_blank +. + + + +
+
+ Custom Examples + + Setting a T:Newtonsoft.Json.JsonConverter on a type + using an IContractResolver. + + + + + Conditionally serializing properties + ConditionalProperties.htm + _self + using an IContractResolver. + + + +
+ + T:Newtonsoft.Json.Serialization.IContractResolver + T:Newtonsoft.Json.Serialization.DefaultContractResolver + T:Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver + +
\ No newline at end of file diff --git a/Doc/Performance.aml b/Doc/Performance.aml index ce89d2ff..0115f609 100644 --- a/Doc/Performance.aml +++ b/Doc/Performance.aml @@ -1,84 +1,84 @@ - - - - - Out of the box Json.NET is faster than DataContractJsonSerializer and JavaScriptSerializer. - Here are some tips to make it go even faster. - -
- Optimize Memory Usage - - To keep an application consistantly fast it is important to minimize the - amount of time the .NET framework spends performing -garbage collection -http://msdn.microsoft.com/en-us/library/ms973837.aspx -_blank -. - Allocating too many objects, or allocating very large objects can slow down or even - halt an application while garbage collection is in progress. - - To minimize memory usage and the number of objects allocated Json.NET supports - serializing and deserializing directly to a stream. Reading or writing JSON a piece at a time instead of having - the entire JSON string loaded into memory is especially important when working with JSON - documents greater than 85kb in size to avoid the JSON string ending up in the -large object heap -http://msdn.microsoft.com/en-us/magazine/cc534993.aspx -_blank -. - - - - - -
-
- JsonConverters - - Passing a T:Newtonsoft.Json.JsonConverter to SerializeObject or DeserializeObject provides a simple way to completely - change how an object is serialized. There is however a small overhead; the CanConvert method is called for every - type to check whether serialization should be handled by that JsonConverter. - There are a couple of ways to continue to use JsonConverters without any overhead. The simplest way - is to specify the JsonConverter using the T:Newtonsoft.Json.JsonConverterAttribute. This attribute tells the serializer - to always use that converter when serializing and deserializing the type, without the check. - - - - If the class you want to convert isn't your own and you're unable to use an attribute a JsonConverter can still be used by - creating your own T:Newtonsoft.Json.Serialization.IContractResolver. - - - - The IContractResolver in the example above will set all DateTimes to use the JavaScriptDateConverter. - -
-
- Manually Serialize - - The absolute fastest way to read and write JSON is to use JsonTextReader and JsonTextWriter directly. Using a reader or writer - directly skips any of the overhead from a serializer such as reflection. - - - - If performance is important and you don't mind more code to get it then this is your best choice. - -
-
- Benchmarks - - - - - Json.NET Performance - - - -
- - T:Newtonsoft.Json.JsonSerializer - T:Newtonsoft.Json.JsonConverter - T:Newtonsoft.Json.JsonConverterAttribute - T:Newtonsoft.Json.JsonTextWriter - T:Newtonsoft.Json.JsonTextReader - -
+ + + + + Out of the box Json.NET is faster than DataContractJsonSerializer and JavaScriptSerializer. + Here are some tips to make it go even faster. + +
+ Optimize Memory Usage + + To keep an application consistantly fast it is important to minimize the + amount of time the .NET framework spends performing +garbage collection +http://msdn.microsoft.com/en-us/library/ms973837.aspx +_blank +. + Allocating too many objects, or allocating very large objects can slow down or even + halt an application while garbage collection is in progress. + + To minimize memory usage and the number of objects allocated Json.NET supports + serializing and deserializing directly to a stream. Reading or writing JSON a piece at a time instead of having + the entire JSON string loaded into memory is especially important when working with JSON + documents greater than 85kb in size to avoid the JSON string ending up in the +large object heap +http://msdn.microsoft.com/en-us/magazine/cc534993.aspx +_blank +. + + + + + +
+
+ JsonConverters + + Passing a T:Newtonsoft.Json.JsonConverter to SerializeObject or DeserializeObject provides a simple way to completely + change how an object is serialized. There is however a small overhead; the CanConvert method is called for every + value to check whether serialization should be handled by that JsonConverter. + There are a couple of ways to continue to use JsonConverters without any overhead. The simplest way + is to specify the JsonConverter using the T:Newtonsoft.Json.JsonConverterAttribute. This attribute tells the serializer + to always use that converter when serializing and deserializing the type, without the check. + + + + If the class you want to convert isn't your own and you're unable to use an attribute a JsonConverter can still be used by + creating your own T:Newtonsoft.Json.Serialization.IContractResolver. + + + + The IContractResolver in the example above will set all DateTimes to use the JavaScriptDateConverter. + +
+
+ Manually Serialize + + The absolute fastest way to read and write JSON is to use JsonTextReader/JsonTextWriter directly to manually serialize types. + Using a reader or writer directly skips any of the overhead from a serializer such as reflection. + + + + If performance is important and you don't mind more code to get it then this is your best choice. + +
+
+ Benchmarks + + + + + Json.NET Performance + + + +
+ + T:Newtonsoft.Json.JsonSerializer + T:Newtonsoft.Json.JsonConverter + T:Newtonsoft.Json.JsonConverterAttribute + T:Newtonsoft.Json.JsonTextWriter + T:Newtonsoft.Json.JsonTextReader + +
\ No newline at end of file diff --git a/Doc/doc.content b/Doc/doc.content index 47ec2e05..1b220554 100644 --- a/Doc/doc.content +++ b/Doc/doc.content @@ -1,28 +1,29 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Doc/doc.shfbproj b/Doc/doc.shfbproj index 467a9577..c7d9481b 100644 --- a/Doc/doc.shfbproj +++ b/Doc/doc.shfbproj @@ -8,7 +8,7 @@ AnyCPU 2.0 {fd19f68e-72c8-4576-9775-b4480b0db686} - 1.9.3.0 + 1.9.5.0 Documentation @@ -32,7 +32,7 @@ Website - .NET 4.0.30319 + .NET Framework 4.0 Json.NET - Documentation vs2005 MemberName @@ -137,6 +137,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Documentation/ConditionalPropertiesTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/ConditionalPropertiesTests.cs new file mode 100644 index 00000000..7ae89735 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Documentation/ConditionalPropertiesTests.cs @@ -0,0 +1,177 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET35 || NET20 || PORTABLE) +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Dynamic; +using System.IO; +using System.Linq; +using System.Net; +using System.Reflection; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Linq; +#if !NETFX_CORE +using NUnit.Framework; +#else +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; +using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; +using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; +#endif +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Tests.TestObjects; +using Newtonsoft.Json.Utilities; +using System.Globalization; + +namespace Newtonsoft.Json.Tests.Documentation +{ + public class Employee + { + public string Name { get; set; } + public Employee Manager { get; set; } + } + + #region ShouldSerializeContractResolver + public class ShouldSerializeContractResolver : DefaultContractResolver + { + public new static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver(); + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty property = base.CreateProperty(member, memberSerialization); + + if (property.DeclaringType == typeof(Employee) && property.PropertyName == "Manager") + { + property.ShouldSerialize = + instance => + { + Employee e = (Employee) instance; + return e.Manager != e; + }; + } + + return property; + } + } + #endregion + + [TestFixture] + public class ConditionalPropertiesTests : TestFixtureBase + { + #region EmployeeShouldSerializeExample + public class Employee + { + public string Name { get; set; } + public Employee Manager { get; set; } + + public bool ShouldSerializeManager() + { + // don't serialize the Manager property if an employee is their own manager + return (Manager != this); + } + } + #endregion + + [Test] + public void ShouldSerializeClassTest() + { + #region ShouldSerializeClassTest + Employee joe = new Employee(); + joe.Name = "Joe Employee"; + Employee mike = new Employee(); + mike.Name = "Mike Manager"; + + joe.Manager = mike; + + // mike is his own manager + // ShouldSerialize will skip this property + mike.Manager = mike; + + string json = JsonConvert.SerializeObject(new[] { joe, mike }, Formatting.Indented); + // [ + // { + // "Name": "Joe Employee", + // "Manager": { + // "Name": "Mike Manager" + // } + // }, + // { + // "Name": "Mike Manager" + // } + // ] + #endregion + + Assert.AreEqual(@"[ + { + ""Name"": ""Joe Employee"", + ""Manager"": { + ""Name"": ""Mike Manager"" + } + }, + { + ""Name"": ""Mike Manager"" + } +]", json); + } + + [Test] + public void ShouldSerializeContractResolverTest() + { + Newtonsoft.Json.Tests.Documentation.Employee joe = new Newtonsoft.Json.Tests.Documentation.Employee(); + joe.Name = "Joe Employee"; + Newtonsoft.Json.Tests.Documentation.Employee mike = new Newtonsoft.Json.Tests.Documentation.Employee(); + mike.Name = "Mike Manager"; + + joe.Manager = mike; + mike.Manager = mike; + + string json = JsonConvert.SerializeObject( + new[] {joe, mike}, + Formatting.Indented, + new JsonSerializerSettings + { + ContractResolver = ShouldSerializeContractResolver.Instance + }); + + Assert.AreEqual(@"[ + { + ""Name"": ""Joe Employee"", + ""Manager"": { + ""Name"": ""Mike Manager"" + } + }, + { + ""Name"": ""Mike Manager"" + } +]", json); + + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Documentation/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/PerformanceTests.cs index b0058366..eafc15eb 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/PerformanceTests.cs @@ -80,10 +80,13 @@ namespace Newtonsoft.Json.Tests.Documentation #region JsonConverterContractResolver public class ConverterContractResolver : DefaultContractResolver { + public new static readonly ConverterContractResolver Instance = new ConverterContractResolver(); + protected override JsonContract CreateContract(Type objectType) { JsonContract contract = base.CreateContract(objectType); + // this will only be called once and then cached if (objectType == typeof(DateTime) || objectType == typeof(DateTimeOffset)) contract.Converter = new JavaScriptDateTimeConverter(); @@ -117,7 +120,7 @@ namespace Newtonsoft.Json.Tests.Documentation { string json = JsonConvert.SerializeObject(new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc), new JsonSerializerSettings { - ContractResolver = new ConverterContractResolver() + ContractResolver = ConverterContractResolver.Instance }); Console.WriteLine(json); diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index afa8eb6d..a2083c25 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -117,6 +117,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 19683bd3..22046b2c 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -76,5 +76,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.10.15427")] +[assembly: AssemblyFileVersion("4.5.10.15431")] #endif diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 715b0c5c..3f86979e 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -90,7 +90,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.10.15427")] +[assembly: AssemblyFileVersion("4.5.10.15431")] #endif [assembly: CLSCompliant(true)] From 92ce080f247c0d0ee643ef448a209af6de245b91 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 1 Nov 2012 17:32:05 +1300 Subject: [PATCH 3/3] -Documentation -Performance --- Doc/JsonNetVsDotNetSerializers.aml | 454 ++++++++++++++++++ Doc/JsonNetVsWindowsDataJson.aml | 101 ++++ Doc/cross.png | Bin 0 -> 655 bytes Doc/doc.content | 58 +-- Doc/doc.shfbproj | 374 ++++++++------- Doc/jsonnetwindowsdatajson.png | Bin 0 -> 6537 bytes Doc/tick.png | Bin 0 -> 537 bytes .../JsonNetVsWindowsDataJsonTests.cs | 108 +++++ .../JsonTextWriterTest.cs | 2 + Src/Newtonsoft.Json.Tests/PerformanceTests.cs | 22 +- .../Properties/AssemblyInfo.cs | 2 +- Src/Newtonsoft.Json/JsonTextWriter.cs | 77 +-- Src/Newtonsoft.Json/JsonWriter.cs | 166 +++---- .../Properties/AssemblyInfo.cs | 2 +- 14 files changed, 1023 insertions(+), 343 deletions(-) create mode 100644 Doc/JsonNetVsDotNetSerializers.aml create mode 100644 Doc/JsonNetVsWindowsDataJson.aml create mode 100644 Doc/cross.png create mode 100644 Doc/jsonnetwindowsdatajson.png create mode 100644 Doc/tick.png create mode 100644 Src/Newtonsoft.Json.Tests/Documentation/JsonNetVsWindowsDataJsonTests.cs diff --git a/Doc/JsonNetVsDotNetSerializers.aml b/Doc/JsonNetVsDotNetSerializers.aml new file mode 100644 index 00000000..b6956e83 --- /dev/null +++ b/Doc/JsonNetVsDotNetSerializers.aml @@ -0,0 +1,454 @@ + + + + + Json.NET offers many features not found in the JavaScriptSerializer and DataContractSerializer that come with .NET. + +
+ Feature Comparison + + + + + + Json.NET + DataContractJsonSerializer + JavaScriptSerializer + + + + Supports JSON + + + + + + Supports BSON + + + + + + Supports JSON Schema + + + + + + Supports .NET 2.0 + + + + + + Supports .NET 3.5 + + + + + + Supports .NET 4.0 + + + + + + Supports Silverlight + + + + + + Supports Windows Phone + + + + + + Supports WinRT + + + + + + Open Source + + + + + + MIT License + + + + + + LINQ to JSON + + + + + + Thread Safe + + + + + + XPath-like JSON query syntax + + + + + + Indented JSON support + + + + + + + + Efficient dictionary serialization + http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net + _blank + + + + + + + + + + Nonsensical dictionary serialization + http://stackoverflow.com/questions/4559991/any-way-to-make-datacontractjsonserializer-serialize-dictionaries-properly + _blank + + + + + + + + Deserializes IList, IEnumerable, ICollection, IDictionary properties + + + + + + Serializes circular references + + + + + + Supports serializing objects by reference + + + + + + Deserializes polymorphic properties and collections + + + + + + Serializes and deserializes multidimensional arrays + + + + + + Supports including type names with JSON + + + + + + Globally customize serialization process + + + + + + Supports excluding null values when serializing + + + + + + Supports SerializationBinder + + + + + + Conditional property serialization + + + + + + Includes line number information in errors + + + + + + Converts XML to JSON and JSON to XML + + + + + + JSON Schema validation + + + + + + JSON Schema generation from .NET types + + + + + + Camel case JSON property names + + + + + + Non-default constructors support + + + + + + Serialization error handling + + + + + + Supports populating an existing object + + + + + + Efficiently serializes byte arrays as base64 text + + + + + + Handles NaN, Infinity, -Infinity and undefined + + + + + + Handles JavaScript constructors + + + + + + Serializes .NET 4.0 dynamic objects + + + + + + Serializes ISerializable objects + + + + + + Supports serializing enums to their text name + + + + + + JSON recursion limit support + + + + + + Attribute property name customization + + + + + + Attribute property order customization + + + + + + Attribute property required customization + + + + + + Supports ISO8601 dates + + + + + + Supports JavaScript constructor dates + + + + + + Supports Microsoft AJAX dates + + + + + + Unquoted property names support + + + + + + Raw JSON support + + + + + + Supports reading and writing comments + + + + + + Serializes anonymous types + + + + + + Deserializes anonymous types + + + + + + Opt-in mode serialization + + + + + + Opt-out mode serialization + + + + + + Field (Serializable) mode serialization + + + + + + Efficiently stream reading and writing JSON + + + + + + Single or double quote JSON content + + + + + + Supports overriding a type's serialization + + + + + + Supports OnDeserialized, OnSerializing, OnSerialized and OnDeserializing attributes + + + + + + Supports serializing private properties + + + + + + DataMember attribute support + + + + + + MetdataType attribute support + + + + + + DefaultValue attribute support + + + + + + Serializes DataSets and DataTables + + + + + + Serailizes Entity Framework + + + + + + Serializes nHibernate + + + + + + Case-insensitive property deserialization + + + + +
+
+
+
+ Benchmarks + + + + + Json.NET Performance + + + +
+
+
\ No newline at end of file diff --git a/Doc/JsonNetVsWindowsDataJson.aml b/Doc/JsonNetVsWindowsDataJson.aml new file mode 100644 index 00000000..33e0a5ea --- /dev/null +++ b/Doc/JsonNetVsWindowsDataJson.aml @@ -0,0 +1,101 @@ + + + + + Windows 8 introduces a new way to work with JSON via the +Windows.Data.Json +http://msdn.microsoft.com/en-us/library/windows/apps/xaml/br240639.aspx +_blank + namespace. Similar to LINQ to JSON in Json.NET + it defines classes that can be used to parse values, strings, objects, and arrays from + JSON text or serialize value types into JSON text. + Below is a comparison of Json.NET's LINQ to JSON to Window 8's Windows.Data.Json. + +
+ Exclusive Json.NET Features + + + + + Runs on .NET 2, .NET 3, .NET 4, Silverlight and Windows Phone 7 + + + + + Supports dynamic programming + + + + + Write indented JSON + + + + + Customize reading and writing JSON with JsonConverters + + + + + Read and write ISO8601 dates + + + + + Better LINQ support + + + + +
+
+ Creating JSON + + The big difference between the two libraries when creating JSON is Windows.Data.Json requires + string/integer/double values to be explicitly converted to JsonValue objects. + Note that there is a weird limitation to creating JSON with Windows.Data.Json: it doesn't + allow you to set properties to null or put null values in an array. + + + + +
+
+ Querying JSON + + Windows.Data.Json requires a value to be cast to its exact type with the GetObject/GetArray methods + before it can be used, making Windows.Data.Json's code verbose compared to LINQ to JSON. + + + + +
+
+ Performance + + Json.NET is slightly slower at writing JSON than Windows.Data.Json but considerably faster at parsing JSON. + + + + Json.NET and Windows.Data.Json Performance + + + +
+
+ Converting Between LINQ to JSON and Windows.Data.Json + + Json.NET supports converting between the types of the two libraries. + This feature is useful if you are working with another API but you want to use Json.NET inside your own code. + Use Overload:Newtonsoft.Json.Linq.JToken.FromObject to convert a Windows.Data.Json + value to LINQ to JSON and use Overload:Newtonsoft.Json.Linq.JToken.ToObject to convert LINQ to JSON to Windows.Data.Json. + + + + +
+ + N:Newtonsoft.Json.Linq + +
+
\ No newline at end of file diff --git a/Doc/cross.png b/Doc/cross.png new file mode 100644 index 0000000000000000000000000000000000000000..1514d51a3cf1b67e1c5b9ada36f1fd474e2d214a GIT binary patch literal 655 zcmV;A0&x9_P)uEoyT++I zn$b9r%cFfhHe2K68PkBu*@^<$y+7xQ$wJ~;c5aBx$R=xq*41Wo zhwQus_VOgm0hughj}MhOvs#{>Vg09Y8WxjWUJY5YW zJ?&8eG!59Cz=|E%Ns@013KLWOLV)CObIIj_5{>{#k%TEAMs_GbdDV`x-iYsGH z#=Z{USAQA>NY(}X7=3{K8# - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Doc/doc.shfbproj b/Doc/doc.shfbproj index c7d9481b..ce7cf2bd 100644 --- a/Doc/doc.shfbproj +++ b/Doc/doc.shfbproj @@ -1,181 +1,195 @@ - - - - - Debug - AnyCPU - 2.0 - {fd19f68e-72c8-4576-9775-b4480b0db686} - 1.9.5.0 - - Documentation - Documentation - Documentation - - ..\Working\Documentation\ - Documentation - - - Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter, AutoDocumentDispose - InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected - - - - - - - - - - - Website - .NET Framework 4.0 - Json.NET - Documentation - vs2005 - MemberName - ..\Src\Newtonsoft.Json\bin\Release - - - - - - - The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework. - The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>. - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON. - The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding. - The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema. - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON. - - The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement BSON. - - True - API Reference - - - - - - - - {@HelpFormatOutputPaths} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {@CachedFrameworkCommentList} - {@CommentFileList} - - - - - - False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - performance - performance - - - logo - logo - - - - + + + + + Debug + AnyCPU + 2.0 + {fd19f68e-72c8-4576-9775-b4480b0db686} + 1.9.5.0 + + Documentation + Documentation + Documentation + + ..\Working\Documentation\ + Documentation + + + Summary, Parameter, Returns, AutoDocumentCtors, Namespace, TypeParameter, AutoDocumentDispose + InheritedMembers, InheritedFrameworkMembers, Protected, SealedProtected + + + + + + + + + + + Website + .NET Framework 4.0 + Json.NET - Documentation + vs2005 + MemberName + ..\Src\Newtonsoft.Json\bin\Release + + + + + + + The <b>Newtonsoft.Json</b> namespace provides classes that are used to implement the core services of the framework. + The <b>Newtonsoft.Json.Converters</b> namespace provides classes that inherit from <a href="T_Newtonsoft_Json_JsonConverter.htm">JsonConverter</a>. + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement LINQ to JSON. + The <b>Newtonsoft.Json.Linq.ComponentModel</b> namespace provides classes for LINQ to JSON databinding. + The <b>Newtonsoft.Json.Schema</b> namespace provides classes that are used to implement JSON schema. + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used when serializing and deserializing JSON. + + The <b>Newtonsoft.Json.Linq</b> namespace provides classes that are used to implement BSON. + + True + API Reference + + + + + + + + {@HelpFormatOutputPaths} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {@CachedFrameworkCommentList} + {@CommentFileList} + + + + + + False + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + tick + tick + + + cross + cross + + + jsonnetwindowsdatajson + jsonnetwindowsdatajson + + + performance + performance + + + logo + logo + + + + \ No newline at end of file diff --git a/Doc/jsonnetwindowsdatajson.png b/Doc/jsonnetwindowsdatajson.png new file mode 100644 index 0000000000000000000000000000000000000000..76c7782a555b59d95a40e5f7586523eebee857f4 GIT binary patch literal 6537 zcmeHLcT`hNo4-j6B~k=}FCa*lfPf-Bf`r}-1`MGJiV~zt2Sp4>M<7zA8k!W5CcO%Z z(vhY@h)6Heq^Jm(jlS={kUtrKQcCZcf7aXHBWm0bIsAsTiQq)E8r_DEiWx|_Uw*l z4ghej>T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v^W{W>z0vn zA@0PY$sASJ=Z=drd0%Yxj5=55{J)!oBw_Jye<^SXSX@}Vdp2}xOQk5&eBIGZQs&x< zLx9@74lr~AvFWw@Vas>?MNn7AxnJ9=KH4e89dH7Mu9vZ+I#(&6A9N(TW2y@hp#MIL zf}8(2_71Nk@zevTxf5j2bhx_k6E(cxZTql_}&UY&O3xbz_Y~h~S==?uJHaphG~k^V`wY|- ztGe{`(8WXGE4JXJIzz52Z+*E!^X`J;9-`yjoaE^0v>q=y)Y1!>QYCj$_s7?+^>=(h zRfr_pcX2e>#b6@l4>sd%H;#uefC^Oq=zC&SNDnQiyx)ez#^th0@g&?dvs@xeYBV5p zQOWa?qZb*A4j%yQ+ z*i?d$`$=1{4^+T^B~+rXd0^(zi)a^oi5|2cT!IX zoD9l;86AIC-5^5c!iU;(?8A@O^B^UX4kak#ls)MaTN0?XG9MtKRsLs>=BDWaBWf@qsdMgH7jzp86n!~6LCaX%m0 zASDqG`Al~TUMLN0{aBG&VJrg(Fli4Hs{AQG2a)rMyf zFQK~pm1x3+7!nu%-M`k}``c>^00{o_1pjWJUTfl8mg=3qGEl8H@}^@w`VUx0_$uy4 z2FhRqKX}xI*?Tv1DJd8z#F#0c%*~rM30HE1@2o5m~}ZyoWhqv>ql{V z1ZGE0lgcoK^lx+eqc*rAX1Ky;Xx3U%u#zG!m-;eD1Qsn@kf3|F9qz~|95=&g3(7!X zB}JAT>RU;a%vaNOGnJ%e1=K6eAh43c(QN8RQ6~GP%O}Jju$~Ld*%`mO1p(); + #endregion +#endif + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index da896424..f6850326 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -713,6 +713,7 @@ _____'propertyName': NaN jsonWriter.WriteValue(data); jsonWriter.WriteValue((object)data); jsonWriter.WriteValue((byte[])null); + jsonWriter.WriteValue((Uri)null); jsonWriter.WriteEndArray(); } @@ -720,6 +721,7 @@ _____'propertyName': NaN ""SGVsbG8gd29ybGQu"", ""SGVsbG8gd29ybGQu"", ""SGVsbG8gd29ybGQu"", + null, null ]"; string result = sb.ToString(); diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index 7bcc6a18..01f578c0 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -64,8 +64,8 @@ namespace Newtonsoft.Json.Tests [TestFixture] public class PerformanceTests : TestFixtureBase { - private const int Iterations = 100; - //private const int Iterations = 5000; + //private const int Iterations = 100; + private const int Iterations = 5000; #region Data @@ -122,21 +122,19 @@ namespace Newtonsoft.Json.Tests TestClass test = CreateSerializationObject(); SerializeTests(test); - - Deserialize(); } private void SerializeTests(object value) { - BenchmarkSerializeMethod(SerializeMethod.DataContractSerializer, value); - BenchmarkSerializeMethod(SerializeMethod.BinaryFormatter, value); - BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value); - BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value); - BenchmarkSerializeMethod(SerializeMethod.JsonNet, value); - BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value); + //BenchmarkSerializeMethod(SerializeMethod.DataContractSerializer, value); + //BenchmarkSerializeMethod(SerializeMethod.BinaryFormatter, value); + //BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, value); + //BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, value); + //BenchmarkSerializeMethod(SerializeMethod.JsonNet, value); + //BenchmarkSerializeMethod(SerializeMethod.JsonNetLinq, value); BenchmarkSerializeMethod(SerializeMethod.JsonNetManual, value); - BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value); - BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value); + //BenchmarkSerializeMethod(SerializeMethod.JsonNetWithIsoConverter, value); + //BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, value); } [Test] diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 22046b2c..f5b0c6bd 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -76,5 +76,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.10.15431")] +[assembly: AssemblyFileVersion("4.5.10.15501")] #endif diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 18f012eb..cb390a98 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -148,7 +148,7 @@ namespace Newtonsoft.Json /// public override void WriteStartObject() { - base.WriteStartObject(); + InternalWriteStart(JsonToken.StartObject, JsonContainerType.Object); _writer.Write("{"); } @@ -158,7 +158,7 @@ namespace Newtonsoft.Json /// public override void WriteStartArray() { - base.WriteStartArray(); + InternalWriteStart(JsonToken.StartArray, JsonContainerType.Array); _writer.Write("["); } @@ -169,7 +169,7 @@ namespace Newtonsoft.Json /// The name of the constructor. public override void WriteStartConstructor(string name) { - base.WriteStartConstructor(name); + InternalWriteStart(JsonToken.StartConstructor, JsonContainerType.Constructor); _writer.Write("new "); _writer.Write(name); @@ -204,7 +204,7 @@ namespace Newtonsoft.Json /// The name of the property. public override void WritePropertyName(string name) { - base.WritePropertyName(name); + InternalWritePropertyName(name); JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName); @@ -259,7 +259,7 @@ namespace Newtonsoft.Json /// public override void WriteNull() { - base.WriteNull(); + InternalWriteNull(); WriteValueInternal(JsonConvert.Null, JsonToken.Null); } @@ -268,7 +268,7 @@ namespace Newtonsoft.Json /// public override void WriteUndefined() { - base.WriteUndefined(); + InternalWriteUndefined(); WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined); } @@ -278,7 +278,7 @@ namespace Newtonsoft.Json /// The raw JSON to write. public override void WriteRaw(string json) { - base.WriteRaw(json); + InternalWriteRaw(); _writer.Write(json); } @@ -289,7 +289,8 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(string value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.String); + if (value == null) WriteValueInternal(JsonConvert.Null, JsonToken.Null); else @@ -302,7 +303,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(int value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -313,7 +314,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public override void WriteValue(uint value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -323,7 +324,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(long value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -334,7 +335,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public override void WriteValue(ulong value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -344,7 +345,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(float value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Float); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float); } @@ -354,7 +355,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(double value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Float); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float); } @@ -364,7 +365,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(bool value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Boolean); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean); } @@ -374,7 +375,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(short value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -385,7 +386,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public override void WriteValue(ushort value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -395,8 +396,8 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(char value) { - base.WriteValue(value); - WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); + InternalWriteValue(JsonToken.String); + WriteValueInternal(JsonConvert.ToString(value), JsonToken.String); } /// @@ -405,7 +406,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(byte value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -416,7 +417,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public override void WriteValue(sbyte value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Integer); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer); } @@ -426,7 +427,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(decimal value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Float); WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float); } @@ -436,7 +437,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(DateTime value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Date); value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling); JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling); } @@ -447,10 +448,13 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(byte[] value) { - base.WriteValue(value); - - if (value != null) + if (value == null) { + WriteNull(); + } + else + { + InternalWriteValue(JsonToken.Bytes); _writer.Write(_quoteChar); Base64Encoder.Encode(value, 0, value.Length); Base64Encoder.Flush(); @@ -465,7 +469,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(DateTimeOffset value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.Date); WriteValueInternal(JsonConvert.ToString(value, DateFormatHandling), JsonToken.Date); } #endif @@ -476,7 +480,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(Guid value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.String); WriteValueInternal(JsonConvert.ToString(value), JsonToken.String); } @@ -486,7 +490,7 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(TimeSpan value) { - base.WriteValue(value); + InternalWriteValue(JsonToken.String); WriteValueInternal(JsonConvert.ToString(value), JsonToken.String); } @@ -496,8 +500,15 @@ namespace Newtonsoft.Json /// The value to write. public override void WriteValue(Uri value) { - base.WriteValue(value); - WriteValueInternal(JsonConvert.ToString(value), JsonToken.String); + if (value == null) + { + WriteNull(); + } + else + { + InternalWriteValue(JsonToken.String); + WriteValueInternal(JsonConvert.ToString(value), JsonToken.String); + } } #endregion @@ -507,7 +518,7 @@ namespace Newtonsoft.Json /// Text to place inside the comment. public override void WriteComment(string text) { - base.WriteComment(text); + InternalWriteComment(); _writer.Write("/*"); _writer.Write(text); @@ -520,7 +531,7 @@ namespace Newtonsoft.Json /// The string of white space characters. public override void WriteWhitespace(string ws) { - base.WriteWhitespace(ws); + InternalWriteWhitespace(ws); _writer.Write(ws); } diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 583d73d9..3072725c 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -54,7 +54,6 @@ namespace Newtonsoft.Json Array, ConstructorStart, Constructor, - Bytes, Closed, Error } @@ -69,7 +68,7 @@ namespace Newtonsoft.Json /* StartObject */new[]{ State.ObjectStart, State.ObjectStart, State.Error, State.Error, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.ObjectStart, State.Error, State.Error }, /* StartArray */new[]{ State.ArrayStart, State.ArrayStart, State.Error, State.Error, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.ArrayStart, State.Error, State.Error }, /* StartConstructor */new[]{ State.ConstructorStart, State.ConstructorStart, State.Error, State.Error, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.Error, State.Error }, -/* StartProperty */new[]{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error }, +/* Property */new[]{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error }, /* Comment */new[]{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Raw */new[]{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Value (this will be copied) */new[]{ State.Start, State.Object, State.Error, State.Error, State.Array, State.Array, State.Constructor, State.Constructor, State.Error, State.Error } @@ -157,7 +156,7 @@ namespace Newtonsoft.Json return JsonPosition.BuildPath(positions); } } - + /// /// Gets the state of the writer. /// @@ -316,10 +315,15 @@ namespace Newtonsoft.Json /// Writes the beginning of a Json object. /// public virtual void WriteStartObject() + { + InternalWriteStart(JsonToken.StartObject, JsonContainerType.Object); + } + + internal void InternalWriteStart(JsonToken token, JsonContainerType container) { UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.StartObject); - Push(JsonContainerType.Object); + AutoComplete(token); + Push(container); } /// @@ -327,7 +331,7 @@ namespace Newtonsoft.Json /// public virtual void WriteEndObject() { - AutoCompleteClose(JsonContainerType.Object); + InternalWriteEnd(JsonContainerType.Object); } /// @@ -335,9 +339,7 @@ namespace Newtonsoft.Json /// public virtual void WriteStartArray() { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.StartArray); - Push(JsonContainerType.Array); + InternalWriteStart(JsonToken.StartArray, JsonContainerType.Array); } /// @@ -345,7 +347,7 @@ namespace Newtonsoft.Json /// public virtual void WriteEndArray() { - AutoCompleteClose(JsonContainerType.Array); + InternalWriteEnd(JsonContainerType.Array); } /// @@ -354,9 +356,7 @@ namespace Newtonsoft.Json /// The name of the constructor. public virtual void WriteStartConstructor(string name) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.StartConstructor); - Push(JsonContainerType.Constructor); + InternalWriteStart(JsonToken.StartConstructor, JsonContainerType.Constructor); } /// @@ -364,7 +364,12 @@ namespace Newtonsoft.Json /// public virtual void WriteEndConstructor() { - AutoCompleteClose(JsonContainerType.Constructor); + InternalWriteEnd(JsonContainerType.Constructor); + } + + internal void InternalWriteEnd(JsonContainerType container) + { + AutoCompleteClose(container); } /// @@ -372,6 +377,11 @@ namespace Newtonsoft.Json /// /// The name of the property. public virtual void WritePropertyName(string name) + { + InternalWritePropertyName(name); + } + + internal void InternalWritePropertyName(string name) { _currentPosition.PropertyName = name; AutoComplete(JsonToken.PropertyName); @@ -564,21 +574,6 @@ namespace Newtonsoft.Json } } - private JsonContainerType GetTypeForCloseToken(JsonToken token) - { - switch (token) - { - case JsonToken.EndObject: - return JsonContainerType.Object; - case JsonToken.EndArray: - return JsonContainerType.Array; - case JsonToken.EndConstructor: - return JsonContainerType.Constructor; - default: - throw JsonWriterException.Create(this, "No type for token: " + token, null); - } - } - private JsonToken GetCloseTokenForType(JsonContainerType type) { switch (type) @@ -699,22 +694,16 @@ namespace Newtonsoft.Json { WriteValueDelimiter(); } - else if (_currentState == State.Property) - { - if (_formatting == Formatting.Indented) - WriteIndentSpace(); - } if (_formatting == Formatting.Indented) { - WriteState writeState = WriteState; + if (_currentState == State.Property) + WriteIndentSpace(); // don't indent a property when it is the first token to be written (i.e. at the start) - if ((tokenBeingWritten == JsonToken.PropertyName && writeState != WriteState.Start) || - writeState == WriteState.Array || writeState == WriteState.Constructor) - { + if ((_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.Constructor || _currentState == State.ConstructorStart) + || (tokenBeingWritten == JsonToken.PropertyName && _currentState != State.Start)) WriteIndent(); - } } _currentState = newState; @@ -725,6 +714,11 @@ namespace Newtonsoft.Json /// Writes a null value. /// public virtual void WriteNull() + { + InternalWriteNull(); + } + + internal void InternalWriteNull() { UpdateScopeWithFinishedValue(); AutoComplete(JsonToken.Null); @@ -734,6 +728,11 @@ namespace Newtonsoft.Json /// Writes an undefined value. /// public virtual void WriteUndefined() + { + InternalWriteUndefined(); + } + + internal void InternalWriteUndefined() { UpdateScopeWithFinishedValue(); AutoComplete(JsonToken.Undefined); @@ -744,6 +743,11 @@ namespace Newtonsoft.Json /// /// The raw JSON to write. public virtual void WriteRaw(string json) + { + InternalWriteRaw(); + } + + internal void InternalWriteRaw() { } @@ -764,9 +768,14 @@ namespace Newtonsoft.Json /// /// The value to write. public virtual void WriteValue(string value) + { + InternalWriteValue(JsonToken.String); + } + + internal void InternalWriteValue(JsonToken token) { UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.String); + AutoComplete(token); } /// @@ -775,8 +784,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(int value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -786,8 +794,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public virtual void WriteValue(uint value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -796,8 +803,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(long value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -807,8 +813,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public virtual void WriteValue(ulong value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -817,8 +822,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(float value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Float); + InternalWriteValue(JsonToken.Float); } /// @@ -827,8 +831,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(double value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Float); + InternalWriteValue(JsonToken.Float); } /// @@ -837,8 +840,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(bool value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Boolean); + InternalWriteValue(JsonToken.Boolean); } /// @@ -847,8 +849,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(short value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -858,8 +859,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public virtual void WriteValue(ushort value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -868,8 +868,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(char value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.String); + InternalWriteValue(JsonToken.String); } /// @@ -878,8 +877,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(byte value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -889,8 +887,7 @@ namespace Newtonsoft.Json [CLSCompliant(false)] public virtual void WriteValue(sbyte value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Integer); + InternalWriteValue(JsonToken.Integer); } /// @@ -899,8 +896,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(decimal value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Float); + InternalWriteValue(JsonToken.Float); } /// @@ -909,8 +905,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(DateTime value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Date); + InternalWriteValue(JsonToken.Date); } #if !PocketPC && !NET20 @@ -920,8 +915,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(DateTimeOffset value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Date); + InternalWriteValue(JsonToken.Date); } #endif @@ -931,8 +925,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(Guid value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.String); + InternalWriteValue(JsonToken.String); } /// @@ -941,8 +934,7 @@ namespace Newtonsoft.Json /// The value to write. public virtual void WriteValue(TimeSpan value) { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.String); + InternalWriteValue(JsonToken.String); } /// @@ -1162,14 +1154,9 @@ namespace Newtonsoft.Json public virtual void WriteValue(byte[] value) { if (value == null) - { WriteNull(); - } else - { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.Bytes); - } + InternalWriteValue(JsonToken.Bytes); } /// @@ -1179,14 +1166,9 @@ namespace Newtonsoft.Json public virtual void WriteValue(Uri value) { if (value == null) - { WriteNull(); - } else - { - UpdateScopeWithFinishedValue(); - AutoComplete(JsonToken.String); - } + InternalWriteValue(JsonToken.String); } /// @@ -1297,7 +1279,11 @@ namespace Newtonsoft.Json /// Text to place inside the comment. public virtual void WriteComment(string text) { - UpdateScopeWithFinishedValue(); + InternalWriteComment(); + } + + internal void InternalWriteComment() + { AutoComplete(JsonToken.Comment); } @@ -1306,6 +1292,11 @@ namespace Newtonsoft.Json /// /// The string of white space characters. public virtual void WriteWhitespace(string ws) + { + InternalWriteWhitespace(ws); + } + + internal void InternalWriteWhitespace(string ws) { if (ws != null) { @@ -1314,7 +1305,6 @@ namespace Newtonsoft.Json } } - void IDisposable.Dispose() { Dispose(true); diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 3f86979e..3db07c51 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -90,7 +90,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.5.10.15431")] +[assembly: AssemblyFileVersion("4.5.10.15501")] #endif [assembly: CLSCompliant(true)]