-Fixed serializing ConcurrentDictionary in .NET Core 2.0
-Fixed serializing ConcurrentStack/Queue/Bag
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
$signAssemblies = $false
|
$signAssemblies = $false
|
||||||
$signKeyPath = "C:\Development\Releases\newtonsoft.snk"
|
$signKeyPath = "C:\Development\Releases\newtonsoft.snk"
|
||||||
$buildDocumentation = $false
|
$buildDocumentation = $false
|
||||||
$buildNuGet = $true
|
$buildNuGet = $false
|
||||||
$treatWarningsAsErrors = $false
|
$treatWarningsAsErrors = $false
|
||||||
$workingName = if ($workingName) {$workingName} else {"Working"}
|
$workingName = if ($workingName) {$workingName} else {"Working"}
|
||||||
$netCliVersion = "1.0.0"
|
$netCliVersion = "1.0.0"
|
||||||
|
|||||||
@@ -25,11 +25,9 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
#if !(NET35 || NET20 || PORTABLE || PORTABLE40)
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
#endif
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
@@ -63,6 +61,53 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonSerializerCollectionsTests : TestFixtureBase
|
public class JsonSerializerCollectionsTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
#if !(NET20 || NET35)
|
||||||
|
[Test]
|
||||||
|
public void SerializeConcurrentQueue()
|
||||||
|
{
|
||||||
|
ConcurrentQueue<int> queue1 = new ConcurrentQueue<int>();
|
||||||
|
queue1.Enqueue(1);
|
||||||
|
|
||||||
|
string output = JsonConvert.SerializeObject(queue1);
|
||||||
|
Assert.AreEqual(@"[1]", output);
|
||||||
|
|
||||||
|
ConcurrentQueue<int> queue2 = JsonConvert.DeserializeObject<ConcurrentQueue<int>>(output);
|
||||||
|
int i;
|
||||||
|
Assert.IsTrue(queue2.TryDequeue(out i));
|
||||||
|
Assert.AreEqual(1, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeConcurrentBag()
|
||||||
|
{
|
||||||
|
ConcurrentBag<int> bag1 = new ConcurrentBag<int>();
|
||||||
|
bag1.Add(1);
|
||||||
|
|
||||||
|
string output = JsonConvert.SerializeObject(bag1);
|
||||||
|
Assert.AreEqual(@"[1]", output);
|
||||||
|
|
||||||
|
ConcurrentBag<int> bag2 = JsonConvert.DeserializeObject<ConcurrentBag<int>>(output);
|
||||||
|
int i;
|
||||||
|
Assert.IsTrue(bag2.TryTake(out i));
|
||||||
|
Assert.AreEqual(1, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeConcurrentStack()
|
||||||
|
{
|
||||||
|
ConcurrentStack<int> stack1 = new ConcurrentStack<int>();
|
||||||
|
stack1.Push(1);
|
||||||
|
|
||||||
|
string output = JsonConvert.SerializeObject(stack1);
|
||||||
|
Assert.AreEqual(@"[1]", output);
|
||||||
|
|
||||||
|
ConcurrentStack<int> stack2 = JsonConvert.DeserializeObject<ConcurrentStack<int>>(output);
|
||||||
|
int i;
|
||||||
|
Assert.IsTrue(stack2.TryPop(out i));
|
||||||
|
Assert.AreEqual(1, i);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void DoubleKey_WholeValue()
|
public void DoubleKey_WholeValue()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,9 +26,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using Newtonsoft.Json.Schema;
|
using Newtonsoft.Json.Schema;
|
||||||
#if HAVE_CONCURRENT_COLLECTIONS
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
#endif
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
#if HAVE_DYNAMIC
|
#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)
|
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
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#if HAVE_FSHARP_TYPES
|
#if HAVE_FSHARP_TYPES
|
||||||
if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName)
|
if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName)
|
||||||
{
|
{
|
||||||
@@ -905,14 +920,13 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
private static bool ShouldSkipSerializing(Type t)
|
private static bool ShouldSkipSerializing(Type t)
|
||||||
{
|
{
|
||||||
#if HAVE_FSHARP_TYPES
|
if (IsConcurrentCollection(t))
|
||||||
if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#if DOTNET
|
#if HAVE_FSHARP_TYPES
|
||||||
if (t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>))
|
if (t.Name == FSharpUtils.FSharpSetTypeName || t.Name == FSharpUtils.FSharpMapTypeName)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user