Home C# model for nested properties
Reply: 2

C# model for nested properties

Naveed Ahmed
1#
Naveed Ahmed Published in 2018-01-12 14:14:42Z

I need to make API request to CloudFlare to purge cache of individual files.

Can someone please guide how to represent the below as C# model class.

files: [
        "http://www.example.com/css/styles.css",
        {
          "url": "http://www.example.com/cat_picture.jpg",
          "headers": {
            "Origin": "cloudflare.com",
            "CF-IPCountry": "US",
            "CF-Device-Type": "desktop"
          }
        }
      ]
omaxel
2#
omaxel Reply to 2018-01-12 14:46:24Z

Using classes (maybe you could use better class names then mine :) ):

class Root
{
    [JsonProperty(PropertyName = "files")]
    public List<object> Files { get; set; } = new List<object>();
}

class UrlContainer
{
    [JsonProperty(PropertyName = "url")]
    public string Url { get; set; }

    [JsonProperty(PropertyName = "headers")]
    public Headers Headers { get; set; }
}

class Headers
{
    public string Origin { get; set; }

    [JsonProperty(PropertyName = "CF-IPCountry")]
    public string CfIpCountry { get; set; }

    [JsonProperty(PropertyName = "CF-Device-Type")]
    public string CfDeviceType { get; set; }
}

Usage

var root = new Root();

// Add a simple url string
root.Files.Add("http://www.example.com/css/styles.css");

var urlContainer = new UrlContainer() {
    Url = "http://www.example.com/cat_picture.jpg",
    Headers = new Headers()
    {
        Origin = "cloudflare.com",
        CfIpCountry = "US",
        CfDeviceType = "desktop"
    }
};

// Adding url with headers
root.Files.Add(urlContainer);

string json = JsonConvert.SerializeObject(root);

Note: I'm using Newtonsoft.Json

Marc Gravell
3#
Marc Gravell Reply to 2018-01-12 14:27:13Z
    var obj = new
    {
        files = new object[]
        {
            "http://www.example.com/css/styles.css",
            new
            {
                url = "http://www.example.com/cat_picture.jpg",
                headers = new Dictionary<string,string>
                {
                    { "Origin", "cloudflare.com" },
                    { "CF-IPCountry","US" },
                    { "CF-Device-Type", "desktop"}
                }
            }
        }
    };

The dictionary is there because of the awkward property names like CF-IPCountry which make it not possible to use an anonymous type.

To show it working:

var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented);

gives:

{
  "files": [
    "http://www.example.com/css/styles.css",
    {
      "url": "http://www.example.com/cat_picture.jpg",
      "headers": {
        "Origin": "cloudflare.com",
        "CF-IPCountry": "US",
        "CF-Device-Type": "desktop"
      }
    }
  ]
}

Edit; that's not quite right - the dictionary didn't work, but I don't have time to fix it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342481 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO