Newtonsoft.Json在序列化数组的中间增加了省略号(...) [英] Newtonsoft.Json adds ellipsis (...) at the middle of a serialized array

查看:317
本文介绍了Newtonsoft.Json在序列化数组的中间增加了省略号(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 Newtonsoft.Json 的序列化功能非常怪异的行为。我试过几乎所有的东西(比如我没有继续使用.net反射通过了 Newtonsoft.Json.dll 集的算法一步走一步)。

This is a very weird behaviour of Newtonsoft.Json's serialization functionality. I've tried almost everything (for instance I didn't go ahead and use .NET Reflector to walk step by step through the Newtonsoft.Json.dll assembly's algorithms).

的情况如下:

我有一个小POCO持有4串属性:

I have a small POCO which holds 4 string properties:

public class MyPoco {
    public string op { get; set; }
    public string left { get; set; }
    public string right { get; set; }
    public string result { get; set; }
}



我创造的618阵列 MyPoco 实例:

由此产生的 JSON 的总是在中间用碎省略号:

The resulting json is always broken at the middle by an ellipsis:

结果字符串的确切解剖是这样的:

The exact anatomy of the resulting string is this:


  • 字符串的第一部分是第156 MyPoco 实例的成功系列化

  • 的字符串的第二部分是从字面上3点(这也打破了JSON语法 - 这实际上是一件好事),其次是 MyPoco 实例是谁的0基于指数的后半部分是466

  • 字符串的第三部分是最后152 MyPoco 实例的成功系列化

  • The first part of the string is the successful serialization of the first 156 MyPoco instances
  • The second part of the string is literally 3 dots (which also breaks the Json syntax - which is actually a good thing) followed by the last half of the MyPoco instance who's 0 based index is 466
  • The third part of the string is the successful serialization of the last 152 MyPoco instances

所以基本上,把它包起来:

So basically, to wrap it up:


  • Newtonsoft.Json 成功序列化我的数组的第一个156项目(指数0到155)

  • 它还成功序列化最后152项(467指数通过617)

  • 这也成功地写在一开始,并在结果字符串

  • 的尽头开闭方括号(代表阵列)
  • 在这个字符串的正中央,它增加了和省略号其中切割线的一半,似乎追求的是15000健康字领先的一群,尾随一群15000健康字之前

  • Newtonsoft.Json is successfully serializing the first 156 items of my array (indices 0 through 155)
  • It also successfully serializes the last 152 items (indices 467 through 617)
  • It also successfully writes the opening and closing square brackets (representing the Array) at the very beginning and at the very end of the resulting string
  • At the very middle of this string, it adds and ellipsis which cuts the string in half, after what would appear to be a leading bunch of 15,000 "healthy" characters and before the trailing bunch of 15,000 "healthy" characters

我不知道该怎么办。我可以继续下去,并使用的JavaScriptSerializer ,但我不希望在 Newtonsoft.Json 失去信任。

I don't know what to do. I could go on and use JavaScriptSerializer but I don't want to lose trust in Newtonsoft.Json.

这是主要的问题。

这感觉就像它应该有一个全面的异常已经崩溃了,而是它静静地失败,可能会令到生产应用严重并发症。

It feels like it should've crashed with a comprehensive exception, but instead it silently fails, which could leave to serious complications in production apps.

我到处找最大缓冲区大小之类的设置,但没有找到任何东西比已经有臭名昭著的最大深度设置在这里是不是这样的,因为我有一个3层树(与最底层的原始字符串)。

I've looked everywhere for "Max Buffer Size" like settings and couldn't find anything more than the already notorious "Max Depth" setting which is not the case here since I have a 3 layer tree (with primitive strings on the deepest layer).

有没有人遇到过这样的的 Newtonsoft.Json

Has anyone ever experienced such a weird behaviour of Newtonsoft.Json?

我同时使用8.0.2和7.0.1的NuGet包版本(我跳过8.0.1)。
两个版本都表现出相同的症状。

I used both 8.0.2 and 7.0.1 Nuget package versions (I skipped 8.0.1). Both versions exhibit the same symptoms.

我针对.NET 4.6和我们谈论一个空控制台应用程序(我复制的症状在干净的方式可能)。

I'm targeting .NET 4.6 and we're talking about an empty Console App (I replicated the symptoms in the cleanest way possible).

下面是因为看到那里,在省略号的快照Visual Studio调试器:

Here's a snapshot of the ellipsis as seen right there, in the Visual Studio debugger:

推荐答案

好消息!似乎毕竟没有任何问题。

Good news! Seems there's no problem after all.

如果你正在做的和我一样,你正在检查的 JSON 变量。然后复制其内容并粘贴在其​​他地方,以验证

If you are doing the same as me, you are inspecting the json variable and then copying its content and pasting somewhere else to validate.

事情是:Visual Studio中走的是beggining加省略号,然后取内容的末尾

The thing is: Visual Studio is taking the beggining adding the ellipsis and then taking the end of the content.

如果你把它写入一个文件,它是完整的,有效的JSON!

If you write it to a file, it's complete and valid JSON!

var lines = new MyPoco[6000];

for (int i = 0; i < lines.Length; i++)
{
    lines[i] = new MyPoco
    {
        op = "Concatenate" + i,
        left = "Integer",
        right = "String",
        result = "String",
    };
}

var json = JsonConvert.SerializeObject(lines, Formatting.Indented);
File.WriteAllText("JsonNet.json", json);

var json2 = new JavaScriptSerializer().Serialize(lines);
File.WriteAllText("JavaScriptSerializer.json", json2);



希望它帮助!

Hope it helps!

这篇关于Newtonsoft.Json在序列化数组的中间增加了省略号(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆