Why does JSON.NET serialize everything on a single line?

后端 未结 2 1898
情深已故
情深已故 2021-01-02 05:46

I simply want the JSON.net serializer to write out JSON objects (to file), one object per line but instead it just appends everything on the same top line.

2条回答
  •  忘掉有多难
    2021-01-02 06:04

    The samples provided are indented for clarity, but the default behavior is to write the resulting JSON string without any unnecessary whitespace. You can override this behavior like this:

    jw.Formatting = Formatting.Indented;
    jw.WriteStartObject();
    ...
    

    Further Reading

    • Formatting enum

    To ensure that each entry is appended to a new line, you could simply write a new line character after you've written your JSON object, like this:

    ...
    jw.WriteEndObject();
    jw.WriteRaw("\n");
    

    Or by calling WriteLine on the underlying TextWriter, though that would need to be done outside of this method.

提交回复
热议问题