4

I usually get very big JSON with a lot of lists and a lot of elements inside the lists. My goal is to get the left structure instead of the right one. The right structure is what I get from Newtonsoft. Is there another library that gives me the control to print it like this?

enter image description here

Optimal case is:

enter image description here

erikvimz
  • 5,256
  • 6
  • 44
  • 60
Dakson
  • 75
  • 1
  • 7
  • And if you will get it ever more compact than the left one, is that what you're looking for ? – Orel Eraki Nov 09 '18 at 10:01
  • When you serialise an object don't format it `SerializeObject(product, Formatting.Indented)` would become `SerializeObject(product)` or `SerializeObject(product, Formatting.None)` E: This will make it all on one line, why do you want your JSON to look like the optimal case? –  Nov 09 '18 at 10:04
  • @OrelEraki Hello, I updated my post with the optimal case . – Dakson Nov 09 '18 at 10:04
  • @Matt then I get a big block which is very unreadable. I still want to be able to see some of the elements properly. Basically a balance between unformatted and formatted. (Often I have to check manually for specific elements) – Dakson Nov 09 '18 at 10:06
  • @Dakson what else have you tried? – kͩeͣmͮpͥ ͩ Nov 09 '18 at 10:07
  • @DavidKemp NewtonSoft, DataContractJsonSerializer, JToken – Dakson Nov 09 '18 at 10:09
  • @Dakson Code? You know that you can use the JsonWriter directly for example? – kͩeͣmͮpͥ ͩ Nov 09 '18 at 10:10
  • ^ The good news is you wont need to write code to deserialise it! –  Nov 09 '18 at 10:27
  • Related or duplicate: [How to apply indenting serialization only to some properties?](https://stackoverflow.com/q/28655996/3744182), [Newtonsoft inline formatting for subelement while serializing](https://stackoverflow.com/q/30831895/3744182). Agree? – dbc Nov 10 '18 at 18:37
  • Yes I can agree – Dakson Nov 11 '18 at 14:31

1 Answers1

8

You can get the indenting you want with Json.Net (a.k.a. Newtonsoft Json) if you subclass the JsonTextWriter class and override the WriteIndent method like this:

public class CustomJsonTextWriter : JsonTextWriter
{
    public CustomJsonTextWriter(TextWriter writer) : base(writer)
    {
    }

    protected override void WriteIndent()
    {
        if (WriteState != WriteState.Array)
            base.WriteIndent();
        else
            WriteIndentSpace();
    }
}

Then create a small helper method to make it easy to use the custom writer:

public static class JsonHelper
{
    public static string SerializeWithCustomIndenting(object obj)
    {
        using (StringWriter sw = new StringWriter())
        using (JsonWriter jw = new CustomJsonTextWriter(sw))
        {
            jw.Formatting = Formatting.Indented;
            JsonSerializer ser = new JsonSerializer();
            ser.Serialize(jw, obj);
            return sw.ToString();
        }
    }
} 

Here is a working demo: https://dotnetfiddle.net/RusBGI

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Amazing, exactly what I was searching for and always found out big monstrous Writers. This one is just simple and perfect. Thanks a lot. – Martin Apr 19 '23 at 08:58