Kevin Simple Kevin Simple - 1 month ago 12
C# Question

OutOfMemoryException in Custom Serialization - JSON.NET

I created my own Custom Serialization method with JSON.NET,
the method looks like :

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var Obj = (xObject)value;
writer.WriteStartArray();

foreach(var TargetObject in Obj)
{
writer.WriteStartObject();
writer.WritePropertyName(TargetObject.Data.Key);
writer.WriteValue(TargetObject.Data.Value);
writer.WriteEndObject();
}
writer.WriteEndArray();
}


I got an
OutOfMemoryException
exception at
writer.WriteValue(TargetObject.Data.Value)
, saying insufficient memory, any idea that I can keep my custom serialization but avoid this issue?

Note, yes, you are right,the object I'm trying to serialize is more than 1GB.

Answer

I found finally, the reason is because the exception from overflowing the String type from .NET. So , simply, its just too big data for a String field, there is no solution as there isn't a problem with json.net, my solution is to simplifying my data, or output as a file on drive rather than storing its whole data any time in memory as string field, just impossible....