I want to serialize .NET objects to JSON in a human-readable way, but I would like to have more control about whether an object's properties or array's elements end up on a line of their own.
Currently I'm using JSON.NET's JsonConvert.SerializeObject(object, Formatting, JsonSerializerSettings)
method for serialization, but it seems I can only apply the Formatting.Indented
(all elements on individual lines) or Formatting.None
(everything on a single line without any whitespace) formatting rules globally for the entire object. Is there a way to globally use indenting by default, but turn it off for certain classes or properties, e.g. using attributes or other parameters?
To help you understand the problem, here are some output examples. Using Formatting.None
:
{"array":["element 1","element 2","element 3"],"object":{"property1":"value1","property2":"value2"}}
Using Formatting.Indented
:
{
"array": [
"element 1",
"element 2",
"element 3"
],
"object": {
"property1": "value1",
"property2":"value2"
}
}
What I would like to see:
{
"array": ["element 1","element 2","element 3"],
"object": {"property1":"value1","property2":"value2"}
}
(I realize my question may be slightly related to this one, but the comments there totally miss the point and don't actually provide a valid answer.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…