免責聲明:我經歷了本文提供的大多數解決方案,但其中大部分都是在反序列化時討論OOM異常。使用Json.Net序列化對象會導致內存不足異常
我想使用Json.Net將對象(它是一棵樹)序列化爲Json。一切工作正常的小對象,但我得到OOM異常,當我嘗試與大對象。由於它與相同數據類型的較小對象一起工作,我假定沒有循環引用(我檢查過它的數據結構)。 有沒有一種方法可以將我的對象轉換爲流(這是一個Windows應用商店應用)並使用該流生成Json?
public static async Task<bool> SerializeIntoJson<T>(string fileName, StorageFolder destinationFolder, Content content)
{
ITraceWriter traceWriter = new MemoryTraceWriter();
try
{
string jsonString = JsonConvert.SerializeObject(content, Formatting.Indented, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.All,
Error = ReportJsonErrors,
TraceWriter = traceWriter,
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
});
System.Diagnostics.Debug.WriteLine(traceWriter);
StorageFile file = await destinationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(file, jsonString);
return true;
}
catch (NullReferenceException nullException)
{
System.Diagnostics.Debug.WriteLine(traceWriter);
logger.LogError("Exception happened while serializing input object, Error: " + nullException.Message);
return false;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(traceWriter);
logger.LogError("Exception happened while serializing input object, Error: " + e.Message, e.ToString());
return false;
}
}
爲了我的對象轉換成流,我發現代碼中使用的BinaryFormatter這是不是在Windows商店應用的DLL可用。
嘗試使用'JsonSerializer'以'StreamReader'結合所概述[這裏](HTTP:/ /stackoverflow.com/questions/8157636/can-json-net-serialize-deserialize-to-from-a-stream)。 – 2014-09-30 19:18:32
我確實看了看代碼,並將我帶入了一個問題:有沒有辦法將我的對象轉換爲流(這是Windows應用商店應用)? – 2014-09-30 19:35:38
這就是串行器的功能。它採用所有的屬性並將它們轉換爲字符串。在這種情況下,不用返回字符串,它可以將它們直接存入流(例如文件流),以便它們不在內存中。 – 2014-09-30 19:42:11