skip to Main Content

I’m working on a reading in a JSON file in C# using the JsonSerializer.Deserialize<>(json) method from the System.Text.Json package. The JSON file contains the following node textClasses:

"textClasses": {
        "callout": {
            "fontSize": 29,
            "fontFace": "Roboto Regular",
            "color": "#5158A7"
        },
        "title": {
            "fontSize": 12,
            "fontFace": "Roboto Regular",
            "color": "#5A6066"
        },
        "header": {
            "fontSize": 11,
            "fontFace": "Roboto Light",
            "color": "#5A6066"
        },
        "label": {
            "fontSize": 11,
            "fontFace": "Roboto Light",
            "color": "#5A6066"
        }
    },

How should I define the class structure in my C# project as we have variable sub root names (callout, title, header, label) under the textClasses node?

2

Answers


  1. My suggestion would be to create classes which have all possibilities for your subroot names. Instances which don’t have values for some of then would simply map nulls, zeroes or empty strings where the data is missing.

    public class TextClass
    {
        public CalloutBase Callout { get; set; }
        public CalloutBase Title { get; set; }
        public CalloutBase Header { get; set; }
        public CalloutBase Label { get; get; }
    }
    public class CalloutBase
    {
        public int FontSize { get; set; }
        public string FontFace { get; set; }
        public string Color { get; set; }
    }
    

    Your CalloutBase class defines the sub-members and the textClass reuses the CalloutBase for the callout, title, header and label.

    Any missing data would resolve to null.

    For example, the following Json would resolve to a TextClass object having valid data for callout, title and header, but a null value for label:

    "textClasses": {
            "callout": {
                "fontSize": 29,
                "fontFace": "Roboto Regular",
                "color": "#5158A7"
            },
            "title": {
                "fontSize": 12,
                "fontFace": "Roboto Regular",
                "color": "#5A6066"
            },
            "header": {
                "fontSize": 11,
                "fontFace": "Roboto Light",
                "color": "#5A6066"
            },
    }
    
    Login or Signup to reply.
  2. as my understanding, its not a full json. so base on what i see your class would be (you can go and use online json to c# class converters) but always take it with grain of salt (it might create a duplicated classes.):

    void Main()
    {
        string json = "{"textClasses":{"callout":{"fontSize":29,"fontFace":"Roboto Regular","color":"#5158A7"},"title":{"fontSize":12,"fontFace":"Roboto Regular","color":"#5A6066"},"header":{"fontSize":11,"fontFace":"Roboto Light","color":"#5A6066"},"label":{"fontSize":11,"fontFace":"Roboto Light","color":"#5A6066"}}}";
        JsonConvert.DeserializeObject<ROOT>(json).Dump();
    }
    
    public class ROOT
    {
        public TextClasses textClasses {get;set;}
    }
    public class TextClasses
    {
        public TextClass callout { get; set; }
        public TextClass title { get; set; }
        public TextClass header { get; set; }
        public TextClass label { get; set; }
        
        public TextClasses(){
            callout = new TextClass();
            title = new TextClass();
            header = new TextClass();
            label = new TextClass();
        }
    }
    
    public class TextClass
    {
        public int FontSize { get; set; }
        public string FontFace { get; set; }
        public string Color { get; set; }
        public TextClass() {}
    }
    

    result would be
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search