![](/img/trans.png)
[英]JSON Newtonsoft C# Good Practice for Serialize/ Deserialize Lists of Objects
[英]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]並從GetRawReader
和GetRawWriter
方法中GetRawWriter
。
您的字典可以視為IEnumerable<KeyValuePair<string, SomBigObject>>
。 為什么不把它分成 100K 行的塊並一次存儲一個塊?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.