SerializationException when serializing lots of objects in .NET

后端 未结 9 977
深忆病人
深忆病人 2020-12-10 01:41

I\'m running into problems serializing lots of objects in .NET. The object graph is pretty big with some of the new data sets being used, so I\'m getting:

Sy         


        
9条回答
  •  余生分开走
    2020-12-10 02:32

    Binary serilization of very large objects

    If you run into this limitation binaryformatter the internal array cannot expand to greater than int32.maxvalue elements using BinaryFormater, help yourself with this code snippet Step 1

    Install nuget package: Install-Package Newtonsoft.Json.Bson -Version 1.0.2

    using Newtonsoft.Json.Bson; //import require namesapace
    
    //Code snippet for serialization/deserialization
    
    public byte[] Serialize(T obj)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var writer = new BsonDataWriter(memoryStream))
            {
                var serializer = new JsonSerializer();
                serializer.Serialize(writer, obj);
            }
            return memoryStream.ToArray();
        }
    }
    
    public T Deserialize(byte[] data)
    {
        using (var memoryStream = new MemoryStream(data))
        {
            using (var reader = new BsonDataReader(memoryStream))
            {
                var serializer = new JsonSerializer();
                return serializer.Deserialize(reader);
            }
        }
    }
    

提交回复
热议问题