JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

Why Writeindent is protected? Why WriteComment not have a "newline with indent" override? #2864

Open Flithor opened 1 year ago

Flithor commented 1 year ago

I'm try to write some custom comment by custom JsonConverter for some members.
But i cannot find a direct way to write indent width of current level.
And WriteComment not have a "start with newline and indent" override.

Expected output

{
  "MySubObj": {
    /*The SubProperty1 is for balabalabala...*/
    "SubProperty1": [],
    /*The Dictionary is for balabalabala...*/
    "Dictionary": {
      /*The key1 is for balabalabala...*/
      "key1": "value1",
      /*The key2 is for balabalabala...*/
      "key2": "value2",
      /*The key3 is for balabalabala...*/
      "key3": "value3"
    }
  }
}

Expected behavior

Make WriteIndent public or make WriteComment have a override can output in newline with indent.

Actual behavior

WriteIndent is protected, and WriteComment only append text only, cannot write a indented comment of current indent level directly.

Related issue: https://github.com/JamesNK/Newtonsoft.Json/issues/2334

Flithor commented 1 year ago

My current implementation:

public class JsonWriterEx
{
    static void WriteIndent(this JsonWriter jw)
    {
        typeof(JsonWriter).GetMethod("WriteIndent", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(jw, null);
    }
}