Step JSON Serialisation

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        string json = @"
        {
            ""reports"":
            {
                ""report"":
                {
                    ""id"":""1"",
                    ""step"":
                    {
                        ""id"":""2"",
                        ""step"":
                        [
                            {
                                ""id"":""3"",
                                ""step"":
                                [
                                    {
                                        ""id"":""4"",
                                        ""step"":
                                        [
                                            {
                                                ""id"":""8""
                                            },
                                            {
                                                ""id"":""9""
                                            }
                                        ]
                                    },
                                    {
                                        ""id"":""5""
                                    }
                                ]
                            },
                            {
                                ""id"":""6"",
                                ""step"":
                                {
                                    ""id"":""7""
                                }
                            }
                        ]
                    }
                }
            }
        }";

        Rootobject reportResult = JsonConvert.DeserializeObject<Rootobject>(json);

        json = JsonConvert.SerializeObject(reportResult, Formatting.Indented);
        Console.WriteLine(json);
    }
}



public class Rootobject
{
    public Reports reports { get; set; }
}

public class Reports
{
    public Report report { get; set; }
}

public class Report
{
    public string id { get; set; }
    public Step step { get; set; }
}


public class Step
{
    public string id { get; set; }
    [JsonConverter(typeof(SingleOrArrayConverter<Step>))]
    public List<Step> step { get; set; }
}



class SingleOrArrayConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<T>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Array)
        {
            return token.ToObject<List<T>>();
        }
        return new List<T> { token.ToObject<T>() };
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        List<T> list = (List<T>)value;
        if (list.Count == 1)
        {
            value = list[0];
        }
        serializer.Serialize(writer, value);
    }
}
;