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).
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:
The resulting json is always broken at the middle by an ellipsis:
The exact anatomy of the resulting string is this:
MyPoco
instancesMyPoco
instance who's 0 based index is 466MyPoco
instancesSo basically, to wrap it up:
Newtonsoft.Json
is successfully serializing the first 156 items of my array (indices 0 through 155)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
?
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).
Here's a snapshot of the ellipsis as seen right there, in the Visual Studio debugger:
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.
Specifies the settings on a JsonSerializer object. Newtonsoft.Json.
Provides methods for converting between . NET types and JSON types.
SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With