0

I am having trouble formatting my serialized C# objects using System.Text.Json.Serialization. Here are some example objects similar to what I am serializing:

public class school
{
    public string school {get;set;}
    public string address {get;set;}
    public List<classroom> classes {get;set;} 
}

public class classroom
{
    public string topic {get;set;}
    public int room {get;set;}
    public List<student> students {get;set;}
} 

public class student
{
    public string name {get;set;}
    public int age {get;set;}
    public double grade {get;set;}
    public List<pet>? pets {get;set;}
}

public class pet
{
    public string name {get;set;}
    public string type {get;set;}
}

And here is the way I would like to have the json formatted: I think I am trying to have the JsonSerializerOptions set to not WriteIndented for most of my objects, but I do want a newline for each object in an array. Basically, I want every "{" to start a new line.

{"school": "fake elementary", "address": "1234 Fake Ln.", "classes":[
    {"class": "history", "room": 21, "students": [
        {"name": "henry", "age": 8, "grad": 0.85, "pets": [
            {"type": "dog", "name": "kitty"}
        ]},
        {"name": "jill", "age": 9, "grad": 0.95, "pets": [
            {"type": "cat", "name": "pooch"}
        ]},
        {"name": "andy", "age": 8, "grad": 0.75},
        {"name": "mary", "age": 9, "grad": 0.85},
        {"name": "sally", "age": 8, "grad": 0.45}
    ]},
    {"class": "math", "room": 15, "students": [
        {"name": "henry", "age": 8, "grad": 0.95},
        {"name": "jill", "age": 9, "grad": 0.75},
        {"name": "andy", "age": 8, "grad": 0.45},
        {"name": "mary", "age": 9, "grad": 0.95},
        {"name": "sally", "age": 8, "grad": 0.85}
    ]}
]}

Unfortunately, I can either serialize the school object with or without WriteIndented and that applies to all of its objects. Is there a way around this? I am trying to see if I need to make a converter that inherits from JsonConverter, but I haven't been able to figure this out. Otherwise, I could just write out my serialized json without any Indentation, then modify the document to look the way I want it, but this does not seem like the right approach.

  • Don't think there is any support for custom whitespace. Why would you want this? – Charlieface Jun 16 '22 at 20:37
  • I think this question is duplicate of[JSON formatter in C#](https://stackoverflow.com/questions/4580397/json-formatter-in-c) you can check the answers. – sina fayazi Jun 16 '22 at 20:52
  • @Charlieface, I am wanting to minimize the number of lines while maintaining readability. The actual json will have machine commands and the human operator needs to sometimes review the instructions. – mnewsum Jun 16 '22 at 21:06
  • @sinafayazi, I looked at this post, but it does not address my formatting issue. It doers add indentation, but it does so to all objects. Also, it uses Newtonsoft.Json instead of System.Text.Json. – mnewsum Jun 16 '22 at 21:08
  • I removed indentation, then modified the resulting json with the code below. I does not look great, but it puts newlines where I want them (but does not tab accordingly). jsonString = jsonString.Replace("[", "[" + System.Environment.NewLine); jsonString = jsonString.Replace("]", "]" + System.Environment.NewLine); jsonString = jsonString.Replace("},{", "}," + System.Environment.NewLine + "{"); – mnewsum Jun 16 '22 at 21:09
  • A custom formatter would probably be your best bet. Iterate through the unformatted JSON string while using a string builder. When you hit '{' increment an indentation index and create a new line with '\t' * the indent index, decrement on '}' etc. Play around with the algorithm until it creates the spacing you want. I think doing this kind of iteration with a string builder is easier than using the replace method. – tvandinther Jun 17 '22 at 01:29

0 Answers0