小编Ond*_*jko的帖子

序列化时JSON.NET StackOverflowException

当我尝试序列化具有类似结构的对象时,我的C#程序正在运行StackOverflowException:

  • 对象具有互相引用的成员
  • 无法尝试捕获(idk为什么)
  • 如果计数设置为6500以下(可能因计算机而异),则表示已成功序列化

下面的示例代码:

class Chacha
{
    public Chacha NextChacha { get; set; }
}    
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All,
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

static void Main(string[] args)
{
        int count = 15000;

        Chacha[] steps = new Chacha[count];
        steps[0] = new Chacha();

        for (int i = 1; i < count; i++)
        {
            steps[i] = new Chacha();
            steps[i-1].NextChacha = steps[i];
        }

        string serSteps = JsonConvert.SerializeObject(steps, Settings);
}
Run Code Online (Sandbox Code Playgroud)

JSON.NET版本是:9.0.1
.NET Framework:4.5.2有什么 …

c# stack-overflow serialization json.net

1
推荐指数
1
解决办法
4623
查看次数

标签 统计

c# ×1

json.net ×1

serialization ×1

stack-overflow ×1