Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

json.net

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:

  • 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 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

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

like image 656
Eduard Dumitru Avatar asked Jan 20 '16 17:01

Eduard Dumitru


People also ask

What does Jsonproperty attribute do?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

What is JsonSerializerSettings?

Specifies the settings on a JsonSerializer object. Newtonsoft.Json.

What is Jsonconvert?

Provides methods for converting between . NET types and JSON types.

What is Jsonconvert SerializeObject?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.


1 Answers

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!

like image 54
Anderson Pimentel Avatar answered Sep 21 '22 15:09

Anderson Pimentel