Eduard Dumitru
Eduard Dumitru

Reputation: 3252

Newtonsoft.Json adds ellipsis (...) at the middle of a serialized array

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).

Symptoms

The situation is as follows:

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; }
}

I create an array of 618 MyPoco instances: enter image description here

The resulting json is always broken at the middle by an ellipsis: enter image description here

The exact anatomy of the resulting string is this:

So basically, to wrap it up:

Problem

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.

That is the main issue.

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.

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).

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

Further information

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

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

EDIT #1

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

enter image description here

Upvotes: 7

Views: 1510

Answers (1)

Anderson Pimentel
Anderson Pimentel

Reputation: 5767

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

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.

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

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!

Upvotes: 6

Related Questions