繁体   English   中英

使用 newtonsoft json C# 序列化大对象列表

[英]Serialize big list of objects using newtonsoft json C#

我有我的对象如下

public class CustomizedObject
{
    public int KeyId { get; set; }
    public string SomeName { get; set; }
    public DateTime StartDate { get; set; }
    public bool isPossible { get; set; }

    //Below dictionary is hug contains more than 300k rows (300*1000) 
    public Dictionary<string, SomBigObject> BigObjectMap { get; set; }

}

public class SomBigObject
{
    //This object has so many properties of all type (int, float, datetime, enum, double, bool) 
    //+ another customized user object Lists
}

我正在尝试使用以下设置在 C# 中使用 newtonsoft json 库序列化此对象。

var settings = new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                NullValueHandling = NullValueHandling.Ignore,
                ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                Formatting = Formatting.Indented
            };
var json = JsonConvert.SerializeObject(value, settings);

但它给了我内存不足的异常。 如果我将行数减少到像 100k 行这样的小数,那么它工作正常。

当它出现内存不足异常时,我的计算机仍有 5 GB RAM 可用。

序列化此对象后,我想将其存储在分布式缓存(Apache Ignite)中。

请让我知道我可以尝试解决此问题的替代方法。

基本上不清楚你想用 Ignite 实现什么,但这是我的观察。 您不需要手动序列化您的数据。 Ignite 非常聪明地为您执行这些操作。 此外,将数据保存为单批字符串会适得其反。

您可以尝试使用数据托管并有两个缓存 - 一个用于CustomizedObject ,第二个用于您的SomBigObject [1]并使用 DataStreamer [2]填充后者。 关于序列化,Ignite 使用自己的二进制格式[3]存储所有数据,以提供所需的 API,例如 SQL。 对于大型自定义对象,通常的做法是实现IBInarizable接口[4]并从GetRawReaderGetRawWriter方法中GetRawWriter

您的字典可以视为IEnumerable<KeyValuePair<string, SomBigObject>> 为什么不把它分成 100K 行的块并一次存储一个块?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM