Runtime: Allows setting of 4 spaces indentation or 2 spaces indentation in JsonSerializerOptions.

Created on 24 Dec 2019  Â·  5Comments  Â·  Source: dotnet/runtime

All 5 comments

@KumoKyaku - is this customization required/blocking some scenario or is it just nice to have? Also, are there other "formatting" customization that you would need other than just the 2 spaces vs 4?

To me, since such customization affects the entire JSON stack (serialization and writer), I don't see it worth the potential perf cost, at least on its own. It would help to understand the reason behind such an ask.

Just 4 spaces are easier to read than 2 spaces。

Wouldn't it be better to offer something more like this?

public partial class JsonSerializerOptions
{
    /// <summary>The number of spaces to write per indentation level when <see cref="WriteIndented" /> is enabled.</summary>
    public int IndentSpaces { get; set; }
}

Wouldn't it be better to offer something more like this?

Agreed. Newtonsoft.Json exposes something like this as well.
https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_Indentation.htm

It also exposes capabilities to customize what char to use (i.e. could use tabs if needed):
https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonTextWriter_IndentChar.htm

That said, I don't yet see enough evidence that such customization is common enough or necessary for the built-in JSON API which focuses more on perf and most important features over providing all sorts of customizations. To me, this particular request falls in the camp of "keep using Newtonsoft.Json if such a feature/capability is important to your use case", because it isn't worth the trade-off considering the design goals of S.T.Json.

Closing - we don't want to add complexity to the writer to support this feature.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Drawaes picture Drawaes  Â·  143Comments

galvesribeiro picture galvesribeiro  Â·  185Comments

terrajobst picture terrajobst  Â·  158Comments

hqueue picture hqueue  Â·  155Comments

ghost picture ghost  Â·  230Comments