https://stackoverflow.com/questions/53223517

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
					
public class Program
{
	public static void Main()
	{
        var group = new ElementGroup
        {
            Name = "Group 1",
            Elements = new List<Element>
            {
                new Element
                {
                    Name = "Element 1",
                    Coordinates = new List<List<double>>
                    {
                        new List<double> { 1.1, 1.2, 1.3 },
                        new List<double> { 2.1, 2.2, 2.3 },
                        new List<double> { 3.1, 3.2, 3.3 }
                    },
                    Indices = new List<int> { 1, 2, 3, 4, 6, 7, 8, 10 },
                    Volume = 123.4776,
                    Voids = 73.27857,
                    Type = "foo",
                    Guid = "fgdhgt985hfwe",
                    Quality = "abc"
                },
                new Element
                {
                    Name = "Element 2",
                    Coordinates = new List<List<double>>
                    {
                        new List<double> { 4.1, 4.2, 4.3 },
                        new List<double> { 5.1, 5.2, 5.3 },
                        new List<double> { 6.1, 6.2, 6.3 }
                    },
                    Indices = new List<int> { 3, 5, 7, 8, 9, 11, 18, 20 },
                    Volume = 224.9833,
                    Voids = 91.54733,
                    Type = "bar",
                    Guid = "ghtejkh309gjs",
                    Quality = "def"
                },
            }
        };

        string json = JsonHelper.SerializeWithCustomIndenting(group);
        Console.WriteLine(json);
	}
}

public static class JsonHelper
{
    public static string SerializeWithCustomIndenting(object obj)
    {
        using (StringWriter sw = new StringWriter())
        using (JsonWriter jw = new CustomJsonTextWriter(sw))   // change this to new JsonTextWriter(sw) to see the default formattingg
        {
            jw.Formatting = Formatting.Indented;
            JsonSerializer ser = new JsonSerializer();
            ser.Serialize(jw, obj);
            return sw.ToString();
        }
    }
} 

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

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

public class ElementGroup
{
    public string Name { get; set; }
    public List<Element> Elements { get; set; }
}

public class Element
{
    public string Name { get; set; }
    public List<List<double>> Coordinates { get; set; }
    public List<int> Indices { get; set; }
    public double Volume { get; set; }
    public double Voids { get; set; }
    public string Type { get; set; }
    public string Guid { get; set; }
    public string Quality { get; set; }
}
;