简体   繁体   English

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

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

I have my object as below我有我的对象如下

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
}

I am trying to serialize this object using newtonsoft json library in C# using below settings.我正在尝试使用以下设置在 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);

But it gives me Out of memory exception.但它给了我内存不足的异常。 If i reduce number of rows to small number like 100k rows then it works fine.如果我将行数减少到像 100k 行这样的小数,那么它工作正常。

When it has given out of memory exception, my computer has still 5 GB RAM available.当它出现内存不足异常时,我的计算机仍有 5 GB RAM 可用。

After serializing this object, i want to store it in Distributed Cache (Apache Ignite).序列化此对象后,我想将其存储在分布式缓存(Apache Ignite)中。

Please let me know what alternatives i can try to resolve this issue.请让我知道我可以尝试解决此问题的替代方法。

Basically it's not quite clear what are you trying to achieve with the Ignite, but here are my observations.基本上不清楚你想用 Ignite 实现什么,但这是我的观察。 You don't need to serialize your data manually.您不需要手动序列化您的数据。 Ignite is quite smart to perform those things for you. Ignite 非常聪明地为您执行这些操作。 Moreover, it's counterproductive to save the data as a single batch of strings.此外,将数据保存为单批字符串会适得其反。

You could try to use the data colocation and have two caches - one for the CustomizedObject and the second with your SomBigObject [1] and fill the latter using a DataStreamer [2] .您可以尝试使用数据托管并有两个缓存 - 一个用于CustomizedObject ,第二个用于您的SomBigObject [1]并使用 DataStreamer [2]填充后者。 Regarding the serialization, Ignite stores all data using its own binary format [3] in order to provide required APIs, such as SQL for example.关于序列化,Ignite 使用自己的二进制格式[3]存储所有数据,以提供所需的 API,例如 SQL。 For a large custom object, it's a common practice to implement the IBInarizable interface [4] and get benefits out of the GetRawReader and GetRawWriter methods.对于大型自定义对象,通常的做法是实现IBInarizable接口[4]并从GetRawReaderGetRawWriter方法中GetRawWriter

Your dictionary can be viewed as an IEnumerable<KeyValuePair<string, SomBigObject>> .您的字典可以视为IEnumerable<KeyValuePair<string, SomBigObject>> Why don't you break that up into chunks of 100K rows and store them one chunk at a time?为什么不把它分成 100K 行的块并一次存储一个块?

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

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