我有一个关于System.Text.Json.Serialization下的JsonSerialization库的问题。
实际上,我有一个非常大的对象需要存储以便稍后存储,并且没有真正的方法来使其更小。这是整个多基因组网络,组成了我自制的每个基因组超过数千个节点,所有这些节点都有自己独特的数据,所以很明显,它的数据使用量将是巨大的。
所以,我只是做了"JsonSerialization.Serialize(myAINetworkObject)",,经过一段时间的思考和大约1 1GB的内存使用(完全可以理解),它就崩溃了,并显示以下消息:
Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Text.Json.PooledByteBufferWriter.CheckAndResizeBuffer(Int32 sizeHint)
at System.Text.Json.PooledByteBufferWriter.GetMemory(Int32 sizeHint)
at System.Text.Json.Utf8JsonWriter.Grow(Int32 requiredSize)
at System.Text.Json.Utf8JsonWriter.WriteNumberValueMinimized(Single value)
at System.Text.Json.Utf8JsonWriter.WriteNumberValue(Single value)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnWrite(WriteStackFrame& current, Utf8JsonWriter writer)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at NEAT_AI.Network.saveNetwork2(Network myself) in C:\Users\aj200\Documents\GitHub\My-Projects\My-Projects\Completed Projects\C# NEAT AI\NEAT_AI.cs:line 708
at WebAPIClient.Program.Main(String[] args) in C:\Users\aj200\Documents\GitHub\My-Projects\My-Projects\Completed Projects\C# NEAT AI\Program.cs:line 65我可以在这里发布我正在运行的所有代码,但我更喜欢尽可能保持我几个月的工作的私密性。
因此,实际上我的问题是:“有没有办法增加默认JSONSerialization库可以处理的数据量而不会崩溃”。我可以将网络分成“基本网络数据”,并分别为每个基因组分离文件,但这将需要相当多的工作。
我知道JSONSerialization库可能从未被设计为处理多GB对象,也从未预料到会遇到如此大的对象。
作为另一种选择,我愿意对JSONSerialization库进行物理编辑,使其变量使用Int64而不是Int32,但我想知道这样做是否值得付出努力,或者它是否会有所帮助。
致以良好的问候,安德烈
发布于 2021-02-16 22:23:59
感谢所有提供解决方案的人。我尝试了其他一些JSON格式化程序,但它们都不是我想要的。但是,我最终还是创建了一个自定义的save函数,它模仿了JSON的工作方式。
实际上,它不会将所有这些都保存到一个文件中(因此,实际上没有任何限制)。它将每个对象保存为我的PC上的一个文件夹,然后它的每个属性都在任何文件夹旁边的一个文件中,如果我在这个对象中有一个对象列表,这个文件夹也可能在那里。也就是说,我已经创建了一个JSON工作原理的物理文件版本。
例如,如下所示的对象结构(伪代码):
Network {
string saveData;
List<Genome>();
}
Genome {
string someOtherData;
List<Node>();
}
Node {
int ID;
enum node_type;
int current_weight;
}将如下所示:
/Network/genomeObject1/genomeProperties.txt
/Network/genomeObject1/Nodes/Node ID 1/nodeProperties.txt
/Network/genomeObject1/Nodes/Node ID 2/nodeProperties.txt
/Network/genomeObject1/Nodes/Node ID 3/nodeProperties.txt
/Network/genomeObject1/Nodes/Node ID 4/nodeProperties.txt
/Network/genomeObject2/genomeProperties.txt
/Network/genomeObject2/Nodes/Node ID 1/nodeProperties.txt
/Network/genomeObject2/Nodes/Node ID 2/nodeProperties.txt
/Network/genomeObject2/Nodes/Node ID 3/nodeProperties.txt
/Network/genomeObject2/Nodes/Node ID 4/nodeProperties.txt
etc这是一个非常定制化的解决方案,我想要一个简单的序列化脚本或其他东西,但是无论如何它都可以工作。如果我可以让它处理任何可序列化的数据,我可能会在某个时候删除代码!
感谢大家对我的帮助!
https://stackoverflow.com/questions/66219131
复制相似问题