diff --git a/Build/build.ps1 b/Build/build.ps1 index 636874bc..9f4273dc 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -8,7 +8,7 @@ $signAssemblies = $false $signKeyPath = "C:\Development\Releases\newtonsoft.snk" $buildDocumentation = $false - $buildNuGet = $true + $buildNuGet = $false $treatWarningsAsErrors = $false $workingName = if ($workingName) {$workingName} else {"Working"} $netCliVersion = "1.0.0" diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs index 6c54d628..178299fa 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs @@ -25,11 +25,9 @@ using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Specialized; using System.Runtime.Serialization; -#if !(NET35 || NET20 || PORTABLE || PORTABLE40) -using System.Collections.Concurrent; -#endif using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; @@ -63,6 +61,53 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class JsonSerializerCollectionsTests : TestFixtureBase { +#if !(NET20 || NET35) + [Test] + public void SerializeConcurrentQueue() + { + ConcurrentQueue queue1 = new ConcurrentQueue(); + queue1.Enqueue(1); + + string output = JsonConvert.SerializeObject(queue1); + Assert.AreEqual(@"[1]", output); + + ConcurrentQueue queue2 = JsonConvert.DeserializeObject>(output); + int i; + Assert.IsTrue(queue2.TryDequeue(out i)); + Assert.AreEqual(1, i); + } + + [Test] + public void SerializeConcurrentBag() + { + ConcurrentBag bag1 = new ConcurrentBag(); + bag1.Add(1); + + string output = JsonConvert.SerializeObject(bag1); + Assert.AreEqual(@"[1]", output); + + ConcurrentBag bag2 = JsonConvert.DeserializeObject>(output); + int i; + Assert.IsTrue(bag2.TryTake(out i)); + Assert.AreEqual(1, i); + } + + [Test] + public void SerializeConcurrentStack() + { + ConcurrentStack stack1 = new ConcurrentStack(); + stack1.Push(1); + + string output = JsonConvert.SerializeObject(stack1); + Assert.AreEqual(@"[1]", output); + + ConcurrentStack stack2 = JsonConvert.DeserializeObject>(output); + int i; + Assert.IsTrue(stack2.TryPop(out i)); + Assert.AreEqual(1, i); + } +#endif + [Test] public void DoubleKey_WholeValue() { diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 0fee0df3..d5e840f3 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -26,9 +26,6 @@ using System; using System.Collections; using Newtonsoft.Json.Schema; -#if HAVE_CONCURRENT_COLLECTIONS -using System.Collections.Concurrent; -#endif using System.Collections.Generic; using System.ComponentModel; #if HAVE_DYNAMIC @@ -884,15 +881,33 @@ namespace Newtonsoft.Json.Serialization } } + private static bool IsConcurrentCollection(Type t) + { + if (t.IsGenericType()) + { + Type definition = t.GetGenericTypeDefinition(); + + switch (definition.FullName) + { + case "System.Collections.Concurrent.ConcurrentQueue`1": + case "System.Collections.Concurrent.ConcurrentStack`1": + case "System.Collections.Concurrent.ConcurrentBag`1": + case "System.Collections.Concurrent.ConcurrentDictionary`2": + return true; + } + } + + return false; + } + private static bool ShouldSkipDeserialized(Type t) { -#if HAVE_CONCURRENT_COLLECTIONS // ConcurrentDictionary throws an error in its OnDeserialized so ignore - http://json.codeplex.com/discussions/257093 - if (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>)) + if (IsConcurrentCollection(t)) { return true; } -#endif + #if HAVE_FSHARP_TYPES if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName) { @@ -905,14 +920,13 @@ namespace Newtonsoft.Json.Serialization private static bool ShouldSkipSerializing(Type t) { -#if HAVE_FSHARP_TYPES - if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName) + if (IsConcurrentCollection(t)) { return true; } -#endif -#if DOTNET - if (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>)) + +#if HAVE_FSHARP_TYPES + if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName) { return true; }