Json.net使用组合中的并发集合反序列化复杂对象

时间:2017-03-16 14:18:01

标签: c# json serialization json.net

我有一个这样的课程:

public class ComplexClass
{
    public ConcurrentBag<SimpleClass> _simpleClassObjects;
}

当我序列化这个类时,它可以工作。但是当我尝试反序列化时

public static ComplexClass LoadComplexClass()
    {
        ComplexClass persistedComplexClass;
        using (var stream = new StreamReader(File.Open(jsonFilePath, FileMode.Open)))
        {
            persistedComplexClass = (ComplexClass) JsonSerializer.Create().Deserialize(stream, typeof(ComplexClass));
        }
        return persistedComplexClass;
    }

它抛出异常:

  

Newtonsoft.Json.dll中发生未处理的“System.InvalidCastException”类型异常

     

其他信息:无法将类型为'System.Collections.Concurrent.ConcurrentBag`1 [LabML.Model.Point]'的对象强制转换为'System.Collections.Generic.ICollection`1 [LabML.Model.Point]'

此异常的根本原因是ConcurrentBag<T>不实现通用ICollection<T>,只实现非通用ICollection

如何使用Json.Net解决此问题? (我已经搜索了一段时间,但只有我发现的是将ICollection<T>映射到ConcurrentCollection而不是复杂类。

1 个答案:

答案 0 :(得分:2)

<强>更新

从版本10.0.3开始,Json.NET声称正确序列化ConcurrentBag<T>。根据{{​​3}}:

  • 修复 - 修正了序列化ConcurrentStack / Queue / Bag

原始答案

正如你猜测的那样,问题是release notes实现了ICollectionIEnumerable<T>而不是ICollection<T>,所以Json.NET不知道如何向它添加项目和处理它是一个只读集合。虽然ConcurrentBag<T>确实有ConcurrentBag<T>,但Json.NET不会使用该构造函数,因为它在内部也有parameterized constructor taking an input collection[OnSerializing]回调。当这些回调存在时,Json.NET将不使用参数化构造函数,而是抛出异常

Cannot call OnSerializing on an array or readonly list, or list created from a non-default constructor: System.Collections.Concurrent.ConcurrentBag`1[]

因此,有必要为ConcurrentBag<T>

创建[OnDeserialized]
public class ConcurrentBagConverter : ConcurrentBagConverterBase
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.GetConcurrentBagItemType() != null;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        try
        {
            var itemType = objectType.GetConcurrentBagItemType();
            var method = GetType().GetMethod("ReadJsonGeneric", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
            var genericMethod = method.MakeGenericMethod(new[] { objectType, itemType });
            return genericMethod.Invoke(this, new object[] { reader, objectType, itemType, existingValue, serializer });
        }
        catch (TargetInvocationException ex)
        {
            // Wrap the TargetInvocationException in a JsonSerializationException
            throw new JsonSerializationException("Failed to deserialize " + objectType, ex);
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var objectType = value.GetType();
        try
        {
            var itemType = objectType.GetConcurrentBagItemType();
            var method = GetType().GetMethod("WriteJsonGeneric", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
            var genericMethod = method.MakeGenericMethod(new[] { objectType, itemType });
            genericMethod.Invoke(this, new object[] { writer, value, serializer });
        }
        catch (TargetInvocationException ex)
        {
            // Wrap the TargetInvocationException in a JsonSerializationException
            throw new JsonSerializationException("Failed to serialize " + objectType, ex);
        }
    }
}

public class ConcurrentBagConverter<TItem> : ConcurrentBagConverterBase
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(ConcurrentBagConverter<TItem>).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return ReadJsonGeneric<ConcurrentBag<TItem>, TItem>(reader, objectType, typeof(TItem), existingValue, serializer);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        WriteJsonGeneric<ConcurrentBag<TItem>, TItem>(writer, value, serializer);
    }
}

// https://stackoverflow.com/questions/42836648/json-net-deserialize-complex-object-with-concurrent-collection-in-composition
public abstract class ConcurrentBagConverterBase : JsonConverter
{
    protected TConcurrentBag ReadJsonGeneric<TConcurrentBag, TItem>(JsonReader reader, Type collectionType, Type itemType, object existingValue, JsonSerializer serializer)
        where TConcurrentBag : ConcurrentBag<TItem>
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        if (reader.TokenType != JsonToken.StartArray)
            throw new JsonSerializationException(string.Format("Expected {0}, encountered {1} at path {2}", JsonToken.StartArray, reader.TokenType, reader.Path));
        var collection = existingValue as TConcurrentBag ?? (TConcurrentBag)serializer.ContractResolver.ResolveContract(collectionType).DefaultCreator();
        while (reader.Read())
        {
            switch (reader.TokenType)
            {
                case JsonToken.Comment:
                    break;
                case JsonToken.EndArray:
                    return collection;
                default:
                    collection.Add((TItem)serializer.Deserialize(reader, itemType));
                    break;
            }
        }
        // Should not come here.
        throw new JsonSerializationException("Unclosed array at path: " + reader.Path);
    }

    protected void WriteJsonGeneric<TConcurrentBag, TItem>(JsonWriter writer, object value, JsonSerializer serializer)
        where TConcurrentBag : ConcurrentBag<TItem>
    {
        // Snapshot the bag as an array and serialize the array.
        var array = ((TConcurrentBag)value).ToArray();
        serializer.Serialize(writer, array);
    }
}

internal static class TypeExtensions
{
    public static Type GetConcurrentBagItemType(this Type objectType)
    {
        while (objectType != null)
        {
            if (objectType.IsGenericType
                && objectType.GetGenericTypeDefinition() == typeof(ConcurrentBag<>))
            {
                return objectType.GetGenericArguments()[0];
            }
            objectType = objectType.BaseType;
        }
        return null;
    }
}

public class ConcurrentBagContractResolver : DefaultContractResolver
{
    protected override JsonArrayContract CreateArrayContract(Type objectType)
    {
        var contract = base.CreateArrayContract(objectType);

        var concurrentItemType = objectType.GetConcurrentBagItemType();
        if (concurrentItemType != null)
        {
            if (contract.Converter == null)
                contract.Converter = (JsonConverter)Activator.CreateInstance(typeof(ConcurrentBagConverter<>).MakeGenericType(new[] { concurrentItemType }));
        }

        return contract;
    }
}

然后,将通用版本应用于您的特定字段,如下所示:

public class ComplexClass
{
    [JsonConverter(typeof(ConcurrentBagConverter<SimpleClass>))]
    public ConcurrentBag<SimpleClass> _simpleClassObjects;
}

或者,使用以下设置为所有ConcurrentBag<T>全局T应用通用版本:

var settings = new JsonSerializerSettings
{
    Converters = { new ConcurrentBagConverter() },
};

或者可以使用自定义合约解析器,其性能可能比使用通用转换器稍好一些:

var settings = new JsonSerializerSettings
{
    ContractResolver = new ConcurrentBagContractResolver(),
};

示例custom JsonConverter

话虽如此,上述内容仅在ConcurrentBag<T>属性或字段为读/写时才有效。如果该成员是只读的,那么我发现Json.NET 9.0.1将跳过反序列化,即使存在转换器,因为它推断出集合成员和内容都是只读的。 (这可能是fiddle中的错误。)

作为一种解决方法,您可以将该属性设为可私人设置,并使用[JsonProperty]标记:

public class ComplexClass
{
    ConcurrentBag<SimpleClass> m_simpleClassObjects = new ConcurrentBag<SimpleClass>();

    [JsonConverter(typeof(ConcurrentBagConverter<SimpleClass>))]
    [JsonProperty]
    public ConcurrentBag<SimpleClass> _simpleClassObjects { get { return m_simpleClassObjects; } private set { m_simpleClassObjects = value; } }
}

或者使用代理数组属性,从而无需使用任何类型的转换器:

public class ComplexClass
{
    readonly ConcurrentBag<SimpleClass> m_simpleClassObjects = new ConcurrentBag<SimpleClass>();

    [JsonIgnore]
    public ConcurrentBag<SimpleClass> _simpleClassObjects { get { return m_simpleClassObjects; } }

    [JsonProperty("_simpleClassObjects")]
    SimpleClass[] _simpleClassObjectsArray
    {
        get
        {
            return _simpleClassObjects.ToArray();
        }
        set
        {
            if (value == null)
                return;
            foreach (var item in value)
                _simpleClassObjects.Add(item);
        }
    }
}