diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec index 6d811062..c9746031 100644 --- a/Build/Newtonsoft.Json.nuspec +++ b/Build/Newtonsoft.Json.nuspec @@ -2,7 +2,7 @@ Newtonsoft.Json - 5.0.1 + 5.0.2 Json.NET Json.NET is a popular high-performance JSON framework for .NET James Newton-King diff --git a/Build/build.ps1 b/Build/build.ps1 index 02e306af..9b8af29c 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -1,7 +1,7 @@ properties { - $zipFileName = "Json50r1.zip" + $zipFileName = "Json50r2.zip" $majorVersion = "4.5" - $majorWithReleaseVersion = "5.0.1" + $majorWithReleaseVersion = "5.0.2" $version = GetVersion $majorWithReleaseVersion $signAssemblies = $false $signKeyPath = "D:\Development\Releases\newtonsoft.snk" @@ -18,10 +18,9 @@ $workingDir = "$baseDir\Working" $builds = @( @{Name = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net45"; NuGetDir = "net45"; Framework="net-4.0"; Sign=$true}, - @{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net45+sl5+windowsphone8+win8"; Framework="net-4.0"; Sign=$true}, + @{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net45+wp80+win8"; Framework="net-4.0"; Sign=$true}, + @{Name = "Newtonsoft.Json.Portable40"; TestsName = "Newtonsoft.Json.Tests.Portable40"; Constants="PORTABLE40"; FinalDir="Portable40"; NuGetDir = "portable-net40+sl4+wp7+win8"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "netcore45"; Framework="net-4.5"; Sign=$true}, - @{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "windowsphone8"; Framework="net-4.0"; Sign=$true}, - @{Name = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl5"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net40"; TestsName = "Newtonsoft.Json.Tests.Net40"; Constants="NET40"; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net20"; TestsName = "Newtonsoft.Json.Tests.Net20"; Constants="NET20"; FinalDir="Net20"; NuGetDir = "net20"; Framework="net-2.0"; Sign=$true} @@ -72,7 +71,7 @@ task Package -depends Build { $name = $build.TestsName $finalDir = $build.FinalDir - robocopy "$sourceDir\Newtonsoft.Json\bin\Release\$finalDir" $workingDir\Package\Bin\$finalDir /NP /XO /XF *.pri | Out-Default + robocopy "$sourceDir\Newtonsoft.Json\bin\Release\$finalDir" $workingDir\Package\Bin\$finalDir *.dll *.pdb *.xml /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default } if ($buildNuGet) @@ -90,7 +89,7 @@ task Package -depends Build { foreach ($frameworkDir in $frameworkDirs) { - robocopy "$sourceDir\Newtonsoft.Json\bin\Release\$finalDir" $workingDir\NuGet\lib\$frameworkDir /NP /XO /XF *.pri | Out-Default + robocopy "$sourceDir\Newtonsoft.Json\bin\Release\$finalDir" $workingDir\NuGet\lib\$frameworkDir *.dll *.pdb *.xml /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default } } } @@ -113,7 +112,7 @@ task Package -depends Build { } Copy-Item -Path $docDir\readme.txt -Destination $workingDir\Package\ - Copy-Item -Path $docDir\versions.txt -Destination $workingDir\Package\Bin\ + Copy-Item -Path $docDir\license.txt -Destination $workingDir\Package\ robocopy $sourceDir $workingDir\Package\Source\Src /MIR /NP /XD .svn bin obj TestResults AppPackages /XF *.suo *.user | Out-Default robocopy $buildDir $workingDir\Package\Source\Build /MIR /NP /XD .svn | Out-Default diff --git a/Doc/license.txt b/Doc/license.txt new file mode 100644 index 00000000..0c2edce8 --- /dev/null +++ b/Doc/license.txt @@ -0,0 +1,18 @@ +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. \ No newline at end of file diff --git a/Doc/readme.txt b/Doc/readme.txt index c1f7899b..29dea6c8 100644 --- a/Doc/readme.txt +++ b/Doc/readme.txt @@ -24,31 +24,28 @@ http://james.newtonking.com/projects/json/help/ Versions: -Json.NET comes in different versions for the various .NET frameworks. +Json.NET has different libaries for the various .NET Framework versions. -Net45: - .NET latest + .NET latest (4.5) -Net40: - .NET 4.0, Mono + .NET 4.0 -Net35: - .NET 3.5 SP1 + .NET 3.5 -Net20: .NET 2.0 --Silverlight: - Silverlight 5 - --WindowsPhone: - Windows Phone 8 - --WinRT +-WinRT: Windows 8 Store --Portable - Portable Class Library (.NET 4.5, SL5, WP8, Win8 Store) +-Portable45: + .NET 4.5, Windows Phone 8, Windows 8 Store + +-Portable40: + .NET 4.0, Windows Phone 7, Windows 8 Store, Silverlight 4 Notes: @@ -56,35 +53,4 @@ Notes: Microsoft stopped support for the Compact Framework in Visual Studio 2010. For a Compact Framework 3.5 build download Json.NET 3.5. -For a Silverlight 3 build download Json.NET 3.5. - -For a Silverlight 4 or Windows Phone 7 build download Json.NET 4.5. - - -Instructions: - - 1. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive's /bin - directory into your own applications. - 2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project. - - -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. \ No newline at end of file +For a Silverlight 3.0 build download Json.NET 3.5. \ No newline at end of file diff --git a/Doc/versions.txt b/Doc/versions.txt deleted file mode 100644 index 7ecb9e25..00000000 --- a/Doc/versions.txt +++ /dev/null @@ -1,35 +0,0 @@ -Versions: - -Json.NET comes in different versions for the various .NET frameworks. - --Net40: - .NET latest (4.0 & 4.5) - --Net35: - .NET 3.5 SP1 - --Net20: - .NET 2.0 - --Silverlight: - Silverlight 4 & 5 - --WindowsPhone: - Windows Phone 7 & 7.1 - --WinRT: - Windows 8 Store, WinRT 4.5 - --Portable: - .NET 4.0, Silverlight 4, Windows Phone 7, WinRT 4.5 - - -Notes: - -The Silverlight and Windows Phone builds are not strong-named. -Use the assemblies in the Signed sub-directories for the strong-named versions. - -Microsoft stopped support for the Compact Framework in Visual Studio 2010. -For a Compact Framework 3.5 build download Json.NET 3.5. - -For a Silverlight 3.0 build download Json.NET 3.5. \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Portable40.sln b/Src/Newtonsoft.Json.Portable40.sln new file mode 100644 index 00000000..8b361b36 --- /dev/null +++ b/Src/Newtonsoft.Json.Portable40.sln @@ -0,0 +1,32 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lib", "Lib", "{620042D9-2753-48F5-BEDE-3905248781D2}" + ProjectSection(SolutionItems) = preProject + Lib\NUnit\DotNet\nunit.framework.dll = Lib\NUnit\DotNet\nunit.framework.dll + Lib\NUnit\DotNet\nunit.framework.xml = Lib\NUnit\DotNet\nunit.framework.xml + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Portable40", "Newtonsoft.Json\Newtonsoft.Json.Portable40.csproj", "{959F7F85-C98B-4876-971A-9036224578E4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Tests.Portable40", "Newtonsoft.Json.Tests\Newtonsoft.Json.Tests.Portable40.csproj", "{3E6E2335-B079-4B5B-A65A-9D586914BCBB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {959F7F85-C98B-4876-971A-9036224578E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {959F7F85-C98B-4876-971A-9036224578E4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {959F7F85-C98B-4876-971A-9036224578E4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {959F7F85-C98B-4876-971A-9036224578E4}.Release|Any CPU.Build.0 = Release|Any CPU + {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs index 3c4aba94..50404f7c 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs @@ -24,6 +24,7 @@ #endregion using System; +using System.Collections.Generic; using System.Globalization; #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) using System.Numerics; @@ -70,7 +71,7 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadSingleObject() { - byte[] data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00"); + byte[] data = HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -94,10 +95,80 @@ namespace Newtonsoft.Json.Tests.Bson Assert.AreEqual(JsonToken.None, reader.TokenType); } + [Test] + public void ReadGuid_Text() + { + byte[] data = HexToBytes("31-00-00-00-02-30-00-25-00-00-00-64-38-32-31-65-65-64-37-2D-34-62-35-63-2D-34-33-63-39-2D-38-61-63-32-2D-36-39-32-38-65-35-37-39-62-37-30-35-00-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("d821eed7-4b5c-43c9-8ac2-6928e579b705", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(reader.Read()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + ms = new MemoryStream(data); + reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + JsonSerializer serializer = new JsonSerializer(); + IList l = serializer.Deserialize>(reader); + + Assert.AreEqual(1, l.Count); + Assert.AreEqual(new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"), l[0]); + } + + [Test] + public void ReadGuid_Bytes() + { + byte[] data = HexToBytes("1D-00-00-00-05-30-00-10-00-00-00-04-D7-EE-21-D8-5C-4B-C9-43-8A-C2-69-28-E5-79-B7-05-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"); + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEqual(g.ToByteArray(), (byte[])reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(reader.Read()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(reader.Read()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + ms = new MemoryStream(data); + reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + JsonSerializer serializer = new JsonSerializer(); + IList l = serializer.Deserialize>(reader); + + Assert.AreEqual(1, l.Count); + Assert.AreEqual(g, l[0]); + } + [Test] public void ReadDouble() { - byte[] data = MiscellaneousUtils.HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); + byte[] data = HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -121,7 +192,7 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadDouble_Decimal() { - byte[] data = MiscellaneousUtils.HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); + byte[] data = HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -146,10 +217,12 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadValues() { - byte[] data = MiscellaneousUtils.HexToBytes("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00"); + byte[] data = HexToBytes("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); +#pragma warning disable 612,618 reader.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 reader.ReadRootValueAsArray = true; reader.DateTimeKindHandling = DateTimeKind.Utc; @@ -236,7 +309,7 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadObjectBsonFromSite() { - byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -284,7 +357,7 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadArrayBsonFromSite() { - byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -330,7 +403,7 @@ namespace Newtonsoft.Json.Tests.Bson "Could not convert string to integer: a. Path '[0]'.", () => { - byte[] data = MiscellaneousUtils.HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -354,11 +427,13 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadBytes() { - byte[] data = MiscellaneousUtils.HexToBytes("2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"); + byte[] data = HexToBytes("2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms, true, DateTimeKind.Utc); +#pragma warning disable 612,618 reader.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 Assert.AreEqual(true, reader.ReadRootValueAsArray); Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling); @@ -395,7 +470,7 @@ namespace Newtonsoft.Json.Tests.Bson [Test] public void ReadOid() { - byte[] data = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + byte[] data = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -409,7 +484,7 @@ namespace Newtonsoft.Json.Tests.Bson Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); - CollectionAssert.AreEquivalent(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001"), (byte[])reader.Value); + CollectionAssert.AreEquivalent(HexToBytes("4ABBED9D1D8B0F0218000001"), (byte[])reader.Value); Assert.AreEqual(typeof(byte[]), reader.ValueType); Assert.IsTrue(reader.Read()); @@ -433,7 +508,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "82-00-00-00-07-5F-69-64-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -447,7 +522,7 @@ namespace Newtonsoft.Json.Tests.Bson Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.Bytes, reader.TokenType); - CollectionAssert.AreEquivalent(MiscellaneousUtils.HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"), (byte[])reader.Value); + CollectionAssert.AreEquivalent(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"), (byte[])reader.Value); Assert.AreEqual(typeof(byte[]), reader.ValueType); Assert.IsTrue(reader.Read()); @@ -492,10 +567,12 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); BsonReader reader = new BsonReader(new MemoryStream(data)); +#pragma warning disable 612,618 reader.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 JObject o = (JObject)JToken.ReadFrom(reader); Assert.AreEqual(3, o.Count); @@ -505,27 +582,31 @@ namespace Newtonsoft.Json.Tests.Bson o.WriteTo(writer); writer.Flush(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(hexdoc, bson); } [Test] public void OidAndBytesAreEqual() { - byte[] data1 = MiscellaneousUtils.HexToBytes( + byte[] data1 = HexToBytes( "82-00-00-00-07-5F-69-64-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"); BsonReader reader1 = new BsonReader(new MemoryStream(data1)); +#pragma warning disable 612,618 reader1.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 // oid JObject o1 = (JObject)JToken.ReadFrom(reader1); - byte[] data2 = MiscellaneousUtils.HexToBytes( + byte[] data2 = HexToBytes( "87-00-00-00-05-5F-69-64-00-0C-00-00-00-02-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"); BsonReader reader2 = new BsonReader(new MemoryStream(data2)); +#pragma warning disable 612,618 reader2.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 // bytes JObject o2 = (JObject)JToken.ReadFrom(reader2); @@ -538,7 +619,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "15-00-00-00-0B-72-65-67-65-78-00-74-65-73-74-00-67-69-6D-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -567,7 +648,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "1A-00-00-00-0D-63-6F-64-65-00-0B-00-00-00-49-20-61-6D-20-63-6F-64-65-21-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -596,7 +677,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "10-00-00-00-06-75-6E-64-65-66-69-6E-65-64-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -625,7 +706,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "13-00-00-00-12-6C-6F-6E-67-00-FF-FF-FF-FF-FF-FF-FF-7F-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -654,7 +735,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "1E-00-00-00-0C-6F-69-64-00-04-00-00-00-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -701,7 +782,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "75-00-00-00-0F-63-6F-64-65-57-69-74-68-53-63-6F-70-65-00-61-00-00-00-35-00-00-00-66-6F-72-20-28-69-6E-74-20-69-20-3D-20-30-3B-20-69-20-3C-20-31-30-30-30-3B-20-69-2B-2B-29-0D-0A-7B-0D-0A-20-20-61-6C-65-72-74-28-61-72-67-31-29-3B-0D-0A-7D-00-24-00-00-00-02-61-72-67-31-00-15-00-00-00-4A-73-6F-6E-2E-4E-45-54-20-69-73-20-61-77-65-73-6F-6D-65-2E-00-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -768,7 +849,7 @@ namespace Newtonsoft.Json.Tests.Bson string bson = "4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00"; - BsonReader reader = new BsonReader(new MemoryStream(MiscellaneousUtils.HexToBytes(bson))); + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); StringBuilder largeStringBuilder = new StringBuilder(); for (int i = 0; i < 100; i++) @@ -803,7 +884,7 @@ namespace Newtonsoft.Json.Tests.Bson { string bson = "0C-00-00-00-02-00-01-00-00-00-00-00"; - BsonReader reader = new BsonReader(new MemoryStream(MiscellaneousUtils.HexToBytes(bson))); + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.StartObject, reader.TokenType); @@ -842,7 +923,7 @@ namespace Newtonsoft.Json.Tests.Bson Assert.AreEqual("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson); - BsonReader reader = new BsonReader(new MemoryStream(MiscellaneousUtils.HexToBytes(bson))); + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); Assert.IsTrue(reader.Read()); Assert.AreEqual(JsonToken.StartObject, reader.TokenType); @@ -1155,7 +1236,7 @@ namespace Newtonsoft.Json.Tests.Bson { string hexdoc = "1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00"; - byte[] data = MiscellaneousUtils.HexToBytes(hexdoc); + byte[] data = HexToBytes(hexdoc); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); @@ -1365,7 +1446,7 @@ namespace Newtonsoft.Json.Tests.Bson } #endif -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) public class BigIntegerTestClass { public BigInteger Blah { get; set; } @@ -1376,7 +1457,7 @@ namespace Newtonsoft.Json.Tests.Bson { BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990"); - byte[] data = MiscellaneousUtils.HexToBytes("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00"); + byte[] data = HexToBytes("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00"); MemoryStream ms = new MemoryStream(data); BsonReader reader = new BsonReader(ms); diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs index 67ecc15b..9b411dc2 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs @@ -81,7 +81,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(1); writer.WriteEndObject(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson); } @@ -109,7 +109,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc)); writer.WriteEnd(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-00-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson); } #endif @@ -124,10 +124,27 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(99.99d); writer.WriteEnd(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00", bson); } + [Test] + public void WriteGuid() + { + Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + writer.WriteStartArray(); + writer.WriteValue(g); + writer.WriteEnd(); + + string bson = BytesToHex(ms.ToArray()); + Console.WriteLine(bson); + Assert.AreEqual("1D-00-00-00-05-30-00-10-00-00-00-04-D7-EE-21-D8-5C-4B-C9-43-8A-C2-69-28-E5-79-B7-05-00", bson); + } + [Test] public void WriteArrayBsonFromSite() { @@ -144,7 +161,7 @@ namespace Newtonsoft.Json.Tests.Bson ms.Seek(0, SeekOrigin.Begin); string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"; - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); } @@ -167,7 +184,7 @@ namespace Newtonsoft.Json.Tests.Bson ms.Seek(0, SeekOrigin.Begin); string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"; - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); @@ -189,7 +206,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteStartObject(); writer.WritePropertyName("_id"); - writer.WriteValue(MiscellaneousUtils.HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF")); + writer.WriteValue(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF")); writer.WritePropertyName("a"); writer.WriteStartArray(); @@ -213,7 +230,7 @@ namespace Newtonsoft.Json.Tests.Bson ms.Seek(0, SeekOrigin.Begin); string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); } @@ -298,7 +315,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(largeString); writer.WriteEndObject(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00", bson); } @@ -388,7 +405,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(""); writer.WriteEndObject(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("0C-00-00-00-02-00-01-00-00-00-00-00", bson); } @@ -502,7 +519,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteObjectId(oid); writer.WriteEndObject(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson); ms.Seek(0, SeekOrigin.Begin); @@ -530,12 +547,12 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteStartObject(); writer.WritePropertyName("_id"); - writer.WriteObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001")); + writer.WriteObjectId(HexToBytes("4ABBED9D1D8B0F0218000001")); writer.WritePropertyName("test"); writer.WriteValue("1234£56"); writer.WriteEndObject(); - byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + byte[] expected = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); CollectionAssert.AreEquivalent(expected, ms.ToArray()); } @@ -553,7 +570,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteRegex(string.Empty, null); writer.WriteEndObject(); - byte[] expected = MiscellaneousUtils.HexToBytes("1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00"); + byte[] expected = HexToBytes("1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00"); CollectionAssert.AreEquivalent(expected, ms.ToArray()); } @@ -698,7 +715,7 @@ namespace Newtonsoft.Json.Tests.Bson Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray()))); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) [Test] public void WriteBigInteger() { @@ -712,7 +729,7 @@ namespace Newtonsoft.Json.Tests.Bson writer.WriteValue(i); writer.WriteEndObject(); - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00", bson); ms.Seek(0, SeekOrigin.Begin); diff --git a/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs index b0ce3553..4e784976 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/BinaryConverterTests.cs @@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Tests.Converters public byte[] NullByteArray { get; set; } } -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void DeserializeBinaryClass() { @@ -120,7 +120,7 @@ namespace Newtonsoft.Json.Tests.Converters }", json); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) public class SqlBinaryClass { public SqlBinary SqlBinary { get; set; } diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs index 9811095e..340281d6 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataSetConverterTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; using Newtonsoft.Json.Converters; #if !NETFX_CORE diff --git a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs index f223a6cb..69220af7 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/DataTableConverterTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; #if !NETFX_CORE using System.Collections.Generic; diff --git a/Src/Newtonsoft.Json.Tests/Converters/ExpandoObjectConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/ExpandoObjectConverterTests.cs index 2d328f49..41c3f26e 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/ExpandoObjectConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/ExpandoObjectConverterTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; diff --git a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs index e3c5828b..48850f78 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs @@ -61,6 +61,13 @@ namespace Newtonsoft.Json.Tests.Converters Assert.AreEqual(DateTimeStyles.None, converter.DateTimeStyles); } + public static string GetUtcOffsetText(DateTime d) + { + TimeSpan utcOffset = d.GetUtcOffset(); + + return utcOffset.Hours.ToString("+00;-00", CultureInfo.InvariantCulture) + ":" + utcOffset.Minutes.ToString("00;00", CultureInfo.InvariantCulture); + } + [Test] public void SerializeDateTime() { @@ -76,7 +83,7 @@ namespace Newtonsoft.Json.Tests.Converters d = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Local); result = JsonConvert.SerializeObject(d, converter); - Assert.AreEqual(@"""2000-12-15T22:11:03.055" + d.GetUtcOffsetText() + @"""", result); + Assert.AreEqual(@"""2000-12-15T22:11:03.055" + GetUtcOffsetText(d) + @"""", result); } [Test] diff --git a/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs index 1a9fd55b..ded76aca 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/ObjectIdConverterTests.cs @@ -52,7 +52,7 @@ namespace Newtonsoft.Json.Tests.Converters { ObjectIdTestClass c = new ObjectIdTestClass { - Id = new BsonObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001")), + Id = new BsonObjectId(HexToBytes("4ABBED9D1D8B0F0218000001")), Test = "1234£56" }; @@ -63,7 +63,7 @@ namespace Newtonsoft.Json.Tests.Converters BsonWriter writer = new BsonWriter(ms); serializer.Serialize(writer, c); - byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + byte[] expected = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); CollectionAssert.AreEquivalent(expected, ms.ToArray()); } @@ -71,14 +71,14 @@ namespace Newtonsoft.Json.Tests.Converters [Test] public void Deserialize() { - byte[] bson = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + byte[] bson = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); JsonSerializer serializer = new JsonSerializer(); BsonReader reader = new BsonReader(new MemoryStream(bson)); ObjectIdTestClass c = serializer.Deserialize(reader); - CollectionAssert.AreEquivalent(c.Id.Value, MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001")); + CollectionAssert.AreEquivalent(c.Id.Value, HexToBytes("4ABBED9D1D8B0F0218000001")); Assert.AreEqual(c.Test, "1234£56"); } } diff --git a/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs index 7d9ec30e..25ffcc54 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/RegexConverterTests.cs @@ -81,7 +81,7 @@ namespace Newtonsoft.Json.Tests.Converters serializer.Serialize(writer, new RegexTestClass { Regex = regex }); string expected = "13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00"; - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); } @@ -102,7 +102,7 @@ namespace Newtonsoft.Json.Tests.Converters [Test] public void DeserializeFromBson() { - MemoryStream ms = new MemoryStream(MiscellaneousUtils.HexToBytes("13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00")); + MemoryStream ms = new MemoryStream(HexToBytes("13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00")); BsonReader reader = new BsonReader(ms); JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new RegexConverter()); @@ -150,7 +150,7 @@ namespace Newtonsoft.Json.Tests.Converters serializer.Serialize(writer, new RegexTestClass { Regex = regex }); string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00"; - string bson = MiscellaneousUtils.BytesToHex(ms.ToArray()); + string bson = BytesToHex(ms.ToArray()); Assert.AreEqual(expected, bson); diff --git a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs index 3f386471..c6278a2f 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; using System.Collections.Generic; using Newtonsoft.Json.Tests.Serialization; diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index 03ffaca1..af4cf626 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -143,7 +143,7 @@ now brown cow?", '"', true); result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true); Assert.AreEqual(result, @"'Fred\'s ""cat"".'"); - result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress"); + result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress", '"', true); Assert.AreEqual(result, @"""\u001farray( + ExceptionAssert.Throws( @"Index 1 outside the bounds of JConstructor.", () => { @@ -279,7 +279,7 @@ namespace Newtonsoft.Json.Tests.Linq { JArray a = new JArray(1, 2, 3, 4, 5); - ExceptionAssert.Throws( + ExceptionAssert.Throws( "Index 1000 outside the bounds of JArray.", () => { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs index 724f97ab..1d5365e7 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs @@ -56,7 +56,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(p, p.Value.Parent); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void ListChanged() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index 762f23c5..388650db 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -319,6 +319,8 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(null, (Uri)(new JValue((object)null))); Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")), (string)(new JValue(Encoding.UTF8.GetBytes("hi")))); CollectionAssert.AreEquivalent((byte[])Encoding.UTF8.GetBytes("hi"), (byte[])(new JValue(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi"))))); + Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC").ToByteArray()))); + Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid?)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC").ToByteArray()))); Assert.AreEqual(null, (Uri)(JValue)null); Assert.AreEqual(null, (int?)(JValue)null); @@ -348,18 +350,22 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(5, (int)(new JValue(StringComparison.OrdinalIgnoreCase))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) string bigIntegerText = "1234567899999999999999999999999999999999999999999999999999999999999990"; - Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(bigIntegerText))); - Assert.AreEqual(new BigInteger(long.MaxValue), (BigInteger)(new JValue(long.MaxValue))); - Assert.AreEqual(new BigInteger(4.5d), (BigInteger)(new JValue((4.5d)))); - Assert.AreEqual(new BigInteger(4.5f), (BigInteger)(new JValue((4.5f)))); - Assert.AreEqual(new BigInteger(byte.MaxValue), (BigInteger)(new JValue(byte.MaxValue))); - Assert.AreEqual(new BigInteger(123), (BigInteger)(new JValue(123))); + Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(BigInteger.Parse(bigIntegerText))).Value); + + Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(bigIntegerText)).ToObject()); + Assert.AreEqual(new BigInteger(long.MaxValue), (new JValue(long.MaxValue)).ToObject()); + Assert.AreEqual(new BigInteger(4.5d), (new JValue((4.5d))).ToObject()); + Assert.AreEqual(new BigInteger(4.5f), (new JValue((4.5f))).ToObject()); + Assert.AreEqual(new BigInteger(byte.MaxValue), (new JValue(byte.MaxValue)).ToObject()); + Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject()); + Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject()); + Assert.AreEqual(null, (new JValue((object)null)).ToObject()); byte[] intData = BigInteger.Parse(bigIntegerText).ToByteArray(); - Assert.AreEqual(BigInteger.Parse(bigIntegerText), (BigInteger)(new JValue(intData))); + Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(intData)).ToObject()); Assert.AreEqual(4.0d, (double)(new JValue(new BigInteger(4.5d)))); Assert.AreEqual(true, (bool)(new JValue(new BigInteger(1)))); @@ -424,10 +430,11 @@ namespace Newtonsoft.Json.Tests.Linq #endif ExceptionAssert.Throws("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); }); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - ExceptionAssert.Throws("Can not convert Uri to BigInteger.", () => { var i = (BigInteger)new JValue(new Uri("http://www.google.com")); }); - ExceptionAssert.Throws("Can not convert Null to BigInteger.", () => { var i = (BigInteger)new JValue((object)null); }); - ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (BigInteger)new JValue(Guid.NewGuid()); }); +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + ExceptionAssert.Throws("Can not convert Uri to BigInteger.", () => { var i = (new JValue(new Uri("http://www.google.com"))).ToObject(); }); + ExceptionAssert.Throws("Can not convert Null to BigInteger.", () => { var i = (new JValue((object)null)).ToObject(); }); + ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject(); }); + ExceptionAssert.Throws("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject(); }); #endif } @@ -488,9 +495,10 @@ namespace Newtonsoft.Json.Tests.Linq Assert.IsTrue(JToken.DeepEquals(new JValue((DateTimeOffset?)null), (JValue)(DateTimeOffset?)null)); #endif -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - Assert.IsTrue(JToken.DeepEquals(new JValue(new BigInteger(1)), (JValue)new BigInteger(1))); - Assert.IsTrue(JToken.DeepEquals(new JValue((BigInteger?)null), (JValue)(BigInteger?)null)); +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + // had to remove implicit casting to avoid user reference to System.Numerics.dll + Assert.IsTrue(JToken.DeepEquals(new JValue(new BigInteger(1)), new JValue(new BigInteger(1)))); + Assert.IsTrue(JToken.DeepEquals(new JValue((BigInteger?)null), new JValue((BigInteger?)null))); #endif Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true)); Assert.IsTrue(JToken.DeepEquals(new JValue(true), (JValue)true)); @@ -926,7 +934,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.IsTrue(a.DeepEquals(a2)); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void Clone() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs index fd67f2f1..f5494e8b 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenWriterTest.cs @@ -25,6 +25,9 @@ using System; using System.Collections.Generic; +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) +using System.Numerics; +#endif using System.Text; #if !NETFX_CORE using NUnit.Framework; @@ -116,6 +119,11 @@ namespace Newtonsoft.Json.Tests.Linq jsonWriter.WriteValue("DVD read/writer"); Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + jsonWriter.WriteValue(new BigInteger(123)); + Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); +#endif + jsonWriter.WriteValue(new byte[0]); Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); @@ -140,6 +148,27 @@ namespace Newtonsoft.Json.Tests.Linq /*fail*/]", writer.Token.ToString()); } +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + [Test] + public void WriteBigInteger() + { + JTokenWriter writer = new JTokenWriter(); + + writer.WriteStartArray(); + writer.WriteValue(new BigInteger(123)); + writer.WriteEndArray(); + + JValue i = (JValue) writer.Token[0]; + + Assert.AreEqual(new BigInteger(123), i.Value); + Assert.AreEqual(JTokenType.Integer, i.Type); + + Assert.AreEqual(@"[ + 123 +]", writer.Token.ToString()); + } +#endif + [Test] public void WriteRaw() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs index 4d2f36cf..96c0ae9b 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs @@ -86,7 +86,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual("Pie", v.Value); Assert.AreEqual(JTokenType.String, v.Type); -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) v.Value = DBNull.Value; Assert.AreEqual(DBNull.Value, v.Value); Assert.AreEqual(JTokenType.Null, v.Type); @@ -115,7 +115,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(g, v.Value); Assert.AreEqual(JTokenType.Guid, v.Type); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) BigInteger i = BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990"); v.Value = i; Assert.AreEqual(i, v.Value); @@ -175,13 +175,13 @@ namespace Newtonsoft.Json.Tests.Linq v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5")); Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture)); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) v = new JValue(BigInteger.Parse("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990")); Assert.AreEqual("123456789999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990", v.ToString(null, CultureInfo.InvariantCulture)); #endif } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) [Test] public void JValueParse() { @@ -456,7 +456,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(Int32.MaxValue, Convert.ToInt32(new JValue(Int32.MaxValue))); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) [Test] public void ConvertsToInt32_BigInteger() { @@ -593,7 +593,7 @@ namespace Newtonsoft.Json.Tests.Linq v = new JValue(new Uri("http://www.google.com")); Assert.AreEqual(TypeCode.Object, v.GetTypeCode()); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) v = new JValue(new BigInteger(3)); Assert.AreEqual(TypeCode.Object, v.GetTypeCode()); #endif @@ -622,7 +622,7 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual("2013", v.ToString("yyyy")); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) [Test] public void ToStringNewTypes() { diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index dc701a11..1a6362d5 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -718,7 +718,7 @@ keyword such as type of business."" Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), d); } -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) [Test] public void CovariantIJEnumerable() { @@ -733,6 +733,19 @@ keyword such as type of business."" } #endif +#if !NET20 + [Test] + public void LinqCast() + { + JToken olist = JArray.Parse("[12,55]"); + + List list1 = olist.AsEnumerable().Values().ToList(); + + Assert.AreEqual(12, list1[0]); + Assert.AreEqual(55, list1[1]); + } +#endif + [Test] public void ChildrenExtension() { diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj new file mode 100644 index 00000000..fc0545ad --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj @@ -0,0 +1,332 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {3E6E2335-B079-4B5B-A65A-9D586914BCBB} + Library + Properties + Newtonsoft.Json.Tests + Newtonsoft.Json.Tests + + + + + + + + + + + 3.5 + + + v4.5 + false + + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + + + true + full + false + bin\Debug\Portable40\ + TRACE;DEBUG;PORTABLE40 + prompt + 4 + AllRules.ruleset + false + + + pdbonly + true + bin\Release\Portable40\ + TRACE;PORTABLE40 + prompt + 4 + AllRules.ruleset + false + + + + + False + ..\Lib\NUnit\DotNet\nunit.framework.dll + + + + 3.5 + + + 3.5 + + + + 3.5 + + + 3.5 + + + + + 3.0 + + + + 3.0 + + + 3.5 + + + + 3.5 + + + + + 3.5 + + + + + + + + + + + + + + + + True + True + FileSystemEntityModel.edmx + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + EntityModelCodeGenerator + FileSystemEntityModel.Designer.cs + + + + + Always + + + Always + + + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + + + + {959f7f85-c98b-4876-971a-9036224578e4} + Newtonsoft.Json.Portable40 + + + + + \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index d07ad717..6fd50297 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.1.16001")] +[assembly: AssemblyFileVersion("5.0.1.16008")] diff --git a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs index 7f2b4aba..6ecc7d58 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/ExtensionsTests.cs @@ -202,7 +202,7 @@ namespace Newtonsoft.Json.Tests.Schema #if !NET20 GenerateSchemaAndSerializeFromType(new NullableDateTimeTestClass()); #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) GenerateSchemaAndSerializeFromType(new DataSet()); #endif GenerateSchemaAndSerializeFromType(new object()); @@ -210,7 +210,7 @@ namespace Newtonsoft.Json.Tests.Schema GenerateSchemaAndSerializeFromType("Hi"); GenerateSchemaAndSerializeFromType(new DateTime(2000, 12, 29, 23, 59, 0, DateTimeKind.Utc)); GenerateSchemaAndSerializeFromType(TimeSpan.FromTicks(1000000)); -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) GenerateSchemaAndSerializeFromType(DBNull.Value); #endif GenerateSchemaAndSerializeFromType(new JsonPropertyWithHandlingValues()); diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs index 5932cfc3..f8d21202 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs @@ -90,7 +90,7 @@ namespace Newtonsoft.Json.Tests.Schema Assert.IsTrue(o.IsValid(schema)); } -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void Generate_DefaultValueAttributeTestClass() { @@ -310,7 +310,7 @@ namespace Newtonsoft.Json.Tests.Schema Assert.IsTrue(v.IsValid(schema)); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void GenerateSchemaForISerializable() { @@ -325,7 +325,7 @@ namespace Newtonsoft.Json.Tests.Schema } #endif -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void GenerateSchemaForDBNull() { @@ -357,7 +357,7 @@ namespace Newtonsoft.Json.Tests.Schema IList properties = base.CreateProperties(type, memberSerialization); JsonPropertyCollection c = new JsonPropertyCollection(type); - CollectionUtils.AddRange(c, (IEnumerable)properties.Where(m => m.PropertyName != "Root")); + c.AddRange(properties.Where(m => m.PropertyName != "Root")); return c; } @@ -473,9 +473,9 @@ namespace Newtonsoft.Json.Tests.Schema JsonSchemaGenerator generator = new JsonSchemaGenerator(); generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName; generator.ContractResolver = new CamelCasePropertyNamesContractResolver() - { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) - IgnoreSerializableAttribute = true + { +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) + IgnoreSerializableAttribute = true #endif }; @@ -519,7 +519,7 @@ namespace Newtonsoft.Json.Tests.Schema }", json); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void GenerateSchemaSerializable() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs index 0fffcc10..aaec1787 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/CamelCasePropertyNamesContractResolverTests.cs @@ -102,7 +102,7 @@ namespace Newtonsoft.Json.Tests.Serialization string json = o.ToString(); } -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void MemberSearchFlags() { @@ -177,7 +177,7 @@ namespace Newtonsoft.Json.Tests.Serialization }", json); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) [Test] public void DynamicCamelCasePropertyNames() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs index eb98a2b7..0ebdeb8a 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Runtime.Serialization; #if !NETFX_CORE using NUnit.Framework; @@ -67,6 +68,14 @@ namespace Newtonsoft.Json.Tests.Serialization } } + public class EscapedPropertiesContractResolver : DefaultContractResolver + { + protected internal override string ResolvePropertyName(string propertyName) + { + return base.ResolvePropertyName(propertyName + @"-'-""-"); + } + } + public class Book { public string BookName { get; set; } @@ -87,18 +96,39 @@ namespace Newtonsoft.Json.Tests.Serialization } } -#if !NET20 public class AddressWithDataMember { +#if !NET20 [DataMember(Name = "CustomerAddress1")] - public string AddressLine1 { get; set; } - - } #endif + public string AddressLine1 { get; set; } + } [TestFixture] public class ContractResolverTests : TestFixtureBase { + [Test] + public void SerializeWithEscapedPropertyName() + { + string json = JsonConvert.SerializeObject( + new AddressWithDataMember + { + AddressLine1 = "value!" + }, + new JsonSerializerSettings + { + ContractResolver = new EscapedPropertiesContractResolver() + }); + + Assert.AreEqual(@"{""AddressLine1-'-\""-"":""value!""}", json); + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.Read(); + reader.Read(); + + Assert.AreEqual(@"AddressLine1-'-""-", reader.Value); + } + #if !NET20 [Test] public void DeserializeDataMemberClassWithNoDataContract() @@ -182,7 +212,7 @@ namespace Newtonsoft.Json.Tests.Serialization }", startingWithB); } -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void SerializeCompilerGeneratedMembers() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs index 4bfb1403..c90b5a01 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DynamicTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections; using System.Collections.Generic; @@ -100,7 +100,7 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(dynamicObject.ChildObject.Text, d.ChildObject.Text); } -#if !PORTABLE +#if !(PORTABLE || PORTABLE40) [Test] public void SerializeDynamicObjectWithObjectTracking() { @@ -121,8 +121,8 @@ namespace Newtonsoft.Json.Tests.Serialization Console.WriteLine(json); - string dynamicChildObjectTypeName = ReflectionUtils.GetTypeName(typeof(DynamicChildObject), FormatterAssemblyStyle.Full); - string expandoObjectTypeName = ReflectionUtils.GetTypeName(typeof(ExpandoObject), FormatterAssemblyStyle.Full); + string dynamicChildObjectTypeName = ReflectionUtils.GetTypeName(typeof(DynamicChildObject), FormatterAssemblyStyle.Full, null); + string expandoObjectTypeName = ReflectionUtils.GetTypeName(typeof(ExpandoObject), FormatterAssemblyStyle.Full, null); Assert.AreEqual(@"{ ""$type"": """ + expandoObjectTypeName + @""", diff --git a/Src/Newtonsoft.Json.Tests/Serialization/EntitiesSerializationTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/EntitiesSerializationTests.cs index ff958d49..7e946d99 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/EntitiesSerializationTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/EntitiesSerializationTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20 || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(NET35 || NET20 || SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; using System.Collections.Generic; using System.Data; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 1e18fd9b..517a8aae 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -158,6 +158,249 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(@"{""selectList"":[{""Value"":""Hello""}]}", json); } +#if !NET20 + [DataContract] + public class BaseDataContractWithHidden + { + [DataMember(Name = "virtualMember")] + public virtual string VirtualMember { get; set; } + + [DataMember(Name = "nonVirtualMember")] + public string NonVirtualMember { get; set; } + + public virtual object NewMember { get; set; } + } + + public class ChildDataContractWithHidden : BaseDataContractWithHidden + { + [DataMember(Name = "NewMember")] + public virtual new string NewMember { get; set; } + public override string VirtualMember { get; set; } + public string AddedMember { get; set; } + } + + [Test] + public void ChildDataContractTestWithHidden() + { + var cc = new ChildDataContractWithHidden + { + VirtualMember = "VirtualMember!", + NonVirtualMember = "NonVirtualMember!", + NewMember = "NewMember!" + }; + + string result = JsonConvert.SerializeObject(cc); + Assert.AreEqual(@"{""NewMember"":""NewMember!"",""virtualMember"":""VirtualMember!"",""nonVirtualMember"":""NonVirtualMember!""}", result); + } + + // ignore hiding members compiler warning +#pragma warning disable 108,114 + [DataContract] + public class BaseWithContract + { + [DataMember(Name = "VirtualWithDataMemberBase")] + public virtual string VirtualWithDataMember { get; set; } + [DataMember] + public virtual string Virtual { get; set; } + [DataMember(Name = "WithDataMemberBase")] + public string WithDataMember { get; set; } + [DataMember] + public string JustAProperty { get; set; } + } + + [DataContract] + public class BaseWithoutContract + { + [DataMember(Name = "VirtualWithDataMemberBase")] + public virtual string VirtualWithDataMember { get; set; } + [DataMember] + public virtual string Virtual { get; set; } + [DataMember(Name = "WithDataMemberBase")] + public string WithDataMember { get; set; } + [DataMember] + public string JustAProperty { get; set; } + } + + [DataContract] + public class SubWithoutContractNewProperties : BaseWithContract + { + [DataMember(Name = "VirtualWithDataMemberSub")] + public string VirtualWithDataMember { get; set; } + public string Virtual { get; set; } + [DataMember(Name = "WithDataMemberSub")] + public string WithDataMember { get; set; } + public string JustAProperty { get; set; } + } + + [DataContract] + public class SubWithoutContractVirtualProperties : BaseWithContract + { + public override string VirtualWithDataMember { get; set; } + [DataMember(Name = "VirtualSub")] + public override string Virtual { get; set; } + } + + [DataContract] + public class SubWithContractNewProperties : BaseWithContract + { + [DataMember(Name = "VirtualWithDataMemberSub")] + public string VirtualWithDataMember { get; set; } + [DataMember(Name = "Virtual2")] + public string Virtual { get; set; } + [DataMember(Name = "WithDataMemberSub")] + public string WithDataMember { get; set; } + [DataMember(Name = "JustAProperty2")] + public string JustAProperty { get; set; } + } + + [DataContract] + public class SubWithContractVirtualProperties : BaseWithContract + { + [DataMember(Name = "VirtualWithDataMemberSub")] + public virtual string VirtualWithDataMember { get; set; } + } +#pragma warning restore 108,114 + + [Test] + public void SubWithoutContractNewPropertiesTest() + { + BaseWithContract baseWith = new SubWithoutContractNewProperties + { + JustAProperty = "JustAProperty!", + Virtual = "Virtual!", + VirtualWithDataMember = "VirtualWithDataMember!", + WithDataMember = "WithDataMember!" + }; + + baseWith.JustAProperty = "JustAProperty2!"; + baseWith.Virtual = "Virtual2!"; + baseWith.VirtualWithDataMember = "VirtualWithDataMember2!"; + baseWith.WithDataMember = "WithDataMember2!"; + + string json = AssertSerializeDeserializeEqual(baseWith); + + Assert.AreEqual(@"{ + ""JustAProperty"": ""JustAProperty2!"", + ""Virtual"": ""Virtual2!"", + ""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"", + ""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"", + ""WithDataMemberBase"": ""WithDataMember2!"", + ""WithDataMemberSub"": ""WithDataMember!"" +}", json); + } + + [Test] + public void SubWithoutContractVirtualPropertiesTest() + { + BaseWithContract baseWith = new SubWithoutContractVirtualProperties + { + JustAProperty = "JustAProperty!", + Virtual = "Virtual!", + VirtualWithDataMember = "VirtualWithDataMember!", + WithDataMember = "WithDataMember!" + }; + + baseWith.JustAProperty = "JustAProperty2!"; + baseWith.Virtual = "Virtual2!"; + baseWith.VirtualWithDataMember = "VirtualWithDataMember2!"; + baseWith.WithDataMember = "WithDataMember2!"; + + string json = JsonConvert.SerializeObject(baseWith, Formatting.Indented); + + Console.WriteLine(json); + + Assert.AreEqual(@"{ + ""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"", + ""VirtualSub"": ""Virtual2!"", + ""WithDataMemberBase"": ""WithDataMember2!"", + ""JustAProperty"": ""JustAProperty2!"" +}", json); + } + + [Test] + public void SubWithContractNewPropertiesTest() + { + BaseWithContract baseWith = new SubWithContractNewProperties + { + JustAProperty = "JustAProperty!", + Virtual = "Virtual!", + VirtualWithDataMember = "VirtualWithDataMember!", + WithDataMember = "WithDataMember!" + }; + + baseWith.JustAProperty = "JustAProperty2!"; + baseWith.Virtual = "Virtual2!"; + baseWith.VirtualWithDataMember = "VirtualWithDataMember2!"; + baseWith.WithDataMember = "WithDataMember2!"; + + string json = AssertSerializeDeserializeEqual(baseWith); + + Assert.AreEqual(@"{ + ""JustAProperty"": ""JustAProperty2!"", + ""JustAProperty2"": ""JustAProperty!"", + ""Virtual"": ""Virtual2!"", + ""Virtual2"": ""Virtual!"", + ""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"", + ""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"", + ""WithDataMemberBase"": ""WithDataMember2!"", + ""WithDataMemberSub"": ""WithDataMember!"" +}", json); + } + + [Test] + public void SubWithContractVirtualPropertiesTest() + { + BaseWithContract baseWith = new SubWithContractVirtualProperties + { + JustAProperty = "JustAProperty!", + Virtual = "Virtual!", + VirtualWithDataMember = "VirtualWithDataMember!", + WithDataMember = "WithDataMember!" + }; + + baseWith.JustAProperty = "JustAProperty2!"; + baseWith.Virtual = "Virtual2!"; + baseWith.VirtualWithDataMember = "VirtualWithDataMember2!"; + baseWith.WithDataMember = "WithDataMember2!"; + + string json = AssertSerializeDeserializeEqual(baseWith); + + Assert.AreEqual(@"{ + ""JustAProperty"": ""JustAProperty2!"", + ""Virtual"": ""Virtual2!"", + ""VirtualWithDataMemberBase"": ""VirtualWithDataMember2!"", + ""VirtualWithDataMemberSub"": ""VirtualWithDataMember!"", + ""WithDataMemberBase"": ""WithDataMember2!"" +}", json); + } + + private string AssertSerializeDeserializeEqual(object o) + { + MemoryStream ms = new MemoryStream(); + DataContractJsonSerializer s = new DataContractJsonSerializer(o.GetType()); + s.WriteObject(ms, o); + + var data = ms.ToArray(); + JObject dataContractJson = JObject.Parse(Encoding.UTF8.GetString(data, 0, data.Length)); + dataContractJson = new JObject(dataContractJson.Properties().OrderBy(p => p.Name)); + + JObject jsonNetJson = JObject.Parse(JsonConvert.SerializeObject(o)); + jsonNetJson = new JObject(jsonNetJson.Properties().OrderBy(p => p.Name)); + + Console.WriteLine("Results for " + o.GetType().Name); + Console.WriteLine("DataContractJsonSerializer: " + dataContractJson); + Console.WriteLine("JsonDotNetSerializer : " + jsonNetJson); + + Assert.AreEqual(dataContractJson.Count, jsonNetJson.Count); + foreach (KeyValuePair property in dataContractJson) + { + Assert.IsTrue(JToken.DeepEquals(jsonNetJson[property.Key], property.Value), "Property not equal: " + property.Key); + } + + return jsonNetJson.ToString(); + } +#endif + [Test] public void PersonTypedObjectDeserialization() { @@ -826,7 +1069,7 @@ keyword such as type of business."" ConverableMembers c = JsonConvert.DeserializeObject(json); Assert.AreEqual("string", c.String); Assert.AreEqual(double.MaxValue, c.Double); -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) Assert.AreEqual(DBNull.Value, c.DBNull); #endif } @@ -1645,7 +1888,7 @@ keyword such as type of business."" Assert.AreEqual("titleId", n.FidOrder[n.FidOrder.Count - 1]); } -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE || PORTABLE40) [MetadataType(typeof(OptInClassMetadata))] public class OptInClass { @@ -2196,7 +2439,7 @@ Path '', line 1, position 1.", }); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) [Test] public void CannotDeserializeArrayIntoDynamic() { @@ -2313,7 +2556,7 @@ Path '', line 1, position 2.", { ContractResolver = new DefaultContractResolver { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) IgnoreSerializableAttribute = true #endif } @@ -2332,7 +2575,7 @@ Path '', line 1, position 2.", { ContractResolver = new DefaultContractResolver { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) IgnoreSerializableAttribute = true #endif } @@ -2350,9 +2593,9 @@ Path '', line 1, position 2.", JsonConvert.DeserializeObject("{ReadTimeout:''}", new JsonSerializerSettings { ContractResolver = new DefaultContractResolver - { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) - IgnoreSerializableAttribute = true + { +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) + IgnoreSerializableAttribute = true #endif } }); @@ -2370,7 +2613,7 @@ Path '', line 1, position 2.", { ContractResolver = new DefaultContractResolver { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) IgnoreSerializableAttribute = true #endif } @@ -3041,7 +3284,7 @@ Path '', line 1, position 2.", Assert.AreEqual("value", newModelStateDictionary["key"]); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) public class ISerializableTestObject : ISerializable { internal string _stringValue; @@ -3538,7 +3781,7 @@ To fix this error either change the environment to be fully trusted, change the } #endif -#if !(NET20 || SILVERLIGHT) +#if !(NET20 || SILVERLIGHT || PORTABLE40) [Test] public void SerializeDeserializeXNodeProperties() { @@ -3569,7 +3812,7 @@ To fix this error either change the environment to be fully trusted, change the } #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void SerializeDeserializeXmlNodeProperties() { @@ -4425,7 +4668,7 @@ To fix this error either change the environment to be fully trusted, change the }", json); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) [Test] public void SerializeExpandoObject() { @@ -5112,6 +5355,7 @@ To fix this error either change the environment to be fully trusted, change the }; string result = JsonConvert.SerializeObject(cc); + Console.WriteLine(result); Assert.AreEqual(@"{""differentVirtualMember"":""VirtualMember!"",""nonVirtualMember"":""NonVirtualMember!""}", result); } @@ -5430,7 +5674,7 @@ To fix this error either change the environment to be fully trusted, change the } } -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void SerializeException1() { @@ -5560,7 +5804,7 @@ To fix this error either change the environment to be fully trusted, change the } } -#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE) +#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE || PORTABLE40) [Test] public void DeserializeConcurrentDictionary() { @@ -6508,7 +6752,7 @@ Parameter name: value", Assert.AreEqual("", s); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void SerializeAndDeserializeWithAttributes() { @@ -6674,7 +6918,7 @@ Parameter name: value", }); } - #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void DeserializeException() { @@ -6712,7 +6956,7 @@ Parameter name: value", } #endif - [Test] + [Test] public void AdditionalContentAfterFinish() { ExceptionAssert.Throws( @@ -6930,7 +7174,7 @@ Parameter name: value", Assert.AreEqual("[1.1,0.0,0.0]", json); } -#if !(NET20 || NET35 || NET40 || SILVERLIGHT) +#if !(NET20 || NET35 || NET40 || SILVERLIGHT || PORTABLE40) #if !PORTABLE [Test] public void DeserializeReadOnlyListWithBigInteger() @@ -6992,7 +7236,7 @@ Parameter name: value", obj = JsonConvert.DeserializeObject>(json); }; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) doStuff(); Assert.AreEqual(500, obj.Item1); #else @@ -7025,7 +7269,7 @@ Parameter name: value", } #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || NET35 || NET20) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || NET35 || NET20 || PORTABLE40) [Test] public void SerializeTupleWithSerializableAttribute() { @@ -7052,6 +7296,134 @@ Parameter name: value", } #endif +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) + public class PopulateReadOnlyTestClass + { + public IList NonReadOnlyList { get; set; } + public IDictionary NonReadOnlyDictionary { get; set; } + + public IList Array { get; set; } + + public IList List { get; set; } + public IDictionary Dictionary { get; set; } + + public IReadOnlyCollection IReadOnlyCollection { get; set; } + public ReadOnlyCollection ReadOnlyCollection { get; set; } + public IReadOnlyList IReadOnlyList { get; set; } + + public IReadOnlyDictionary IReadOnlyDictionary { get; set; } + public ReadOnlyDictionary ReadOnlyDictionary { get; set; } + + public PopulateReadOnlyTestClass() + { + NonReadOnlyList = new List { 1 }; + NonReadOnlyDictionary = new Dictionary { { "first", 2 } }; + + Array = new[] {3}; + + List = new ReadOnlyCollection(new[] { 4 }); + Dictionary = new ReadOnlyDictionary(new Dictionary { { "first", 5 } }); + + IReadOnlyCollection = new ReadOnlyCollection(new[] { 6 }); + ReadOnlyCollection = new ReadOnlyCollection(new[] { 7 }); + IReadOnlyList = new ReadOnlyCollection(new[] { 8 }); + + IReadOnlyDictionary = new ReadOnlyDictionary(new Dictionary { { "first", 9 } }); + ReadOnlyDictionary = new ReadOnlyDictionary(new Dictionary { { "first", 10 } }); + } + } + + [Test] + public void SerializeReadOnlyCollections() + { + PopulateReadOnlyTestClass c1 = new PopulateReadOnlyTestClass(); + + string json = JsonConvert.SerializeObject(c1, Formatting.Indented); + + Assert.AreEqual(@"{ + ""NonReadOnlyList"": [ + 1 + ], + ""NonReadOnlyDictionary"": { + ""first"": 2 + }, + ""Array"": [ + 3 + ], + ""List"": [ + 4 + ], + ""Dictionary"": { + ""first"": 5 + }, + ""IReadOnlyCollection"": [ + 6 + ], + ""ReadOnlyCollection"": [ + 7 + ], + ""IReadOnlyList"": [ + 8 + ], + ""IReadOnlyDictionary"": { + ""first"": 9 + }, + ""ReadOnlyDictionary"": { + ""first"": 10 + } +}", json); + } + + [Test] + public void PopulateReadOnlyCollections() + { + string json = @"{ + ""NonReadOnlyList"": [ + 11 + ], + ""NonReadOnlyDictionary"": { + ""first"": 12 + }, + ""Array"": [ + 13 + ], + ""List"": [ + 14 + ], + ""Dictionary"": { + ""first"": 15 + }, + ""IReadOnlyCollection"": [ + 16 + ], + ""ReadOnlyCollection"": [ + 17 + ], + ""IReadOnlyList"": [ + 18 + ], + ""IReadOnlyDictionary"": { + ""first"": 19 + }, + ""ReadOnlyDictionary"": { + ""first"": 20 + } +}"; + + var c2 = JsonConvert.DeserializeObject(json); + + Assert.AreEqual(1, c2.NonReadOnlyDictionary.Count); + Assert.AreEqual(12, c2.NonReadOnlyDictionary["first"]); + + Assert.AreEqual(2, c2.NonReadOnlyList.Count); + Assert.AreEqual(1, c2.NonReadOnlyList[0]); + Assert.AreEqual(11, c2.NonReadOnlyList[1]); + + Assert.AreEqual(1, c2.Array.Count); + Assert.AreEqual(13, c2.Array[0]); + } +#endif + [Test] public void SerializeArray2D() { @@ -7713,7 +8085,7 @@ Parameter name: value", }", json); } -#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT) +#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT || PORTABLE40) [Test] public void ReadTooLargeInteger() { @@ -7807,7 +8179,7 @@ Parameter name: value", } #endif -#if !(PORTABLE || NET20 || NET35) +#if !(NET20 || NET35 || PORTABLE40) [Test] public void HashSetInterface() { @@ -7880,7 +8252,7 @@ Parameter name: value", Assert.AreEqual(1234567890.123456m, d); } -#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || WINDOWS_PHONE) +#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || WINDOWS_PHONE || PORTABLE40) [Test] public void DontSerializeStaticFields() { @@ -7914,7 +8286,7 @@ Parameter name: value", Assert.AreEqual("string!", l[0]); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) [Test] public void SerializeBigInteger() { @@ -7928,7 +8300,7 @@ Parameter name: value", } #endif -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) [Test] public void DeserializeReadOnlyListInterface() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ReferenceLoopHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ReferenceLoopHandlingTests.cs index 4b68ca00..0a4f3557 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ReferenceLoopHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ReferenceLoopHandlingTests.cs @@ -149,7 +149,7 @@ namespace Newtonsoft.Json.Tests.Serialization }", json); } -#if !(PORTABLE || SILVERLIGHT || NETFX_CORE) +#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || PORTABLE40) public class MainClass : ISerializable { public ChildClass Child { get; set; } @@ -210,7 +210,7 @@ namespace Newtonsoft.Json.Tests.Serialization } #endif -#if !(NET20 || NET35) +#if !(NET20 || NET35 || PORTABLE40) public class DictionaryDynamicObject : DynamicObject { public IDictionary Values { get; private set; } diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index 092816ca..9f4c8273 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -612,7 +612,7 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(@"Unexpected end when deserializing object. Path 'events2.code', line 1, position 49.", errors[1]); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) [Test] public void ErrorHandlingEndOfContentDynamic() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TraceWriterTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TraceWriterTests.cs index 5523938a..93a33dd9 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TraceWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TraceWriterTests.cs @@ -34,7 +34,7 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class TraceWriterTests : TestFixtureBase { -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE || NETFX_CORE || PORTABLE40) [Test] public void DiagnosticsTraceWriterTest() { @@ -458,7 +458,7 @@ Newtonsoft.Json Error: 0 : Error! Assert.AreEqual("Finished deserializing System.Collections.Generic.List`1[System.Object]. Path '$values', line 17, position 4.", traceWriter.TraceRecords[9].Message); } -#if !(NETFX_CORE || PORTABLE || SILVERLIGHT) +#if !(NETFX_CORE || PORTABLE || SILVERLIGHT || PORTABLE40) [Test] public void DeserializeISerializable() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index e6486457..d7c6a081 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !PORTABLE +#if !(PORTABLE || PORTABLE40) #if !(NET35 || NET20 || PORTABLE) using System.Dynamic; #endif @@ -165,7 +165,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void WriteTypeNameForObjects() { - string employeeRef = ReflectionUtils.GetTypeName(typeof (EmployeeReference), FormatterAssemblyStyle.Simple); + string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple, null); EmployeeReference employee = new EmployeeReference(); @@ -185,7 +185,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void DeserializeTypeName() { - string employeeRef = ReflectionUtils.GetTypeName(typeof (EmployeeReference), FormatterAssemblyStyle.Simple); + string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple, null); string json = @"{ ""$id"": ""1"", @@ -207,7 +207,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void DeserializeTypeNameFromGacAssembly() { - string cookieRef = ReflectionUtils.GetTypeName(typeof (Cookie), FormatterAssemblyStyle.Simple); + string cookieRef = ReflectionUtils.GetTypeName(typeof (Cookie), FormatterAssemblyStyle.Simple, null); string json = @"{ ""$id"": ""1"", @@ -493,7 +493,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void WriteObjectTypeNameForProperty() { - string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof (TypeNameProperty), FormatterAssemblyStyle.Simple); + string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof (TypeNameProperty), FormatterAssemblyStyle.Simple, null); TypeNameProperty typeNameProperty = new TypeNameProperty { @@ -527,7 +527,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void WriteListTypeNameForProperty() { - string listRef = ReflectionUtils.GetTypeName(typeof (List), FormatterAssemblyStyle.Simple); + string listRef = ReflectionUtils.GetTypeName(typeof (List), FormatterAssemblyStyle.Simple, null); TypeNameProperty typeNameProperty = new TypeNameProperty { @@ -709,9 +709,9 @@ namespace Newtonsoft.Json.Tests.Serialization string json = sw.ToString(); - string contentSubClassRef = ReflectionUtils.GetTypeName(typeof (ContentSubClass), FormatterAssemblyStyle.Simple); - string dictionaryRef = ReflectionUtils.GetTypeName(typeof (Dictionary>), FormatterAssemblyStyle.Simple); - string listRef = ReflectionUtils.GetTypeName(typeof (List), FormatterAssemblyStyle.Simple); + string contentSubClassRef = ReflectionUtils.GetTypeName(typeof (ContentSubClass), FormatterAssemblyStyle.Simple, null); + string dictionaryRef = ReflectionUtils.GetTypeName(typeof (Dictionary>), FormatterAssemblyStyle.Simple, null); + string listRef = ReflectionUtils.GetTypeName(typeof (List), FormatterAssemblyStyle.Simple, null); string expected = @"{ ""TestMember"": { @@ -816,7 +816,7 @@ namespace Newtonsoft.Json.Tests.Serialization TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); - string urlStatusTypeName = ReflectionUtils.GetTypeName(typeof (UrlStatus), FormatterAssemblyStyle.Simple); + string urlStatusTypeName = ReflectionUtils.GetTypeName(typeof (UrlStatus), FormatterAssemblyStyle.Simple, null); Assert.AreEqual(@"{ ""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"", @@ -867,7 +867,7 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void SerializingIEnumerableOfTShouldRetainGenericTypeInfo() { - string productClassRef = ReflectionUtils.GetTypeName(typeof(CustomEnumerable), FormatterAssemblyStyle.Simple); + string productClassRef = ReflectionUtils.GetTypeName(typeof(CustomEnumerable), FormatterAssemblyStyle.Simple, null); CustomEnumerable products = new CustomEnumerable(); @@ -951,7 +951,7 @@ namespace Newtonsoft.Json.Tests.Serialization string output = JsonConvert.SerializeObject(testerObject, Formatting.Indented, jsonSettings); - string carClassRef = ReflectionUtils.GetTypeName(typeof (Car), FormatterAssemblyStyle.Simple); + string carClassRef = ReflectionUtils.GetTypeName(typeof (Car), FormatterAssemblyStyle.Simple, null); Assert.AreEqual(output, @"{ ""$type"": """ + carClassRef + @""", @@ -1445,7 +1445,7 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(1, (int)j["MyProperty"]); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) [Test] public void PropertyItemTypeNameHandlingDynamic() { @@ -1651,7 +1651,7 @@ namespace Newtonsoft.Json.Tests.Serialization public TypeNameHandlingTestObject Data { get; set; } } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) public class PropertyItemTypeNameHandlingDynamic { [JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/WebApiIntegrationTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/WebApiIntegrationTests.cs index 014081f9..936cd0db 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/WebApiIntegrationTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/WebApiIntegrationTests.cs @@ -59,7 +59,7 @@ namespace Newtonsoft.Json.Tests.Serialization nonSerializedField = "Error" }; -#if !(SILVERLIGHT || NETFX_CORE || NET20 || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || NET20 || PORTABLE || PORTABLE40) MemoryStream ms = new MemoryStream(); DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(SerializableType)); dataContractJsonSerializer.WriteObject(ms, serializableType); @@ -74,17 +74,17 @@ namespace Newtonsoft.Json.Tests.Serialization string json = JsonConvert.SerializeObject(serializableType, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver - { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) + { +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) IgnoreSerializableAttribute = false #endif - } + } }); Assert.AreEqual(expected, json); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Test] public void SerializeInheritedType() { @@ -114,7 +114,7 @@ namespace Newtonsoft.Json.Tests.Serialization } } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [Serializable] #else [JsonObject(MemberSerialization.Fields)] @@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Tests.Serialization } } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) [NonSerialized] #else [JsonIgnore] diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index 55fc3c75..edcc751a 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -30,6 +30,8 @@ using System.IO; #if NET20 using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Utilities.LinqBridge; +#else +using System.Runtime.Serialization.Json; #endif using System.Text; using System.Threading; @@ -53,6 +55,18 @@ namespace Newtonsoft.Json.Tests [TestFixture] public abstract class TestFixtureBase { +#if !NET20 + protected string GetDataContractJsonSerializeResult(object o) + { + MemoryStream ms = new MemoryStream(); + DataContractJsonSerializer s = new DataContractJsonSerializer(o.GetType()); + s.WriteObject(ms, o); + + var data = ms.ToArray(); + return Encoding.UTF8.GetString(data, 0, data.Length); + } +#endif + protected string GetOffset(DateTime d, DateFormatHandling dateFormatHandling) { StringWriter sw = new StringWriter(); @@ -62,6 +76,48 @@ namespace Newtonsoft.Json.Tests return sw.ToString(); } + protected string BytesToHex(byte[] bytes) + { + return BytesToHex(bytes, false); + } + + protected string BytesToHex(byte[] bytes, bool removeDashes) + { + string hex = BitConverter.ToString(bytes); + if (removeDashes) + hex = hex.Replace("-", ""); + + return hex; + } + + protected byte[] HexToBytes(string hex) + { + string fixedHex = hex.Replace("-", string.Empty); + + // array to put the result in + byte[] bytes = new byte[fixedHex.Length / 2]; + // variable to determine shift of high/low nibble + int shift = 4; + // offset of the current byte in the array + int offset = 0; + // loop the characters in the string + foreach (char c in fixedHex) + { + // get character code in range 0-9, 17-22 + // the % 32 handles lower case characters + int b = (c - '0') % 32; + // correction for a-f + if (b > 9) b -= 7; + // store nibble (4 bits) in byte array + bytes[offset] |= (byte)(b << shift); + // toggle the shift variable between 0 and 4 + shift ^= 4; + // move to next byte + if (shift != 0) offset++; + } + return bytes; + } + #if !NETFX_CORE [SetUp] protected void TestSetup() diff --git a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs index 4cf32aa3..0a36100f 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE || NETFX_CORE || PORTABLE40) using System; using System.Reflection; #if !NETFX_CORE diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs index f8510d32..6e723b02 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !PORTABLE +#if !(PORTABLE || PORTABLE40) using global::System; using global::System.Collections.Generic; using global::System.Runtime.Serialization.Formatters; @@ -46,16 +46,16 @@ namespace Newtonsoft.Json.Tests.Utilities { string typeName; - typeName = ReflectionUtils.GetTypeName(typeof(IList), FormatterAssemblyStyle.Simple); + typeName = ReflectionUtils.GetTypeName(typeof(IList), FormatterAssemblyStyle.Simple, null); Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName); - typeName = ReflectionUtils.GetTypeName(typeof(IDictionary, IList>), FormatterAssemblyStyle.Simple); + typeName = ReflectionUtils.GetTypeName(typeof(IDictionary, IList>), FormatterAssemblyStyle.Simple, null); Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName); - typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple); + typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple, null); Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName); - typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple); + typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple, null); Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName); } } diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryType.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryType.cs index 08264466..d4977ac5 100644 --- a/Src/Newtonsoft.Json/Bson/BsonBinaryType.cs +++ b/Src/Newtonsoft.Json/Bson/BsonBinaryType.cs @@ -32,8 +32,10 @@ namespace Newtonsoft.Json.Bson Binary = 0x00, Function = 0x01, [Obsolete("This type has been deprecated in the BSON specification. Use Binary instead.")] - Data = 0x02, - Uuid = 0x03, + BinaryOld = 0x02, + [Obsolete("This type has been deprecated in the BSON specification. Use Uuid instead.")] + UuidOld = 0x03, + Uuid = 0x04, Md5 = 0x05, UserDefined = 0x80 } diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs index 2775c52e..65908a3e 100644 --- a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs @@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Bson public void Close() { -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) _writer.Close(); #else _writer.Dispose(); @@ -161,11 +161,11 @@ namespace Newtonsoft.Json.Bson break; case BsonType.Binary: { - BsonValue value = (BsonValue)t; + BsonBinary value = (BsonBinary)t; byte[] data = (byte[])value.Value; _writer.Write(data.Length); - _writer.Write((byte)BsonBinaryType.Binary); + _writer.Write((byte)value.BinaryType); _writer.Write(data); } break; @@ -298,7 +298,7 @@ namespace Newtonsoft.Json.Bson return 8; case BsonType.Binary: { - BsonValue value = (BsonValue)t; + BsonBinary value = (BsonBinary)t; byte[] data = (byte[])value.Value; value.CalculatedSize = 4 + 1 + data.Length; diff --git a/Src/Newtonsoft.Json/Bson/BsonReader.cs b/Src/Newtonsoft.Json/Bson/BsonReader.cs index 16207dbe..8d1e6e27 100644 --- a/Src/Newtonsoft.Json/Bson/BsonReader.cs +++ b/Src/Newtonsoft.Json/Bson/BsonReader.cs @@ -89,6 +89,7 @@ namespace Newtonsoft.Json.Bson /// /// true if binary data reading will be compatible with incorrect Json.NET 3.5 written binary; otherwise, false. /// + [Obsolete("JsonNet35BinaryCompatibility will be removed in a future version of Json.NET.")] public bool JsonNet35BinaryCompatibility { get { return _jsonNet35BinaryCompatibility; } @@ -295,7 +296,7 @@ namespace Newtonsoft.Json.Bson base.Close(); if (CloseInput && _reader != null) -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) _reader.Close(); #else _reader.Dispose(); @@ -608,7 +609,7 @@ namespace Newtonsoft.Json.Bson #pragma warning disable 612,618 // the old binary type has the data length repeated in the data for some reason - if (binaryType == BsonBinaryType.Data && !_jsonNet35BinaryCompatibility) + if (binaryType == BsonBinaryType.BinaryOld && !_jsonNet35BinaryCompatibility) { dataLength = ReadInt32(); } diff --git a/Src/Newtonsoft.Json/Bson/BsonToken.cs b/Src/Newtonsoft.Json/Bson/BsonToken.cs index 97a98aea..601d0588 100644 --- a/Src/Newtonsoft.Json/Bson/BsonToken.cs +++ b/Src/Newtonsoft.Json/Bson/BsonToken.cs @@ -121,6 +121,17 @@ namespace Newtonsoft.Json.Bson } } + internal class BsonBinary : BsonValue + { + public BsonBinaryType BinaryType { get; set; } + + public BsonBinary(byte[] value, BsonBinaryType binaryType) + : base(value, BsonType.Binary) + { + BinaryType = binaryType; + } + } + internal class BsonRegex : BsonToken { public BsonString Pattern { get; set; } diff --git a/Src/Newtonsoft.Json/Bson/BsonType.cs b/Src/Newtonsoft.Json/Bson/BsonType.cs index f4e12149..73361e9d 100644 --- a/Src/Newtonsoft.Json/Bson/BsonType.cs +++ b/Src/Newtonsoft.Json/Bson/BsonType.cs @@ -25,7 +25,7 @@ namespace Newtonsoft.Json.Bson { - internal enum BsonType : sbyte + internal enum BsonType : sbyte { Number = 1, String = 2, diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index 7259fd18..3ebef070 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -27,7 +27,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.IO; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using System.Text; @@ -221,6 +221,25 @@ namespace Newtonsoft.Json.Bson } #region WriteValue methods + /// + /// Writes a value. + /// An error will raised if the value cannot be written as a single JSON token. + /// + /// The value to write. + public override void WriteValue(object value) + { +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + if (value is BigInteger) + { + InternalWriteValue(JsonToken.Integer); + AddToken(new BsonBinary(((BigInteger)value).ToByteArray(), BsonBinaryType.Binary)); + } + else +#endif + { + base.WriteValue(value); + } + } /// /// Writes a null value. @@ -360,7 +379,7 @@ namespace Newtonsoft.Json.Bson { base.WriteValue(value); string s = null; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) s = value.ToString(CultureInfo.InvariantCulture); #else s = value.ToString(); @@ -429,7 +448,7 @@ namespace Newtonsoft.Json.Bson public override void WriteValue(byte[] value) { base.WriteValue(value); - AddValue(value, BsonType.Binary); + AddToken(new BsonBinary(value, BsonBinaryType.Binary)); } /// @@ -439,7 +458,7 @@ namespace Newtonsoft.Json.Bson public override void WriteValue(Guid value) { base.WriteValue(value); - AddToken(new BsonString(value.ToString(), true)); + AddToken(new BsonBinary(value.ToByteArray(), BsonBinaryType.Uuid)); } /// @@ -461,18 +480,6 @@ namespace Newtonsoft.Json.Bson base.WriteValue(value); AddToken(new BsonString(value.ToString(), true)); } - -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - base.WriteValue(value); - AddToken(new BsonValue(value.ToByteArray(), BsonType.Binary)); - } -#endif #endregion /// diff --git a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs index f0ed62eb..0da426d4 100644 --- a/Src/Newtonsoft.Json/Converters/BinaryConverter.cs +++ b/Src/Newtonsoft.Json/Converters/BinaryConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using System.Data.SqlTypes; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Converters/DataSetConverter.cs b/Src/Newtonsoft.Json/Converters/DataSetConverter.cs index 4f74b5dc..abed0d63 100644 --- a/Src/Newtonsoft.Json/Converters/DataSetConverter.cs +++ b/Src/Newtonsoft.Json/Converters/DataSetConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using System.Data; using Newtonsoft.Json.Serialization; diff --git a/Src/Newtonsoft.Json/Converters/DataTableConverter.cs b/Src/Newtonsoft.Json/Converters/DataTableConverter.cs index 0670a4e9..8c77d30f 100644 --- a/Src/Newtonsoft.Json/Converters/DataTableConverter.cs +++ b/Src/Newtonsoft.Json/Converters/DataTableConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using System.Data; using Newtonsoft.Json.Serialization; diff --git a/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs b/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs index 9e7ecd56..44b92307 100644 --- a/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs +++ b/Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using Newtonsoft.Json.Serialization; using System.Globalization; diff --git a/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs index 653cc56d..76754794 100644 --- a/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs +++ b/Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; diff --git a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs index 17f210ea..bb41ff27 100644 --- a/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs +++ b/Src/Newtonsoft.Json/Converters/KeyValuePairConverter.cs @@ -116,7 +116,7 @@ namespace Newtonsoft.Json.Converters reader.Read(); } - return ReflectionUtils.CreateInstance(t, key, value); + return Activator.CreateInstance(t, key, value); } /// diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index 682cbdd1..38878409 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -23,12 +23,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if (!(SILVERLIGHT) || WINDOWS_PHONE) +#if (!(SILVERLIGHT) || WINDOWS_PHONE) && !PORTABLE40 using System; using System.Collections.Generic; using System.Globalization; using System.Xml; -#if !NET20 +#if !(NET20 || PORTABLE40) using System.Xml.Linq; #endif using Newtonsoft.Json.Utilities; @@ -41,7 +41,7 @@ using System.Linq; namespace Newtonsoft.Json.Converters { #region XmlNodeWrappers -#if !SILVERLIGHT && !NETFX_CORE && !PORTABLE +#if !SILVERLIGHT && !NETFX_CORE && !PORTABLE && !PORTABLE40 internal class XmlDocumentWrapper : XmlNodeWrapper, IXmlDocument { private readonly XmlDocument _document; @@ -195,11 +195,6 @@ namespace Newtonsoft.Json.Converters get { return _node.NodeType; } } - public string Name - { - get { return _node.Name; } - } - public string LocalName { get { return _node.LocalName; } @@ -263,11 +258,6 @@ namespace Newtonsoft.Json.Converters return newChild; } - public string Prefix - { - get { return _node.Prefix; } - } - public string NamespaceUri { get { return _node.NamespaceURI; } @@ -1308,7 +1298,7 @@ namespace Newtonsoft.Json.Converters if (count == 1 && WriteArrayAttribute) { - IXmlElement arrayElement = nestedArrayElement.ChildNodes.CastValid().Single(n => n.LocalName == propertyName); + IXmlElement arrayElement = nestedArrayElement.ChildNodes.OfType().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } } @@ -1489,7 +1479,7 @@ namespace Newtonsoft.Json.Converters if (count == 1 && WriteArrayAttribute) { - IXmlElement arrayElement = currentNode.ChildNodes.CastValid().Single(n => n.LocalName == propertyName); + IXmlElement arrayElement = currentNode.ChildNodes.OfType().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } } diff --git a/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs b/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs index 215e866b..c2c3e443 100644 --- a/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs +++ b/Src/Newtonsoft.Json/FormatterAssemblyStyle.cs @@ -1,4 +1,4 @@ -#if SILVERLIGHT || PocketPC || NETFX_CORE || PORTABLE +#if SILVERLIGHT || PocketPC || NETFX_CORE || PORTABLE40 || PORTABLE namespace System.Runtime.Serialization.Formatters { /// diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs index 035d68dd..64a471b6 100644 --- a/Src/Newtonsoft.Json/JsonConvert.cs +++ b/Src/Newtonsoft.Json/JsonConvert.cs @@ -26,10 +26,10 @@ using System; using System.IO; using System.Globalization; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) using System.Threading.Tasks; #endif using Newtonsoft.Json.Linq; @@ -37,7 +37,7 @@ using Newtonsoft.Json.Utilities; using System.Xml; using Newtonsoft.Json.Converters; using System.Text; -#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE) +#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE) && !PORTABLE40 using System.Xml.Linq; #endif @@ -422,13 +422,8 @@ namespace Newtonsoft.Json return value.ToString(null, CultureInfo.InvariantCulture); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Converts the to its JSON string representation. - /// - /// The value to convert. - /// A JSON string representation of the . - public static string ToString(BigInteger value) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) + private static string ToStringInternal(BigInteger value) { return value.ToString(null, CultureInfo.InvariantCulture); } @@ -547,7 +542,7 @@ namespace Newtonsoft.Json { string text = null; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) text = value.ToString("D", CultureInfo.InvariantCulture); #else text = value.ToString("D"); @@ -671,9 +666,9 @@ namespace Newtonsoft.Json return ToString((Uri) value); case PrimitiveTypeCode.TimeSpan: return ToString((TimeSpan) value); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) case PrimitiveTypeCode.BigInteger: - return ToString((BigInteger) value); + return ToStringInternal((BigInteger)value); #endif } @@ -929,7 +924,7 @@ namespace Newtonsoft.Json return sw.ToString(); } -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) /// /// Asynchronously serializes the specified object to a JSON string using a collection of . /// @@ -1122,7 +1117,7 @@ namespace Newtonsoft.Json return jsonSerializer.Deserialize(new JsonTextReader(sr), type); } -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) /// /// Asynchronously deserializes the JSON to the specified .NET type. /// @@ -1217,7 +1212,7 @@ namespace Newtonsoft.Json } } -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) /// /// Asynchronously populates the object with values from the JSON string. /// @@ -1236,7 +1231,7 @@ namespace Newtonsoft.Json } #endif -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) /// /// Serializes the XML node to a JSON string. /// @@ -1315,7 +1310,7 @@ namespace Newtonsoft.Json } #endif -#if !NET20 && (!(SILVERLIGHT) || WINDOWS_PHONE) +#if !NET20 && (!(SILVERLIGHT) || WINDOWS_PHONE) && !PORTABLE40 /// /// Serializes the to a JSON string. /// diff --git a/Src/Newtonsoft.Json/JsonException.cs b/Src/Newtonsoft.Json/JsonException.cs index 5a901967..ad4cdf15 100644 --- a/Src/Newtonsoft.Json/JsonException.cs +++ b/Src/Newtonsoft.Json/JsonException.cs @@ -35,7 +35,7 @@ namespace Newtonsoft.Json /// /// The exception thrown when an error occurs during Json serialization or deserialization. /// -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE40 || PORTABLE) [Serializable] #endif public class JsonException : Exception @@ -68,7 +68,7 @@ namespace Newtonsoft.Json { } -#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/JsonReaderException.cs b/Src/Newtonsoft.Json/JsonReaderException.cs index 9aefd25d..e842f381 100644 --- a/Src/Newtonsoft.Json/JsonReaderException.cs +++ b/Src/Newtonsoft.Json/JsonReaderException.cs @@ -33,7 +33,7 @@ namespace Newtonsoft.Json /// /// The exception thrown when an error occurs while reading Json text. /// -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE || PORTABLE40) [Serializable] #endif public class JsonReaderException : JsonException @@ -84,7 +84,7 @@ namespace Newtonsoft.Json { } -#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/JsonSerializationException.cs b/Src/Newtonsoft.Json/JsonSerializationException.cs index 3d132ddc..40cfdc00 100644 --- a/Src/Newtonsoft.Json/JsonSerializationException.cs +++ b/Src/Newtonsoft.Json/JsonSerializationException.cs @@ -33,7 +33,7 @@ namespace Newtonsoft.Json /// /// The exception thrown when an error occurs during Json serialization or deserialization. /// -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE40 || PORTABLE) [Serializable] #endif public class JsonSerializationException : JsonException @@ -66,7 +66,7 @@ namespace Newtonsoft.Json { } -#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index ab4d9196..db4fb92d 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using System.Text; @@ -81,10 +81,12 @@ namespace Newtonsoft.Json _chars = new char[4097]; } +#if DEBUG internal void SetCharBuffer(char[] chars) { _chars = chars; } +#endif private StringBuffer GetBuffer() { @@ -1257,7 +1259,7 @@ namespace Newtonsoft.Json } else { -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture); #else // todo - validate number was a valid integer to make sure overflow was the reason for failure @@ -1503,7 +1505,7 @@ namespace Newtonsoft.Json base.Close(); if (CloseInput && _reader != null) -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) _reader.Close(); #else _reader.Dispose(); diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 62505d9c..a3a41f7a 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Globalization; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using System.Text; @@ -143,7 +143,7 @@ namespace Newtonsoft.Json base.Close(); if (CloseOutput && _writer != null) -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) _writer.Close(); #else _writer.Dispose(); @@ -303,6 +303,26 @@ namespace Newtonsoft.Json } #region WriteValue methods + /// + /// Writes a value. + /// An error will raised if the value cannot be written as a single JSON token. + /// + /// The value to write. + public override void WriteValue(object value) + { +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + if (value is BigInteger) + { + InternalWriteValue(JsonToken.Integer); + WriteValueInternal(((BigInteger)value).ToString(CultureInfo.InvariantCulture), JsonToken.String); + } + else +#endif + { + base.WriteValue(value); + } + } + /// /// Writes a null value. /// @@ -597,18 +617,6 @@ namespace Newtonsoft.Json WriteValueInternal(JsonConvert.ToString(value, _quoteChar), JsonToken.String); } } - -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - InternalWriteValue(JsonToken.Integer); - WriteValueInternal(value.ToString(CultureInfo.InvariantCulture), JsonToken.String); - } -#endif #endregion /// diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index f43da0af..a2926218 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -25,7 +25,7 @@ using System; using System.Collections.Generic; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using Newtonsoft.Json.Linq; @@ -735,7 +735,7 @@ namespace Newtonsoft.Json if (schema.DivisibleBy != null) { bool notDivisible; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (value is BigInteger) { // not that this will lose any decimal point on DivisibleBy @@ -749,7 +749,7 @@ namespace Newtonsoft.Json } else #endif - notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.Value); + notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.Value); if (notDivisible) RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema); diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index d5f390ff..31b2b4d0 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.IO; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using Newtonsoft.Json.Utilities; @@ -1231,29 +1231,6 @@ namespace Newtonsoft.Json InternalWriteValue(JsonToken.String); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public virtual void WriteValue(BigInteger value) - { - InternalWriteValue(JsonToken.Integer); - } - - /// - /// Writes a value. - /// - /// The value to write. - public virtual void WriteValue(BigInteger? value) - { - if (value == null) - WriteNull(); - else - WriteValue(value.Value); - } -#endif - /// /// Writes a value. /// An error will raised if the value cannot be written as a single JSON token. @@ -1262,9 +1239,20 @@ namespace Newtonsoft.Json public virtual void WriteValue(object value) { if (value == null) + { WriteNull(); + } else + { +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + // this is here because adding a WriteValue(BigInteger) to JsonWriter will + // mean the user has to add a reference to System.Numerics.dll + if (value is BigInteger) + throw CreateUnsupportedTypeException(this, value); +#endif + WriteValue(this, ConvertUtils.GetTypeCode(value), value); + } } #endregion @@ -1419,11 +1407,13 @@ namespace Newtonsoft.Json case PrimitiveTypeCode.TimeSpanNullable: writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value); break; -#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) case PrimitiveTypeCode.BigInteger: + // this will call to WriteValue(object) writer.WriteValue((BigInteger)value); break; case PrimitiveTypeCode.BigIntegerNullable: + // this will call to WriteValue(object) writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value); break; #endif @@ -1456,10 +1446,15 @@ namespace Newtonsoft.Json else #endif { - throw JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null); + throw CreateUnsupportedTypeException(writer, value); } break; } } + + private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value) + { + return JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonWriterException.cs b/Src/Newtonsoft.Json/JsonWriterException.cs index fdccc546..6c34172f 100644 --- a/Src/Newtonsoft.Json/JsonWriterException.cs +++ b/Src/Newtonsoft.Json/JsonWriterException.cs @@ -33,7 +33,7 @@ namespace Newtonsoft.Json /// /// The exception thrown when an error occurs while reading Json text. /// -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE40 || PORTABLE) [Serializable] #endif public class JsonWriterException : JsonException @@ -72,7 +72,7 @@ namespace Newtonsoft.Json { } -#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs index a5b01418..d4ab9d11 100644 --- a/Src/Newtonsoft.Json/Linq/IJEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/IJEnumerable.cs @@ -32,7 +32,7 @@ namespace Newtonsoft.Json.Linq /// /// The type of token public interface IJEnumerable< -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40) out #endif T> : IEnumerable where T : JToken diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 9ab9e4cc..5cce3789 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -25,7 +25,9 @@ using System; using System.Collections.Generic; +#if !PORTABLE40 using System.Collections.Specialized; +#endif using System.Threading; using Newtonsoft.Json.Utilities; using System.Collections; @@ -43,17 +45,17 @@ namespace Newtonsoft.Json.Linq /// Represents a token that can contain other tokens. /// public abstract class JContainer : JToken, IList -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) , ITypedList, IBindingList -#else +#elif PORTABLE , INotifyCollectionChanged #endif , IList -#if !(SILVERLIGHT || NET20 || NET35 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NET35 || NETFX_CORE || PORTABLE40 || PORTABLE) , INotifyCollectionChanged #endif { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) internal ListChangedEventHandler _listChanged; internal AddingNewEventHandler _addingNew; @@ -75,7 +77,7 @@ namespace Newtonsoft.Json.Linq remove { _addingNew -= value; } } #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) internal NotifyCollectionChangedEventHandler _collectionChanged; /// @@ -95,7 +97,9 @@ namespace Newtonsoft.Json.Linq protected abstract IList ChildrenTokens { get; } private object _syncRoot; +#if !(PORTABLE40) private bool _busy; +#endif internal JContainer() { @@ -114,8 +118,10 @@ namespace Newtonsoft.Json.Linq internal void CheckReentrancy() { +#if !(PORTABLE40) if (_busy) throw new InvalidOperationException("Cannot change {0} during a collection change event.".FormatWith(CultureInfo.InvariantCulture, GetType())); +#endif } internal virtual IList CreateChildrenCollection() @@ -123,7 +129,7 @@ namespace Newtonsoft.Json.Linq return new List(); } - #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Raises the event. /// @@ -157,7 +163,7 @@ namespace Newtonsoft.Json.Linq } } #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) /// /// Raises the event. /// @@ -350,11 +356,11 @@ namespace Newtonsoft.Json.Linq ChildrenTokens.Insert(index, item); -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (_listChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index)); #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) if (_collectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)); #endif @@ -384,11 +390,11 @@ namespace Newtonsoft.Json.Linq ChildrenTokens.RemoveAt(index); -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (_listChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index)); #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) if (_collectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); #endif @@ -448,11 +454,11 @@ namespace Newtonsoft.Json.Linq existing.Previous = null; existing.Next = null; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) if (_listChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index)); #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) if (_collectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, existing, index)); #endif @@ -471,11 +477,11 @@ namespace Newtonsoft.Json.Linq ChildrenTokens.Clear(); -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (_listChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) if (_collectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); #endif @@ -745,7 +751,7 @@ namespace Newtonsoft.Json.Linq return hashCode; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) string ITypedList.GetListName(PropertyDescriptor[] listAccessors) { return string.Empty; @@ -924,7 +930,7 @@ namespace Newtonsoft.Json.Linq #region IBindingList Members -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) void IBindingList.AddIndex(PropertyDescriptor property) { } diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 455902e2..7ba340d5 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -26,9 +26,11 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +#if !PORTABLE40 using System.Collections.Specialized; +#endif using System.ComponentModel; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.Dynamic; using System.Linq.Expressions; #endif @@ -50,10 +52,10 @@ namespace Newtonsoft.Json.Linq /// /// public class JObject : JContainer, IDictionary, INotifyPropertyChanged -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) , ICustomTypeDescriptor #endif -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) , INotifyPropertyChanging #endif { @@ -73,7 +75,7 @@ namespace Newtonsoft.Json.Linq /// public event PropertyChangedEventHandler PropertyChanged; -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE || PORTABLE40) /// /// Occurs when a property value is changing. /// @@ -156,11 +158,11 @@ namespace Newtonsoft.Json.Linq internal void InternalPropertyChanged(JProperty childProperty) { OnPropertyChanged(childProperty.Name); -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (_listChanged != null) OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty))); #endif -#if SILVERLIGHT || !(NET20 || NET35) +#if SILVERLIGHT || !(NET20 || NET35 || PORTABLE40) if (_collectionChanged != null) OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, childProperty, childProperty, IndexOfItem(childProperty))); #endif @@ -168,7 +170,7 @@ namespace Newtonsoft.Json.Linq internal void InternalPropertyChanging(JProperty childProperty) { -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) OnPropertyChanging(childProperty.Name); #endif } @@ -271,7 +273,7 @@ namespace Newtonsoft.Json.Linq } else { -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) OnPropertyChanging(propertyName); #endif Add(new JProperty(propertyName, value)); @@ -584,7 +586,7 @@ namespace Newtonsoft.Json.Linq PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || NET20) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE || NET20) /// /// Raises the event with the provided arguments. /// @@ -596,7 +598,7 @@ namespace Newtonsoft.Json.Linq } #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) // include custom type descriptor on JObject rather than use a provider because the properties are specific to a type #region ICustomTypeDescriptor /// @@ -755,7 +757,7 @@ namespace Newtonsoft.Json.Linq #endregion #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) /// /// Returns the responsible for binding operations performed on this object. /// diff --git a/Src/Newtonsoft.Json/Linq/JPath.cs b/Src/Newtonsoft.Json/Linq/JPath.cs index 572144fb..bd8eb160 100644 --- a/Src/Newtonsoft.Json/Linq/JPath.cs +++ b/Src/Newtonsoft.Json/Linq/JPath.cs @@ -173,7 +173,7 @@ namespace Newtonsoft.Json.Linq if (a.Count <= index) { if (errorWhenNoMatch) - throw new IndexOutOfRangeException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index)); + throw new JsonException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index)); return null; } @@ -185,7 +185,7 @@ namespace Newtonsoft.Json.Linq if (c.Count <= index) { if (errorWhenNoMatch) - throw new IndexOutOfRangeException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index)); + throw new JsonException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index)); return null; } diff --git a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs index 6f2282ba..377f6e8c 100644 --- a/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs +++ b/Src/Newtonsoft.Json/Linq/JPropertyDescriptor.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; using System.ComponentModel; using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 5dee3ce6..df2ad5a6 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -25,12 +25,12 @@ using System; using System.Collections.Generic; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.Dynamic; using System.Linq.Expressions; #endif using System.IO; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using System.Text; @@ -50,11 +50,11 @@ namespace Newtonsoft.Json.Linq /// Represents an abstract JSON token. /// public abstract class JToken : IJEnumerable, IJsonLineInfo -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) -, ICloneable +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) + , ICloneable #endif -#if !(NET35 || NET20) -, IDynamicMetaObjectProvider +#if !(NET35 || NET20 || PORTABLE40) + , IDynamicMetaObjectProvider #endif { private JContainer _parent; @@ -69,7 +69,7 @@ namespace Newtonsoft.Json.Linq private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean }; private static readonly JTokenType[] BigIntegerTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes }; private static readonly JTokenType[] StringTypes = new[] { JTokenType.Date, JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes, JTokenType.Guid, JTokenType.TimeSpan, JTokenType.Uri }; - private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid }; + private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid, JTokenType.Bytes }; private static readonly JTokenType[] TimeSpanTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.TimeSpan }; private static readonly JTokenType[] UriTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Uri }; private static readonly JTokenType[] CharTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw }; @@ -455,7 +455,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, BooleanTypes, false)) throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return Convert.ToBoolean((int)(BigInteger)v.Value); #endif @@ -497,7 +497,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, BooleanTypes, true)) throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return Convert.ToBoolean((int)(BigInteger)v.Value); #endif @@ -516,7 +516,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (long)(BigInteger)v.Value; #endif @@ -585,7 +585,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (decimal?)(BigInteger)v.Value; #endif @@ -607,7 +607,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (double?)(BigInteger)v.Value; #endif @@ -629,7 +629,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, CharTypes, true)) throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (char?)(BigInteger)v.Value; #endif @@ -648,7 +648,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (int)(BigInteger)v.Value; #endif @@ -667,7 +667,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (short)(BigInteger)v.Value; #endif @@ -687,7 +687,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (ushort)(BigInteger)v.Value; #endif @@ -707,7 +707,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, CharTypes, false)) throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (char)(BigInteger)v.Value; #endif @@ -726,7 +726,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (byte)(BigInteger)v.Value; #endif @@ -748,7 +748,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (int?)(BigInteger)v.Value; #endif @@ -770,7 +770,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (short?)(BigInteger)v.Value; #endif @@ -793,7 +793,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (ushort?)(BigInteger)v.Value; #endif @@ -815,7 +815,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (byte?)(BigInteger)v.Value; #endif @@ -856,7 +856,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (long?)(BigInteger)v.Value; #endif @@ -878,7 +878,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (float?)(BigInteger)v.Value; #endif @@ -897,7 +897,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (decimal)(BigInteger)v.Value; #endif @@ -920,7 +920,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (uint?)(BigInteger)v.Value; #endif @@ -943,7 +943,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, true)) throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (ulong?)(BigInteger)v.Value; #endif @@ -962,7 +962,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (double) (BigInteger) v.Value; #endif @@ -981,7 +981,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (float)(BigInteger)v.Value; #endif @@ -1007,7 +1007,7 @@ namespace Newtonsoft.Json.Linq return null; if (v.Value is byte[]) return Convert.ToBase64String((byte[]) v.Value); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return ((BigInteger)v.Value).ToString(CultureInfo.InvariantCulture); #endif @@ -1027,7 +1027,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (uint)(BigInteger)v.Value; #endif @@ -1047,7 +1047,7 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, NumberTypes, false)) throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return (ulong)(BigInteger)v.Value; #endif @@ -1071,7 +1071,7 @@ namespace Newtonsoft.Json.Linq if (v.Value is string) return Convert.FromBase64String(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (v.Value is BigInteger) return ((BigInteger)v.Value).ToByteArray(); #endif @@ -1093,7 +1093,10 @@ namespace Newtonsoft.Json.Linq if (v == null || !ValidateToken(v, GuidTypes, false)) throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value))); - return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); + if (v.Value is byte[]) + return new Guid((byte[]) v.Value); + + return (v.Value is Guid) ? (Guid) v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); } /// @@ -1113,6 +1116,9 @@ namespace Newtonsoft.Json.Linq if (v.Value == null) return null; + if (v.Value is byte[]) + return new Guid((byte[])v.Value); + return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); } @@ -1170,13 +1176,8 @@ namespace Newtonsoft.Json.Linq return (v.Value is Uri) ? (Uri)v.Value : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture)); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Performs an explicit conversion from to . - /// - /// The value. - /// The result of the conversion. - public static explicit operator BigInteger(JToken value) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) + private static BigInteger ToBigInteger(JToken value) { JValue v = EnsureValue(value); if (v == null || !ValidateToken(v, BigIntegerTypes, false)) @@ -1185,12 +1186,7 @@ namespace Newtonsoft.Json.Linq return ConvertUtils.ToBigInteger(v.Value); } - /// - /// Performs an explicit conversion from to . - /// - /// The value. - /// The result of the conversion. - public static explicit operator BigInteger?(JToken value) + private static BigInteger? ToBigIntegerNullable(JToken value) { JValue v = EnsureValue(value); if (v == null || !ValidateToken(v, BigIntegerTypes, true)) @@ -1415,18 +1411,6 @@ namespace Newtonsoft.Json.Linq return new JValue(value); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Performs an implicit conversion from to . - /// - /// The value to create a from. - /// The initialized with the specified value. - public static implicit operator JToken(BigInteger? value) - { - return new JValue(value); - } -#endif - /// /// Performs an implicit conversion from to . /// @@ -1489,18 +1473,6 @@ namespace Newtonsoft.Json.Linq return new JValue(value); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Performs an implicit conversion from to . - /// - /// The value to create a from. - /// The initialized with the specified value. - public static implicit operator JToken(BigInteger value) - { - return new JValue(value); - } -#endif - /// /// Performs an implicit conversion from to . /// @@ -1714,11 +1686,11 @@ namespace Newtonsoft.Json.Linq return (TimeSpan?)this; case PrimitiveTypeCode.TimeSpan: return (TimeSpan)this; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) case PrimitiveTypeCode.BigIntegerNullable: - return (BigInteger?)this; + return ToBigIntegerNullable(this); case PrimitiveTypeCode.BigInteger: - return (BigInteger)this; + return ToBigInteger(this); #endif } @@ -1881,7 +1853,7 @@ namespace Newtonsoft.Json.Linq return p.Evaluate(this, errorWhenNoMatch); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) /// /// Returns the responsible for binding operations performed on this object. /// @@ -1907,7 +1879,7 @@ namespace Newtonsoft.Json.Linq } #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) object ICloneable.Clone() { return DeepClone(); diff --git a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs index 27feebdf..4f89cbf1 100644 --- a/Src/Newtonsoft.Json/Linq/JTokenWriter.cs +++ b/Src/Newtonsoft.Json/Linq/JTokenWriter.cs @@ -25,7 +25,7 @@ using System; using System.Globalization; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using Newtonsoft.Json.Utilities; @@ -181,6 +181,26 @@ namespace Newtonsoft.Json.Linq } #region WriteValue methods + /// + /// Writes a value. + /// An error will raised if the value cannot be written as a single JSON token. + /// + /// The value to write. + public override void WriteValue(object value) + { +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) + if (value is BigInteger) + { + InternalWriteValue(JsonToken.Integer); + AddValue(value, JsonToken.Integer); + } + else +#endif + { + base.WriteValue(value); + } + } + /// /// Writes a null value. /// @@ -330,7 +350,7 @@ namespace Newtonsoft.Json.Linq { base.WriteValue(value); string s = null; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) s = value.ToString(CultureInfo.InvariantCulture); #else s = value.ToString(); @@ -431,18 +451,6 @@ namespace Newtonsoft.Json.Linq base.WriteValue(value); AddValue(value, JsonToken.String); } - -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) - /// - /// Writes a value. - /// - /// The value to write. - public override void WriteValue(BigInteger value) - { - base.WriteValue(value); - AddValue(value, JsonToken.Integer); - } -#endif #endregion } -} +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index 264a1130..c090a5a8 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -27,11 +27,11 @@ using System; using System.Collections.Generic; using Newtonsoft.Json.Utilities; using System.Globalization; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.Dynamic; using System.Linq.Expressions; #endif -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif @@ -194,7 +194,7 @@ namespace Newtonsoft.Json.Linq get { return false; } } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) private static int CompareBigInteger(BigInteger i1, object i2) { int result = i1.CompareTo(ConvertUtils.ToBigInteger(i2)); @@ -231,7 +231,7 @@ namespace Newtonsoft.Json.Linq switch (valueType) { case JTokenType.Integer: -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (objA is BigInteger) return CompareBigInteger((BigInteger)objA, objB); if (objB is BigInteger) @@ -244,7 +244,7 @@ namespace Newtonsoft.Json.Linq else return Convert.ToInt64(objA, CultureInfo.InvariantCulture).CompareTo(Convert.ToInt64(objB, CultureInfo.InvariantCulture)); case JTokenType.Float: -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (objA is BigInteger) return CompareBigInteger((BigInteger)objA, objB); if (objB is BigInteger) @@ -347,7 +347,7 @@ namespace Newtonsoft.Json.Linq return d1.CompareTo(d2); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) private static bool Operation(ExpressionType operation, object objA, object objB, out object result) { if (objA is string || objB is string) @@ -359,7 +359,7 @@ namespace Newtonsoft.Json.Linq } } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (objA is BigInteger || objB is BigInteger) { if (objA == null || objB == null) @@ -524,7 +524,7 @@ namespace Newtonsoft.Json.Linq { if (value == null) return JTokenType.Null; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) else if (value == DBNull.Value) return JTokenType.Null; #endif @@ -535,7 +535,7 @@ namespace Newtonsoft.Json.Linq return JTokenType.Integer; else if (value is Enum) return JTokenType.Integer; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) else if (value is BigInteger) return JTokenType.Integer; #endif @@ -637,12 +637,12 @@ namespace Newtonsoft.Json.Linq writer.WriteUndefined(); return; case JTokenType.Integer: -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (_value is BigInteger) writer.WriteValue((BigInteger)_value); else #endif - writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture)); + writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture)); return; case JTokenType.Float: if (_value is decimal) @@ -802,7 +802,7 @@ namespace Newtonsoft.Json.Linq return _value.ToString(); } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) /// /// Returns the responsible for binding operations performed on this object. /// @@ -929,7 +929,7 @@ namespace Newtonsoft.Json.Linq if (_value is DateTimeOffset) return TypeCode.DateTime; #endif -#if !(NET20 || NET35 || SILVERLIGHT) +#if !(NET20 || NET35 || PORTABLE40 || SILVERLIGHT) if (_value is BigInteger) return TypeCode.Object; #endif diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj index 1e5b31f6..4e54d7e7 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj @@ -232,7 +232,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 05d4c9b9..1acce3ec 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -246,7 +246,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj index 3d98fa84..0c007da1 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj @@ -244,7 +244,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj index 63baf38c..b9922e63 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj @@ -204,7 +204,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj new file mode 100644 index 00000000..ea320862 --- /dev/null +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj @@ -0,0 +1,228 @@ + + + + Debug + AnyCPU + {959F7F85-C98B-4876-971A-9036224578E4} + Library + Properties + Newtonsoft.Json + Newtonsoft.Json + v4.0 + Profile2 + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + + + true + full + false + bin\Debug\Portable40\ + TRACE;DEBUG;CODE_ANALYSIS;PORTABLE40 + prompt + 4 + true + Newtonsoft.Json.ruleset + + + pdbonly + true + bin\Release\Portable40\ + TRACE;CODE_ANALYSIS;PORTABLE40 + prompt + 4 + bin\Release\Portable40\Newtonsoft.Json.xml + true + Newtonsoft.Json.ruleset + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj index 92ccbd5a..d968d5f9 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WinRT.csproj @@ -23,6 +23,8 @@ DEBUG;TRACE;NETFX_CORE prompt 3 + Newtonsoft.Json.ruleset + true pdbonly @@ -32,6 +34,8 @@ prompt 4 bin\Release\WinRT\Newtonsoft.Json.xml + true + Newtonsoft.Json.ruleset true @@ -268,7 +272,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj index d260d720..2adffc9a 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj @@ -193,7 +193,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 6ab40ad8..2252a7b2 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -21,6 +21,8 @@ prompt 4 bin\Debug\Net45\Newtonsoft.Json.xml + true + Newtonsoft.Json.ruleset pdbonly @@ -30,6 +32,8 @@ prompt 4 bin\Release\Net45\Newtonsoft.Json.xml + true + Newtonsoft.Json.ruleset @@ -200,7 +204,6 @@ - diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 6358d699..181171b7 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -36,6 +36,8 @@ using System.Security; [assembly: AssemblyTitle("Json.NET Windows Phone")] #elif SILVERLIGHT [assembly: AssemblyTitle("Json.NET Silverlight")] +#elif PORTABLE40 +[assembly: AssemblyTitle("Json.NET Portable .NET 4.0")] #elif PORTABLE [assembly: AssemblyTitle("Json.NET Portable")] #elif NETFX_CORE @@ -70,7 +72,7 @@ using System.Security; [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -#if !PORTABLE +#if !(PORTABLE40 || PORTABLE) // Setting ComVisible to false makes the types in this assembly not visible // to COM componenets. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. @@ -90,6 +92,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.1.16001")] +[assembly: AssemblyFileVersion("5.0.1.16008")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs index 3fc70a5f..448bd4a4 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaException.cs @@ -31,7 +31,7 @@ namespace Newtonsoft.Json.Schema /// /// Returns detailed information about the schema exception. /// -#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE40 || PORTABLE) [Serializable] #endif public class JsonSchemaException : JsonException @@ -83,7 +83,7 @@ namespace Newtonsoft.Json.Schema { } - #if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(WINDOWS_PHONE || SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Initializes a new instance of the class. /// diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs index 6f552ce7..e462462d 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs @@ -179,7 +179,7 @@ namespace Newtonsoft.Json.Schema if (containerAttribute != null && !string.IsNullOrEmpty(containerAttribute.Description)) return containerAttribute.Description; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) DescriptionAttribute descriptionAttribute = ReflectionUtils.GetAttribute(type); if (descriptionAttribute != null) return descriptionAttribute.Description; @@ -327,14 +327,14 @@ namespace Newtonsoft.Json.Schema } } break; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) case JsonContractType.Serializable: CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired); CurrentSchema.Id = GetTypeId(type, false); GenerateISerializableContract(type, (JsonISerializableContract) contract); break; #endif -#if !(NET35 || NET20 || WINDOWS_PHONE) +#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE40) case JsonContractType.Dynamic: #endif case JsonContractType.Linq: @@ -386,7 +386,7 @@ namespace Newtonsoft.Json.Schema CurrentSchema.AllowAdditionalProperties = false; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) private void GenerateISerializableContract(Type type, JsonISerializableContract contract) { CurrentSchema.AllowAdditionalProperties = true; diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index b581f5ba..9d458d07 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -25,18 +25,18 @@ using System; using System.Collections; -#if !(NET35 || NET20 || SILVERLIGHT || PORTABLE) +#if !(NET35 || NET20 || SILVERLIGHT || PORTABLE || PORTABLE40) using System.Collections.Concurrent; #endif using System.Collections.Generic; using System.ComponentModel; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.Dynamic; #endif using System.Globalization; using System.Reflection; using System.Runtime.Serialization; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) using System.Security.Permissions; #endif using System.Xml.Serialization; @@ -94,16 +94,16 @@ namespace Newtonsoft.Json.Serialization } private static readonly IList BuiltInConverters = new List { -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) new EntityKeyMemberConverter(), #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) new ExpandoObjectConverter(), #endif -#if (!(SILVERLIGHT) || WINDOWS_PHONE) +#if (!(SILVERLIGHT || PORTABLE40) || WINDOWS_PHONE) new XmlNodeConverter(), #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) new BinaryConverter(), new DataSetConverter(), new DataTableConverter(), @@ -151,7 +151,7 @@ namespace Newtonsoft.Json.Serialization /// public bool SerializeCompilerGeneratedMembers { get; set; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) /// /// Gets or sets a value indicating whether to ignore the interface when serializing and deserializing types. /// @@ -191,7 +191,7 @@ namespace Newtonsoft.Json.Serialization #if !NETFX_CORE DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance; #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) IgnoreSerializableAttribute = true; #endif @@ -256,7 +256,7 @@ namespace Newtonsoft.Json.Serialization protected virtual List GetSerializableMembers(Type objectType) { bool ignoreSerializableAttribute; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) ignoreSerializableAttribute = IgnoreSerializableAttribute; #else ignoreSerializableAttribute = true; @@ -350,7 +350,7 @@ namespace Newtonsoft.Json.Serialization InitializeContract(contract); bool ignoreSerializableAttribute; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) ignoreSerializableAttribute = IgnoreSerializableAttribute; #else ignoreSerializableAttribute = true; @@ -373,7 +373,7 @@ namespace Newtonsoft.Json.Serialization } else if (contract.MemberSerialization == MemberSerialization.Fields) { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) // mimic DataContractSerializer behaviour when populating fields by overriding default creator to create an uninitialized object // note that this is only possible when the application is fully trusted so fall back to using the default constructor (if available) in partial trust if (JsonTypeReflector.FullyTrusted) @@ -571,7 +571,7 @@ namespace Newtonsoft.Json.Serialization if (onDeserialized != null) { // ConcurrentDictionary throws an error here so don't use its OnDeserialized - http://json.codeplex.com/discussions/257093 -#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE) +#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE || PORTABLE40) if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>))) contract.OnDeserializedCallbacks.AddRange(onDeserialized); #else @@ -715,7 +715,7 @@ namespace Newtonsoft.Json.Serialization return contract; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) /// /// Creates a for the given type. /// @@ -738,7 +738,7 @@ namespace Newtonsoft.Json.Serialization } #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) /// /// Creates a for the given type. /// @@ -802,12 +802,12 @@ namespace Newtonsoft.Json.Serialization if (CanConvertToString(t)) return CreateStringContract(objectType); -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t)) return CreateISerializableContract(objectType); #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t)) return CreateDynamicContract(objectType); #endif @@ -817,7 +817,7 @@ namespace Newtonsoft.Json.Serialization internal static bool CanConvertToString(Type type) { -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) TypeConverter converter = ConvertUtils.GetConverter(type); // use the objectType's TypeConverter if it has one and can convert to a string @@ -921,7 +921,7 @@ namespace Newtonsoft.Json.Serialization // warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes IValueProvider valueProvider; -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) if (DynamicCodeGeneration) valueProvider = new DynamicValueProvider(member); else @@ -1018,15 +1018,15 @@ namespace Newtonsoft.Json.Serialization bool hasJsonIgnoreAttribute = JsonTypeReflector.GetAttribute(attributeProvider) != null -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) || JsonTypeReflector.GetAttribute(attributeProvider) != null #endif - ; +; if (memberSerialization != MemberSerialization.OptIn) { - bool hasIgnoreDataMemberAttribute = false; - + bool hasIgnoreDataMemberAttribute = false; + #if !(NET20 || NET35) hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute(attributeProvider) != null); #endif diff --git a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs index 3ce9000f..1d743665 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs @@ -49,7 +49,7 @@ namespace Newtonsoft.Json.Serialization { Assembly assembly; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) // look, I don't like using obsolete methods as much as you do but this is the only way // Assembly.Load won't check the GAC for a partial name #pragma warning disable 618,612 diff --git a/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs b/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs index f9555b98..96c75a95 100644 --- a/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs @@ -1,4 +1,4 @@ -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) using System; using System.Diagnostics; using DiagnosticsTrace = System.Diagnostics.Trace; diff --git a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs index a6d178f5..bb6a93d6 100644 --- a/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs +++ b/Src/Newtonsoft.Json/Serialization/DynamicValueProvider.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) using System; using System.Collections.Generic; #if NET20 diff --git a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs index 6b1861a9..af7e4f79 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonArrayContract.cs @@ -83,7 +83,7 @@ namespace Newtonsoft.Json.Serialization { CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType); IsReadOnlyOrFixedSize = true; - _genericCollectionDefinitionType = ReflectionUtils.MakeGenericType(typeof (List<>), CollectionItemType); + _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); canDeserialize = true; IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1); @@ -100,6 +100,8 @@ namespace Newtonsoft.Json.Serialization if (CollectionItemType != null) ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); + + IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection<>)); canDeserialize = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType)) @@ -108,27 +110,27 @@ namespace Newtonsoft.Json.Serialization if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection<>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList<>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType); + CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); -#if !(NET20 || NET35) +#if !(NET20 || NET35 || PORTABLE40) if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet<>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet<>), CollectionItemType); + CreatedType = typeof(HashSet<>).MakeGenericType(CollectionItemType); #endif ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); canDeserialize = true; ShouldCreateWrapper = true; } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof (IReadOnlyCollection<>), out tempCollectionType)) { CollectionItemType = underlyingType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof (IReadOnlyCollection<>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof (IReadOnlyList<>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(ReadOnlyCollection<>), CollectionItemType); + CreatedType = typeof(ReadOnlyCollection<>).MakeGenericType(CollectionItemType); - _genericCollectionDefinitionType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType); + _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, CollectionItemType); IsReadOnlyOrFixedSize = true; canDeserialize = (ParametrizedConstructor != null); @@ -139,7 +141,7 @@ namespace Newtonsoft.Json.Serialization CollectionItemType = tempCollectionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable<>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType); + CreatedType = typeof(List<>).MakeGenericType(CollectionItemType); ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(underlyingType, CollectionItemType); @@ -153,7 +155,7 @@ namespace Newtonsoft.Json.Serialization } else { - _genericCollectionDefinitionType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType); + _genericCollectionDefinitionType = typeof(List<>).MakeGenericType(CollectionItemType); IsReadOnlyOrFixedSize = true; ShouldCreateWrapper = true; @@ -188,13 +190,13 @@ namespace Newtonsoft.Json.Serialization { if (_genericWrapperCreator == null) { - _genericWrapperType = ReflectionUtils.MakeGenericType(typeof (CollectionWrapper<>), CollectionItemType); + _genericWrapperType = typeof(CollectionWrapper<>).MakeGenericType(CollectionItemType); Type constructorArgument; if (ReflectionUtils.InheritsGenericDefinition(_genericCollectionDefinitionType, typeof(List<>)) || _genericCollectionDefinitionType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) - constructorArgument = ReflectionUtils.MakeGenericType(typeof(ICollection<>), CollectionItemType); + constructorArgument = typeof(ICollection<>).MakeGenericType(CollectionItemType); else constructorArgument = _genericCollectionDefinitionType; @@ -211,7 +213,7 @@ namespace Newtonsoft.Json.Serialization { // multidimensional array will also have array instances in it Type collectionItemType = (IsMultidimensionalArray) ? typeof (object) : CollectionItemType; - Type temporaryListType = ReflectionUtils.MakeGenericType(typeof(List<>), collectionItemType); + Type temporaryListType = typeof(List<>).MakeGenericType(collectionItemType); _genericTemporaryCollectionCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor(temporaryListType); } diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index edb91e07..a9bf1963 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -39,10 +39,10 @@ namespace Newtonsoft.Json.Serialization Primitive, String, Dictionary, -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) Dynamic, #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) Serializable, #endif Linq diff --git a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs index 0ec48de6..5d51aec4 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDictionaryContract.cs @@ -88,16 +88,20 @@ namespace Newtonsoft.Json.Serialization valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary<,>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(Dictionary<,>), keyType, valueType); + CreatedType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); + +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) + IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary<,>)); +#endif } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary<,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IReadOnlyDictionary<,>))) - CreatedType = ReflectionUtils.MakeGenericType(typeof(ReadOnlyDictionary<,>), keyType, valueType); + CreatedType = typeof(ReadOnlyDictionary<,>).MakeGenericType(keyType, valueType); IsReadOnlyOrFixedSize = true; } @@ -111,7 +115,7 @@ namespace Newtonsoft.Json.Serialization } if (keyType != null && valueType != null) - ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, ReflectionUtils.MakeGenericType(typeof(KeyValuePair<,>), keyType, valueType)); + ParametrizedConstructor = CollectionUtils.ResolveEnumableCollectionConstructor(CreatedType, typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType)); ShouldCreateWrapper = !typeof (IDictionary).IsAssignableFrom(CreatedType); @@ -138,7 +142,7 @@ namespace Newtonsoft.Json.Serialization { if (_genericWrapperCreator == null) { - _genericWrapperType = ReflectionUtils.MakeGenericType(typeof(DictionaryWrapper<,>), DictionaryKeyType, DictionaryValueType); + _genericWrapperType = typeof(DictionaryWrapper<,>).MakeGenericType(DictionaryKeyType, DictionaryValueType); ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType }); _genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall(genericWrapperConstructor); @@ -151,7 +155,7 @@ namespace Newtonsoft.Json.Serialization { if (_genericTemporaryDictionaryCreator == null) { - Type temporaryDictionaryType = ReflectionUtils.MakeGenericType(typeof (Dictionary<,>), DictionaryKeyType, DictionaryValueType); + Type temporaryDictionaryType = typeof(Dictionary<,>).MakeGenericType(DictionaryKeyType, DictionaryValueType); _genericTemporaryDictionaryCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor(temporaryDictionaryType); } diff --git a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs index 0d9abc3b..6092dbf3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonDynamicContract.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; using System.Dynamic; diff --git a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs index 4ecbdb21..e6a5533c 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using System.Globalization; using System.Runtime.Serialization; diff --git a/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs b/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs index d99a5f8b..980d2dc0 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonISerializableContract.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) using System; namespace Newtonsoft.Json.Serialization diff --git a/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs b/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs index 0a4806d3..545e5d97 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonObjectContract.cs @@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization ConstructorParameters = new JsonPropertyCollection(UnderlyingType); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) #if !(NET20 || NET35) [SecuritySafeCritical] #endif diff --git a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs index 35c2973b..ed54fc98 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs @@ -24,7 +24,7 @@ #endregion using System; -#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) using System.Numerics; #endif using Newtonsoft.Json.Utilities; diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index d9ca5dcc..e91d5dd9 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -41,6 +41,9 @@ namespace Newtonsoft.Json.Serialization internal bool _hasExplicitDefaultValue; internal object _defaultValue; + private string _propertyName; + private bool _skipPropertyNameEscape; + // use to cache contract during deserialization internal JsonContract PropertyContract { get; set; } @@ -48,7 +51,35 @@ namespace Newtonsoft.Json.Serialization /// Gets or sets the name of the property. /// /// The name of the property. - public string PropertyName { get; set; } + public string PropertyName + { + get { return _propertyName; } + set + { + _propertyName = value; + CalculateSkipPropertyNameEscape(); + } + } + + private void CalculateSkipPropertyNameEscape() + { + if (_propertyName == null) + { + _skipPropertyNameEscape = false; + } + else + { + _skipPropertyNameEscape = true; + foreach (char c in _propertyName) + { + if (!char.IsLetterOrDigit(c) && c != '_' && c != '@') + { + _skipPropertyNameEscape = false; + break; + } + } + } + } /// /// Gets or sets the type that declared this property. @@ -242,5 +273,13 @@ namespace Newtonsoft.Json.Serialization /// /// The collection's items reference loop handling. public ReferenceLoopHandling? ItemReferenceLoopHandling { get; set; } + + internal void WritePropertyName(JsonWriter writer) + { + if (_skipPropertyNameEscape) + writer.WritePropertyName(PropertyName, false); + else + writer.WritePropertyName(PropertyName); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs index 41b953be..af4b4601 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs @@ -81,19 +81,21 @@ namespace Newtonsoft.Json.Serialization Remove(existingProperty); duplicateProperty = false; } - - if (property.DeclaringType != null && existingProperty.DeclaringType != null) + else { - if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType)) + if (property.DeclaringType != null && existingProperty.DeclaringType != null) { - // current property is on a derived class and hides the existing - Remove(existingProperty); - duplicateProperty = false; - } - if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType)) - { - // current property is hidden by the existing so don't add it - return; + if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType)) + { + // current property is on a derived class and hides the existing + Remove(existingProperty); + duplicateProperty = false; + } + if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType)) + { + // current property is hidden by the existing so don't add it + return; + } } } diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 5be873ed..802fa4c3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -27,13 +27,13 @@ using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.ComponentModel; using System.Dynamic; #endif using System.Diagnostics; using System.Globalization; -#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || SILVERLIGHT) using System.Numerics; #endif using System.Reflection; @@ -58,7 +58,7 @@ namespace Newtonsoft.Json.Serialization } private JsonSerializerProxy _internalSerializer; -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) private JsonFormatterConverter _formatterConverter; #endif @@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Serialization return _internalSerializer; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) private JsonFormatterConverter GetFormatterConverter() { if (_formatterConverter == null) @@ -276,7 +276,7 @@ namespace Newtonsoft.Json.Serialization return EnsureType(reader, constructorName, CultureInfo.InvariantCulture, contract, objectType); case JsonToken.Null: case JsonToken.Undefined: -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) if (objectType == typeof (DBNull)) return DBNull.Value; #endif @@ -301,10 +301,10 @@ namespace Newtonsoft.Json.Serialization { case JsonContractType.Object: case JsonContractType.Dictionary: -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) case JsonContractType.Serializable: #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) case JsonContractType.Dynamic: #endif return @"JSON object (e.g. {""name"":""value""})"; @@ -443,12 +443,12 @@ namespace Newtonsoft.Json.Serialization return targetDictionary; } -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) case JsonContractType.Dynamic: JsonDynamicContract dynamicContract = (JsonDynamicContract) contract; return CreateDynamic(reader, dynamicContract, member, id); #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) case JsonContractType.Serializable: JsonISerializableContract serializableContract = (JsonISerializableContract) contract; return CreateISerializable(reader, serializableContract, id); @@ -540,7 +540,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved type '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName, specifiedType)), null); if (objectType != null -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) && objectType != typeof (IDynamicMetaObjectProvider) #endif && !objectType.IsAssignableFrom(specifiedType)) @@ -669,7 +669,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ private bool HasDefinedType(Type type) { return (type != null && type != typeof (object) && !typeof (JToken).IsSubclassOf(type) -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) && type != typeof (IDynamicMetaObjectProvider) #endif ); @@ -703,7 +703,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ return Enum.ToObject(contract.NonNullableUnderlyingType, value); } -#if !(PORTABLE || NET35 || NET20 || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || SILVERLIGHT) if (value is BigInteger) return ConvertUtils.FromBigInteger((BigInteger)value, targetType); #endif @@ -791,7 +791,12 @@ To fix this error either change the JSON to a {1} or change the deserialized typ currentValue = property.ValueProvider.GetValue(target); gottenCurrentValue = true; - useExistingValue = (currentValue != null && !property.PropertyContract.IsReadOnlyOrFixedSize && !property.PropertyType.IsValueType()); + if (currentValue != null) + { + propertyContract = GetContractSafe(currentValue.GetType()); + + useExistingValue = (!propertyContract.IsReadOnlyOrFixedSize && !propertyContract.UnderlyingType.IsValueType()); + } } if (!property.Writable && !useExistingValue) @@ -1262,7 +1267,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ return underlyingList; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, string id) { Type objectType = contract.UnderlyingType; @@ -1320,7 +1325,7 @@ To fix this error either change the environment to be fully trusted, change the } #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) private object CreateDynamic(JsonReader reader, JsonDynamicContract contract, JsonProperty member, string id) { IDynamicMetaObjectProvider newObject; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index b1d6e8e0..034c0c79 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -27,7 +27,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System.Dynamic; #endif using System.Diagnostics; @@ -93,7 +93,7 @@ namespace Newtonsoft.Json.Serialization { writer.WriteStartObject(); WriteTypeProperty(writer, contract.CreatedType); - writer.WritePropertyName(JsonTypeReflector.ValuePropertyName); + writer.WritePropertyName(JsonTypeReflector.ValuePropertyName, false); JsonWriter.WriteValue(writer, contract.TypeCode, value); @@ -148,12 +148,12 @@ namespace Newtonsoft.Json.Serialization JsonDictionaryContract dictionaryContract = (JsonDictionaryContract) valueContract; SerializeDictionary(writer, (value is IDictionary) ? (IDictionary) value : dictionaryContract.CreateWrapper(value), dictionaryContract, member, containerContract, containerProperty); break; -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) case JsonContractType.Dynamic: SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, containerContract, containerProperty); break; #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) case JsonContractType.Serializable: SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty); break; @@ -271,7 +271,7 @@ namespace Newtonsoft.Json.Serialization TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference to Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, value.GetType())), null); writer.WriteStartObject(); - writer.WritePropertyName(JsonTypeReflector.RefPropertyName); + writer.WritePropertyName(JsonTypeReflector.RefPropertyName, false); writer.WriteValue(reference); writer.WriteEndObject(); } @@ -292,7 +292,7 @@ namespace Newtonsoft.Json.Serialization internal static bool TryConvertToString(object value, Type type, out string s) { -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) TypeConverter converter = ConvertUtils.GetConverter(type); // use the objectType's TypeConverter if it has one and can convert to a string @@ -380,7 +380,7 @@ namespace Newtonsoft.Json.Serialization if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue)) continue; - writer.WritePropertyName(property.PropertyName); + property.WritePropertyName(writer); SerializeValue(writer, memberValue, memberContract, property, contract, member); } catch (Exception ex) @@ -413,7 +413,7 @@ namespace Newtonsoft.Json.Serialization { if (ShouldWriteReference(memberValue, property, memberContract, contract, member)) { - writer.WritePropertyName(property.PropertyName); + property.WritePropertyName(writer); WriteReference(writer, memberValue); return false; } @@ -460,7 +460,7 @@ namespace Newtonsoft.Json.Serialization if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose) TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, type)), null); - writer.WritePropertyName(JsonTypeReflector.IdPropertyName); + writer.WritePropertyName(JsonTypeReflector.IdPropertyName, false); writer.WriteValue(reference); } @@ -471,7 +471,7 @@ namespace Newtonsoft.Json.Serialization if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose) TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing type name '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, typeName, type)), null); - writer.WritePropertyName(JsonTypeReflector.TypePropertyName); + writer.WritePropertyName(JsonTypeReflector.TypePropertyName, false); writer.WriteValue(typeName); } @@ -662,7 +662,7 @@ namespace Newtonsoft.Json.Serialization { WriteTypeProperty(writer, values.GetType()); } - writer.WritePropertyName(JsonTypeReflector.ArrayValuesPropertyName); + writer.WritePropertyName(JsonTypeReflector.ArrayValuesPropertyName, false); } if (contract.ItemContract == null) @@ -671,7 +671,7 @@ namespace Newtonsoft.Json.Serialization return writeMetadataObject; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) #if !(NET20 || NET35) [SecuritySafeCritical] #endif @@ -709,7 +709,7 @@ To fix this error either change the environment to be fully trusted, change the } #endif -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) { OnSerializing(writer, contract, value); @@ -732,7 +732,7 @@ To fix this error either change the environment to be fully trusted, change the if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue)) continue; - writer.WritePropertyName(property.PropertyName); + property.WritePropertyName(writer); SerializeValue(writer, memberValue, memberContract, property, contract, member); } catch (Exception ex) diff --git a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs index fdbe6876..baf6d657 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs @@ -28,7 +28,7 @@ using System.ComponentModel; using System.Globalization; using System.Reflection; using System.Security; -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) using System.Security.Permissions; #endif using Newtonsoft.Json.Utilities; @@ -63,7 +63,7 @@ namespace Newtonsoft.Json.Serialization public const string SpecifiedPostfix = "Specified"; private static readonly ThreadSafeStore JsonConverterTypeCache = new ThreadSafeStore(GetJsonConverterTypeFromAttribute); -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) private static readonly ThreadSafeStore AssociatedMetadataTypesCache = new ThreadSafeStore(GetAssociateMetadataTypeFromAttribute); private const string MetadataTypeAttributeTypeName = @@ -108,7 +108,7 @@ namespace Newtonsoft.Json.Serialization return GetJsonContainerAttribute(type) as JsonDictionaryAttribute; } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40) public static SerializableAttribute GetSerializableAttribute(Type type) { return CachedAttributeGetter.GetAttribute(type); @@ -119,7 +119,18 @@ namespace Newtonsoft.Json.Serialization public static DataContractAttribute GetDataContractAttribute(Type type) { // DataContractAttribute does not have inheritance - return CachedAttributeGetter.GetAttribute(type); + Type currentType = type; + + while (currentType != null) + { + DataContractAttribute result = CachedAttributeGetter.GetAttribute(currentType); + if (result != null) + return result; + + currentType = currentType.BaseType(); + } + + return null; } public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo) @@ -166,7 +177,7 @@ namespace Newtonsoft.Json.Serialization return MemberSerialization.OptIn; #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (!ignoreSerializableAttribute) { SerializableAttribute serializableAttribute = GetSerializableAttribute(objectType); @@ -206,7 +217,7 @@ namespace Newtonsoft.Json.Serialization return null; } -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) public static TypeConverter GetTypeConverter(Type type) { #if !SILVERLIGHT @@ -215,14 +226,14 @@ namespace Newtonsoft.Json.Serialization Type converterType = GetTypeConverterType(type); if (converterType != null) - return (TypeConverter)ReflectionUtils.CreateInstance(converterType); + return (TypeConverter)Activator.CreateInstance(converterType); return null; #endif } #endif -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) private static Type GetAssociatedMetadataType(Type type) { return AssociatedMetadataTypesCache.Get(type); @@ -267,7 +278,7 @@ namespace Newtonsoft.Json.Serialization { T attribute; -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) Type metadataType = GetAssociatedMetadataType(type); if (metadataType != null) { @@ -295,7 +306,7 @@ namespace Newtonsoft.Json.Serialization { T attribute; -#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE40 || PORTABLE) Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType); if (metadataType != null) { @@ -366,7 +377,7 @@ namespace Newtonsoft.Json.Serialization { if (_dynamicCodeGeneration == null) { -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) try { new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); @@ -395,9 +406,9 @@ namespace Newtonsoft.Json.Serialization { if (_fullyTrusted == null) { -#if (NETFX_CORE || SILVERLIGHT || PORTABLE) +#if (NETFX_CORE || SILVERLIGHT || PORTABLE || PORTABLE40) _fullyTrusted = false; -#elif !(NET20 || NET35) +#elif !(NET20 || NET35 || PORTABLE40) AppDomain appDomain = AppDomain.CurrentDomain; _fullyTrusted = appDomain.IsHomogenous && appDomain.IsFullyTrusted; @@ -422,7 +433,7 @@ namespace Newtonsoft.Json.Serialization { get { -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) if (DynamicCodeGeneration) return DynamicReflectionDelegateFactory.Instance; #endif diff --git a/Src/Newtonsoft.Json/SerializationBinder.cs b/Src/Newtonsoft.Json/SerializationBinder.cs index 69307475..864951e6 100644 --- a/Src/Newtonsoft.Json/SerializationBinder.cs +++ b/Src/Newtonsoft.Json/SerializationBinder.cs @@ -1,4 +1,4 @@ -#if SILVERLIGHT || PocketPC || NETFX_CORE || PORTABLE +#if SILVERLIGHT || PocketPC || NETFX_CORE || PORTABLE40 || PORTABLE using System; using System.Reflection; diff --git a/Src/Newtonsoft.Json/TraceLevel.cs b/Src/Newtonsoft.Json/TraceLevel.cs index cef37955..c6106b7d 100644 --- a/Src/Newtonsoft.Json/TraceLevel.cs +++ b/Src/Newtonsoft.Json/TraceLevel.cs @@ -1,4 +1,4 @@ -#if (NETFX_CORE || SILVERLIGHT || PORTABLE) +#if (NETFX_CORE || SILVERLIGHT || PORTABLE40 || PORTABLE) using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs index d71ab96c..ce5dbad6 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs @@ -41,13 +41,6 @@ namespace Newtonsoft.Json.Utilities { internal static class CollectionUtils { - public static IEnumerable CastValid(this IEnumerable enumerable) - { - ValidationUtils.ArgumentNotNull(enumerable, "enumerable"); - - return enumerable.Cast().Where(o => o is T).Cast(); - } - /// /// Determines whether the collection is null or empty. /// @@ -83,20 +76,15 @@ namespace Newtonsoft.Json.Utilities } } - public static void AddRange(this IList initial, IEnumerable collection) +#if (NET20 || NET35 || SILVERLIGHT || PORTABLE40) + public static void AddRange(this IList initial, IEnumerable collection) { ValidationUtils.ArgumentNotNull(initial, "initial"); - ListWrapper wrapper = new ListWrapper(initial); - wrapper.AddRange(collection.Cast()); - } - - public static IList CreateGenericList(Type listType) - { - ValidationUtils.ArgumentNotNull(listType, "listType"); - - return (IList)ReflectionUtils.CreateGeneric(typeof(List<>), listType); + // because earlier versions of .NET didn't support covariant generics + initial.AddRange(collection.Cast()); } +#endif public static bool IsDictionaryType(Type type) { @@ -106,7 +94,7 @@ namespace Newtonsoft.Json.Utilities return true; if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary<,>))) return true; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IReadOnlyDictionary<,>))) return true; #endif @@ -116,7 +104,7 @@ namespace Newtonsoft.Json.Utilities public static ConstructorInfo ResolveEnumableCollectionConstructor(Type collectionType, Type collectionItemType) { - Type genericEnumerable = ReflectionUtils.MakeGenericType(typeof(IEnumerable<>), collectionItemType); + Type genericEnumerable = typeof(IEnumerable<>).MakeGenericType(collectionItemType); foreach (ConstructorInfo constructor in collectionType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)) { @@ -132,73 +120,6 @@ namespace Newtonsoft.Json.Utilities return null; } - public static IWrappedCollection CreateCollectionWrapper(object list) - { - ValidationUtils.ArgumentNotNull(list, "list"); - - Type collectionDefinition; - if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(ICollection<>), out collectionDefinition)) - { - Type collectionItemType = ReflectionUtils.GetCollectionItemType(collectionDefinition); - - // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor - Func, object> instanceCreator = (t, a) => - { - ConstructorInfo c = t.GetConstructor(new[] { collectionDefinition }); - return c.Invoke(new[] { list }); - }; - - return (IWrappedCollection)ReflectionUtils.CreateGeneric(typeof(CollectionWrapper<>), new[] { collectionItemType }, instanceCreator, list); - } - else if (list is IList) - { - return new CollectionWrapper((IList)list); - } - else - { - throw new ArgumentException("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()), "list"); - } - } - - public static IWrappedDictionary CreateDictionaryWrapper(object dictionary) - { - ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); - - Type dictionaryDefinition; - if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary<,>), out dictionaryDefinition)) - { - Type dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition); - Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition); - - // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor - Func, object> instanceCreator = (t, a) => - { - ConstructorInfo c = t.GetConstructor(new[] { dictionaryDefinition }); - return c.Invoke(new[] { dictionary }); - }; - - return (IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper<,>), new[] { dictionaryKeyType, dictionaryValueType }, instanceCreator, dictionary); - } - else if (dictionary is IDictionary) - { - return new DictionaryWrapper((IDictionary)dictionary); - } - else - { - throw new ArgumentException("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()), "dictionary"); - } - } - - public static Array ToArray(Array initial, Type type) - { - if (type == null) - throw new ArgumentNullException("type"); - - Array destinationArray = Array.CreateInstance(type, initial.Length); - Array.Copy(initial, 0, destinationArray, 0, initial.Length); - return destinationArray; - } - public static bool AddDistinct(this IList list, T value) { return list.AddDistinct(value, EqualityComparer.Default); diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index f7d6f903..da127ad1 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -27,7 +27,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.ComponentModel; -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) using System.Numerics; #endif using Newtonsoft.Json.Serialization; @@ -36,7 +36,7 @@ using System.Reflection; using Newtonsoft.Json.Utilities.LinqBridge; #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System.Data.SqlTypes; #endif @@ -139,14 +139,14 @@ namespace Newtonsoft.Json.Utilities { typeof(Guid?), PrimitiveTypeCode.GuidNullable }, { typeof(TimeSpan), PrimitiveTypeCode.TimeSpan }, { typeof(TimeSpan?), PrimitiveTypeCode.TimeSpanNullable }, -#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) { typeof(BigInteger), PrimitiveTypeCode.BigInteger }, { typeof(BigInteger?), PrimitiveTypeCode.BigIntegerNullable }, #endif { typeof(Uri), PrimitiveTypeCode.Uri }, { typeof(string), PrimitiveTypeCode.String }, { typeof(byte[]), PrimitiveTypeCode.Bytes }, -#if !(PORTABLE || NETFX_CORE) +#if !(PORTABLE || PORTABLE40 || NETFX_CORE) { typeof(DBNull), PrimitiveTypeCode.DBNull } #endif }; @@ -189,7 +189,7 @@ namespace Newtonsoft.Json.Utilities Type nonNullable = Nullable.GetUnderlyingType(t); if (nonNullable.IsEnum()) { - Type nullableUnderlyingType = ReflectionUtils.MakeGenericType(typeof (Nullable<>), Enum.GetUnderlyingType(nonNullable)); + Type nullableUnderlyingType = typeof(Nullable<>).MakeGenericType(Enum.GetUnderlyingType(nonNullable)); return GetTypeCode(nullableUnderlyingType); } } @@ -223,7 +223,7 @@ namespace Newtonsoft.Json.Utilities public static TimeSpan ParseTimeSpan(string input) { -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) return TimeSpan.Parse((string) input, CultureInfo.InvariantCulture); #else return TimeSpan.Parse((string)input); @@ -287,7 +287,7 @@ namespace Newtonsoft.Json.Utilities return o => call(null, o); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40) internal static BigInteger ToBigInteger(object value) { if (value is BigInteger) @@ -371,17 +371,14 @@ namespace Newtonsoft.Json.Utilities return System.Convert.ChangeType(initialValue, targetType, culture); } - if (initialValue is string && typeof(Type).IsAssignableFrom(targetType)) - return Type.GetType((string) initialValue, true); - - if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract()) - throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType"); - #if !NET20 if (initialValue is DateTime && targetType == typeof(DateTimeOffset)) return new DateTimeOffset((DateTime)initialValue); #endif + if (initialValue is byte[] && targetType == typeof(Guid)) + return new Guid((byte[]) initialValue); + if (initialValue is string) { if (targetType == typeof (Guid)) @@ -390,16 +387,21 @@ namespace Newtonsoft.Json.Utilities return new Uri((string) initialValue, UriKind.RelativeOrAbsolute); if (targetType == typeof(TimeSpan)) return ParseTimeSpan((string) initialValue); + if (typeof(Type).IsAssignableFrom(targetType)) + return Type.GetType((string) initialValue, true); } -#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) +#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE) if (targetType == typeof(BigInteger)) return ToBigInteger(initialValue); if (initialValue is BigInteger) return FromBigInteger((BigInteger)initialValue, targetType); #endif -#if !(NETFX_CORE || PORTABLE) + if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract()) + throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType"); + +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) // see if source or target types have a TypeConverter that converts between the two TypeConverter toConverter = GetConverter(initialType); @@ -423,7 +425,7 @@ namespace Newtonsoft.Json.Utilities #endif } #endif -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) // handle DBNull and INullable if (initialValue == DBNull.Value) { @@ -433,7 +435,7 @@ namespace Newtonsoft.Json.Utilities throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType)); } #endif -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) if (initialValue is INullable) return EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType); #endif @@ -520,7 +522,7 @@ namespace Newtonsoft.Json.Utilities throw new ArgumentException("Could not cast or convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, (initialType != null) ? initialType.ToString() : "{null}", targetType)); } -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) public static object ToValue(INullable nullableValue) { if (nullableValue == null) @@ -540,7 +542,7 @@ namespace Newtonsoft.Json.Utilities } #endif -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) internal static TypeConverter GetConverter(Type t) { return JsonTypeReflector.GetTypeConverter(t); diff --git a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs index 9330c5bf..4d211d96 100644 --- a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs @@ -31,13 +31,6 @@ namespace Newtonsoft.Json.Utilities { internal static class DateTimeUtils { - public static string GetUtcOffsetText(this DateTime d) - { - TimeSpan utcOffset = d.GetUtcOffset(); - - return utcOffset.Hours.ToString("+00;-00", CultureInfo.InvariantCulture) + ":" + utcOffset.Minutes.ToString("00;00", CultureInfo.InvariantCulture); - } - public static TimeSpan GetUtcOffset(this DateTime d) { #if NET20 @@ -47,7 +40,7 @@ namespace Newtonsoft.Json.Utilities #endif } -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) public static XmlDateTimeSerializationMode ToSerializationMode(DateTimeKind kind) { switch (kind) diff --git a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs index 699b0fba..d8954f82 100644 --- a/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DictionaryWrapper.cs @@ -45,7 +45,7 @@ namespace Newtonsoft.Json.Utilities { private readonly IDictionary _dictionary; private readonly IDictionary _genericDictionary; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) private readonly IReadOnlyDictionary _readOnlyDictionary; #endif private object _syncRoot; @@ -64,7 +64,7 @@ namespace Newtonsoft.Json.Utilities _genericDictionary = dictionary; } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) public DictionaryWrapper(IReadOnlyDictionary dictionary) { ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); @@ -87,7 +87,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.Contains(key); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.ContainsKey(key); #endif @@ -101,7 +101,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.Keys.Cast().ToList(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Keys.ToList(); #endif @@ -124,7 +124,7 @@ namespace Newtonsoft.Json.Utilities return false; } } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) { throw new NotSupportedException(); @@ -151,7 +151,7 @@ namespace Newtonsoft.Json.Utilities return true; } } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) { throw new NotSupportedException(); @@ -169,7 +169,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.Values.Cast().ToList(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Values.ToList(); #endif @@ -184,7 +184,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return (TValue)_dictionary[key]; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary[key]; #endif @@ -195,7 +195,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary[key] = value; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -208,7 +208,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) ((IList)_dictionary).Add(item); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -220,7 +220,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary.Clear(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -232,7 +232,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return ((IList)_dictionary).Contains(item); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Contains(item); #endif @@ -249,7 +249,7 @@ namespace Newtonsoft.Json.Utilities array[arrayIndex++] = new KeyValuePair((TKey)item.Key, (TValue)item.Value); } } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) { throw new NotSupportedException(); @@ -267,7 +267,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.Count; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Count; #endif @@ -282,7 +282,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.IsReadOnly; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return true; #endif @@ -314,7 +314,7 @@ namespace Newtonsoft.Json.Utilities return true; } } -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) { throw new NotSupportedException(); @@ -330,7 +330,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.Cast().Select(de => new KeyValuePair((TKey)de.Key, (TValue)de.Value)).GetEnumerator(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.GetEnumerator(); #endif @@ -347,7 +347,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary.Add(key, value); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -361,7 +361,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary[key]; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary[(TKey)key]; #endif @@ -372,7 +372,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary[key] = value; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -426,7 +426,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary.GetEnumerator(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return new DictionaryEnumerator(_readOnlyDictionary.GetEnumerator()); #endif @@ -438,7 +438,7 @@ namespace Newtonsoft.Json.Utilities { if (_genericDictionary != null) return _genericDictionary.ContainsKey((TKey)key); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.ContainsKey((TKey)key); #endif @@ -452,7 +452,7 @@ namespace Newtonsoft.Json.Utilities { if (_genericDictionary != null) return false; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return true; #endif @@ -467,7 +467,7 @@ namespace Newtonsoft.Json.Utilities { if (_genericDictionary != null) return _genericDictionary.Keys.ToList(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Keys.ToList(); #endif @@ -480,7 +480,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary.Remove(key); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -494,7 +494,7 @@ namespace Newtonsoft.Json.Utilities { if (_genericDictionary != null) return _genericDictionary.Values.ToList(); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary.Values.ToList(); #endif @@ -507,7 +507,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) _dictionary.CopyTo(array, index); -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) throw new NotSupportedException(); #endif @@ -543,7 +543,7 @@ namespace Newtonsoft.Json.Utilities { if (_dictionary != null) return _dictionary; -#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE) +#if !(NET40 || NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE40) else if (_readOnlyDictionary != null) return _readOnlyDictionary; #endif diff --git a/Src/Newtonsoft.Json/Utilities/DynamicProxy.cs b/Src/Newtonsoft.Json/Utilities/DynamicProxy.cs index 4d3e342c..ca049e8a 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicProxy.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicProxy.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; using System.Dynamic; diff --git a/Src/Newtonsoft.Json/Utilities/DynamicProxyMetaObject.cs b/Src/Newtonsoft.Json/Utilities/DynamicProxyMetaObject.cs index d17e19e4..e20d7b6e 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicProxyMetaObject.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicProxyMetaObject.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; using System.Dynamic; diff --git a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs index 8648d834..cf15db57 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE || NETFX_CORE || PORTABLE40) using System; using System.Collections.Generic; #if NET20 diff --git a/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs b/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs index c76a739a..cd1a36dd 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20) +#if !(NET35 || NET20 || PORTABLE40) using System; using System.Collections.Generic; using System.Dynamic; diff --git a/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs b/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs index 9b9c384b..d65edf79 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicWrapper.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) +#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE) using System; using System.Collections.Generic; using System.IO; diff --git a/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs b/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs index b27a28d9..59fb4430 100644 --- a/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs +++ b/Src/Newtonsoft.Json/Utilities/ILGeneratorExtensions.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE || NETFX_CORE) using System; using System.Reflection.Emit; using System.Reflection; diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 4247882f..17333ab9 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -189,11 +189,6 @@ namespace Newtonsoft.Json.Utilities writer.Write(delimiter); } - public static string ToEscapedJavaScriptString(string value) - { - return ToEscapedJavaScriptString(value, '"', true); - } - public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters) { using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(value) ?? 16)) diff --git a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs index 0fd1c3e1..c2ba265e 100644 --- a/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs @@ -57,7 +57,7 @@ namespace Newtonsoft.Json.Utilities ValidationUtils.ArgumentNotNull(type, "type"); if (type.IsValueType()) - return () => (T)ReflectionUtils.CreateInstance(type); + return () => (T)Activator.CreateInstance(type); ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true); diff --git a/Src/Newtonsoft.Json/Utilities/ListWrapper.cs b/Src/Newtonsoft.Json/Utilities/ListWrapper.cs deleted file mode 100644 index b21b81cc..00000000 --- a/Src/Newtonsoft.Json/Utilities/ListWrapper.cs +++ /dev/null @@ -1,189 +0,0 @@ -#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.Collections; -using System.Collections.Generic; - -namespace Newtonsoft.Json.Utilities -{ - internal interface IWrappedList : IList - { - object UnderlyingList { get; } - } - - internal class ListWrapper : CollectionWrapper, IList, IWrappedList - { - private readonly IList _genericList; - - public ListWrapper(IList list) - : base(list) - { - ValidationUtils.ArgumentNotNull(list, "list"); - - if (list is IList) - _genericList = (IList) list; - } - - public ListWrapper(IList list) - : base(list) - { - ValidationUtils.ArgumentNotNull(list, "list"); - - _genericList = list; - } - - public int IndexOf(T item) - { - if (_genericList != null) - return _genericList.IndexOf(item); - else - return ((IList)this).IndexOf(item); - } - - public void Insert(int index, T item) - { - if (_genericList != null) - _genericList.Insert(index, item); - else - ((IList)this).Insert(index, item); - } - - public void RemoveAt(int index) - { - if (_genericList != null) - _genericList.RemoveAt(index); - else - ((IList)this).RemoveAt(index); - } - - public T this[int index] - { - get - { - if (_genericList != null) - return _genericList[index]; - else - return (T)((IList)this)[index]; - } - set - { - if (_genericList != null) - _genericList[index] = value; - else - ((IList)this)[index] = value; - } - } - - public override void Add(T item) - { - if (_genericList != null) - _genericList.Add(item); - else - base.Add(item); - } - - public override void Clear() - { - if (_genericList != null) - _genericList.Clear(); - else - base.Clear(); - } - - public override bool Contains(T item) - { - if (_genericList != null) - return _genericList.Contains(item); - else - return base.Contains(item); - } - - public override void CopyTo(T[] array, int arrayIndex) - { - if (_genericList != null) - _genericList.CopyTo(array, arrayIndex); - else - base.CopyTo(array, arrayIndex); - } - - public override int Count - { - get - { - if (_genericList != null) - return _genericList.Count; - else - return base.Count; - } - } - - public override bool IsReadOnly - { - get - { - if (_genericList != null) - return _genericList.IsReadOnly; - else - return base.IsReadOnly; - } - } - - public override bool Remove(T item) - { - if (_genericList != null) - { - return _genericList.Remove(item); - } - else - { - bool contains = base.Contains(item); - - if (contains) - base.Remove(item); - - return contains; - } - } - - public override IEnumerator GetEnumerator() - { - if (_genericList != null) - return _genericList.GetEnumerator(); - - return base.GetEnumerator(); - } - - public object UnderlyingList - { - get - { - if (_genericList != null) - return _genericList; - else - return UnderlyingCollection; - } - } - } -} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/MathUtils.cs b/Src/Newtonsoft.Json/Utilities/MathUtils.cs index c109de70..2b92368e 100644 --- a/Src/Newtonsoft.Json/Utilities/MathUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/MathUtils.cs @@ -29,7 +29,7 @@ using System.Text; namespace Newtonsoft.Json.Utilities { - internal class MathUtils + internal static class MathUtils { public static int IntLength(int i) { diff --git a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs index dd55f35b..a6fd8fc8 100644 --- a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs @@ -68,22 +68,6 @@ namespace Newtonsoft.Json.Utilities return new ArgumentOutOfRangeException(paramName, newMessage); } - public static bool TryAction(Creator creator, out T output) - { - ValidationUtils.ArgumentNotNull(creator, "creator"); - - try - { - output = creator(); - return true; - } - catch - { - output = default(T); - return false; - } - } - public static string ToString(object value) { if (value == null) @@ -92,48 +76,6 @@ namespace Newtonsoft.Json.Utilities return (value is string) ? @"""" + value.ToString() + @"""" : value.ToString(); } - public static byte[] HexToBytes(string hex) - { - string fixedHex = hex.Replace("-", string.Empty); - - // array to put the result in - byte[] bytes = new byte[fixedHex.Length / 2]; - // variable to determine shift of high/low nibble - int shift = 4; - // offset of the current byte in the array - int offset = 0; - // loop the characters in the string - foreach (char c in fixedHex) - { - // get character code in range 0-9, 17-22 - // the % 32 handles lower case characters - int b = (c - '0') % 32; - // correction for a-f - if (b > 9) b -= 7; - // store nibble (4 bits) in byte array - bytes[offset] |= (byte)(b << shift); - // toggle the shift variable between 0 and 4 - shift ^= 4; - // move to next byte - if (shift != 0) offset++; - } - return bytes; - } - - public static string BytesToHex(byte[] bytes) - { - return BytesToHex(bytes, false); - } - - public static string BytesToHex(byte[] bytes, bool removeDashes) - { - string hex = BitConverter.ToString(bytes); - if (removeDashes) - hex = hex.Replace("-", ""); - - return hex; - } - public static int ByteArrayCompare(byte[] a1, byte[] a2) { int lengthCompare = a1.Length.CompareTo(a2.Length); diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index e1f75746..47823fb1 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -25,7 +25,7 @@ using System; using System.Collections.Generic; -#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) using System.Numerics; #endif using System.Reflection; @@ -43,7 +43,7 @@ using Newtonsoft.Json.Serialization; namespace Newtonsoft.Json.Utilities { -#if NETFX_CORE || PORTABLE +#if (NETFX_CORE || PORTABLE || PORTABLE40) internal enum MemberTypes { Property, @@ -52,7 +52,9 @@ namespace Newtonsoft.Json.Utilities Method, Other } +#endif +#if NETFX_CORE || PORTABLE [Flags] internal enum BindingFlags { @@ -85,7 +87,7 @@ namespace Newtonsoft.Json.Utilities static ReflectionUtils() { -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE40 || PORTABLE) EmptyTypes = Type.EmptyTypes; #else EmptyTypes = new Type[0]; @@ -107,16 +109,36 @@ namespace Newtonsoft.Json.Utilities return false; } + public static MethodInfo GetBaseDefinition(this PropertyInfo propertyInfo) + { + ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo"); + + MethodInfo m = propertyInfo.GetGetMethod(); + if (m != null) + return m.GetBaseDefinition(); + + m = propertyInfo.GetSetMethod(); + if (m != null) + return m.GetBaseDefinition(); + + return null; + } + + public static bool IsPublic(PropertyInfo property) + { + if (property.GetGetMethod() != null && property.GetGetMethod().IsPublic) + return true; + if (property.GetSetMethod() != null && property.GetSetMethod().IsPublic) + return true; + + return false; + } + public static Type GetObjectType(object v) { return (v != null) ? v.GetType() : null; } - public static string GetTypeName(Type t, FormatterAssemblyStyle assemblyFormat) - { - return GetTypeName(t, assemblyFormat, null); - } - public static string GetTypeName(Type t, FormatterAssemblyStyle assemblyFormat, SerializationBinder binder) { string fullyQualifiedTypeName; @@ -189,24 +211,6 @@ namespace Newtonsoft.Json.Utilities return builder.ToString(); } - public static bool IsInstantiatableType(Type t) - { - ValidationUtils.ArgumentNotNull(t, "t"); - - if (t.IsAbstract() || t.IsInterface() || t.IsArray || t.IsGenericTypeDefinition() || t == typeof(void)) - return false; - - if (!HasDefaultConstructor(t)) - return false; - - return true; - } - - public static bool HasDefaultConstructor(Type t) - { - return HasDefaultConstructor(t, false); - } - public static bool HasDefaultConstructor(Type t, bool nonPublic) { ValidationUtils.ArgumentNotNull(t, "t"); @@ -408,24 +412,6 @@ namespace Newtonsoft.Json.Utilities } } - public static Type GetDictionaryValueType(Type dictionaryType) - { - Type keyType; - Type valueType; - GetDictionaryKeyValueTypes(dictionaryType, out keyType, out valueType); - - return valueType; - } - - public static Type GetDictionaryKeyType(Type dictionaryType) - { - Type keyType; - Type valueType; - GetDictionaryKeyValueTypes(dictionaryType, out keyType, out valueType); - - return keyType; - } - /// /// Gets the member's underlying type. /// @@ -690,13 +676,18 @@ namespace Newtonsoft.Json.Utilities if (provider is MemberInfo) return (T[])Attribute.GetCustomAttributes((MemberInfo)provider, typeof(T), inherit); +#if !PORTABLE40 if (provider is Module) return (T[])Attribute.GetCustomAttributes((Module)provider, typeof(T), inherit); +#endif if (provider is ParameterInfo) return (T[])Attribute.GetCustomAttributes((ParameterInfo)provider, typeof(T), inherit); +#if !PORTABLE40 return (T[])((ICustomAttributeProvider)attributeProvider).GetCustomAttributes(typeof(T), inherit); +#endif + throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); } #else public static T[] GetAttributes(object provider, bool inherit) where T : Attribute @@ -720,43 +711,6 @@ namespace Newtonsoft.Json.Utilities } #endif - public static Type MakeGenericType(Type genericTypeDefinition, params Type[] innerTypes) - { - ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition"); - ValidationUtils.ArgumentNotNullOrEmpty(innerTypes, "innerTypes"); - ValidationUtils.ArgumentConditionTrue(genericTypeDefinition.IsGenericTypeDefinition(), "genericTypeDefinition", "Type {0} is not a generic type definition.".FormatWith(CultureInfo.InvariantCulture, genericTypeDefinition)); - - return genericTypeDefinition.MakeGenericType(innerTypes); - } - - public static object CreateGeneric(Type genericTypeDefinition, Type innerType, params object[] args) - { - return CreateGeneric(genericTypeDefinition, new[] { innerType }, args); - } - - public static object CreateGeneric(Type genericTypeDefinition, IList innerTypes, params object[] args) - { - return CreateGeneric(genericTypeDefinition, innerTypes, (t, a) => CreateInstance(t, a.ToArray()), args); - } - - public static object CreateGeneric(Type genericTypeDefinition, IList innerTypes, Func, object> instanceCreator, params object[] args) - { - ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition"); - ValidationUtils.ArgumentNotNullOrEmpty(innerTypes, "innerTypes"); - ValidationUtils.ArgumentNotNull(instanceCreator, "createInstance"); - - Type specificType = MakeGenericType(genericTypeDefinition, innerTypes.ToArray()); - - return instanceCreator(specificType, args); - } - - public static object CreateInstance(Type type, params object[] args) - { - ValidationUtils.ArgumentNotNull(type, "type"); - - return Activator.CreateInstance(type, args); - } - public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName) { int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName); @@ -883,30 +837,51 @@ namespace Newtonsoft.Json.Utilities { // fix weirdness with private PropertyInfos only being returned for the current Type // find base type properties and add them to result - if ((bindingAttr & BindingFlags.NonPublic) != 0) + + // also find base properties that have been hidden by subtype properties with the same name + + while ((targetType = targetType.BaseType()) != null) { - // modify flags to not search for public fields - BindingFlags nonPublicBindingAttr = bindingAttr.RemoveFlag(BindingFlags.Public); - - while ((targetType = targetType.BaseType()) != null) + foreach (PropertyInfo propertyInfo in targetType.GetProperties(bindingAttr)) { - foreach (PropertyInfo propertyInfo in targetType.GetProperties(nonPublicBindingAttr)) - { - PropertyInfo nonPublicProperty = propertyInfo; + PropertyInfo subTypeProperty = propertyInfo; + if (!IsPublic(subTypeProperty)) + { // have to test on name rather than reference because instances are different // depending on the type that GetProperties was called on - int index = initialProperties.IndexOf(p => p.Name == nonPublicProperty.Name); + int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name); if (index == -1) { - initialProperties.Add(nonPublicProperty); + initialProperties.Add(subTypeProperty); } else { // replace nonpublic properties for a child, but gotten from // the parent with the one from the child // the property gotten from the child will have access to private getter/setter - initialProperties[index] = nonPublicProperty; + initialProperties[index] = subTypeProperty; + } + } + else + { + if (!subTypeProperty.IsVirtual()) + { + int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name + && p.DeclaringType == subTypeProperty.DeclaringType); + + if (index == -1) + initialProperties.Add(subTypeProperty); + } + else + { + int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name + && p.IsVirtual() + && p.GetBaseDefinition() != null + && p.GetBaseDefinition().DeclaringType.IsAssignableFrom(subTypeProperty.DeclaringType)); + + if (index == -1) + initialProperties.Add(subTypeProperty); } } } @@ -954,7 +929,7 @@ namespace Newtonsoft.Json.Utilities return 0m; case PrimitiveTypeCode.DateTime: return new DateTime(); -#if !(PORTABLE || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) +#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT) case PrimitiveTypeCode.BigInteger: return new BigInteger(); #endif diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index 4f89601a..974afdae 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -182,7 +182,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsHighSurrogate(char c) { -#if !(SILVERLIGHT || PORTABLE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE) return char.IsHighSurrogate(c); #else return (c >= 55296 && c <= 56319); @@ -191,7 +191,7 @@ namespace Newtonsoft.Json.Utilities public static bool IsLowSurrogate(char c) { -#if !(SILVERLIGHT || PORTABLE) +#if !(SILVERLIGHT || PORTABLE40 || PORTABLE) return char.IsLowSurrogate(c); #else return (c >= 56320 && c <= 57343); diff --git a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs index 1f79e967..b88b540c 100644 --- a/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs +++ b/Src/Newtonsoft.Json/Utilities/TypeExtensions.cs @@ -89,7 +89,7 @@ namespace Newtonsoft.Json.Utilities public static MemberTypes MemberType(this MemberInfo memberInfo) { -#if !(NETFX_CORE || PORTABLE) +#if !(NETFX_CORE || PORTABLE || PORTABLE40) return memberInfo.MemberType; #else if (memberInfo is PropertyInfo) @@ -177,6 +177,41 @@ namespace Newtonsoft.Json.Utilities #endif } +#if PORTABLE40 + public static PropertyInfo GetProperty(this Type type, string name, BindingFlags bindingFlags, object placeholder1, Type propertyType, IList indexParameters, object placeholder2) + { + IList propertyInfos = type.GetProperties(bindingFlags); + + return propertyInfos.Where(p => + { + if (name != null && name != p.Name) + return false; + if (propertyType != null && propertyType != p.PropertyType) + return false; + if (indexParameters != null) + { + if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters)) + return false; + } + + return true; + }).SingleOrDefault(); + } + + public static IEnumerable GetMember(this Type type, string name, MemberTypes memberType, BindingFlags bindingFlags) + { + return type.GetMembers(bindingFlags).Where(m => + { + if (name != null && name != m.Name) + return false; + if (m.MemberType() != memberType) + return false; + + return true; + }); + } +#endif + #if (NETFX_CORE || PORTABLE) public static MethodInfo GetBaseDefinition(this MethodInfo method) { diff --git a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs index 3f01824c..9b187af9 100644 --- a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs @@ -50,30 +50,10 @@ namespace Newtonsoft.Json.Utilities throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName); } - public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName) - { - ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName)); - } - - public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName, string message) - { - if (collection == null) - throw new ArgumentNullException(parameterName); - - if (collection.Count == 0) - throw new ArgumentException(message, parameterName); - } - public static void ArgumentNotNull(object value, string parameterName) { if (value == null) throw new ArgumentNullException(parameterName); } - - public static void ArgumentConditionTrue(bool condition, string parameterName, string message) - { - if (!condition) - throw new ArgumentException(message, parameterName); - } } } \ No newline at end of file