-Fixed serializing ConcurrentDictionary in .NET Core 2.0
-Fixed serializing ConcurrentStack/Queue/Bag
This commit is contained in:
@@ -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<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]
|
||||
public void DoubleKey_WholeValue()
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user