.NET Power Tip 9: Paste JSON/XML as Classes

This .NET Power Tip makes me bang my head against the wall over and over again!

How many times have I watched developers write line after line of C# code to create a structure that matches the structure of an XML or JSON document in classes?

I can tell you, it was not just once or twice…

I know, there are plenty of online tools that can do that. However, starting the browser, googling, copy-pasting, and so on. It’s always a hassle, but not anymore!

It turns out that Visual Studio has two very interesting functions that I have never heard of before:

Edit –> Paste  Special –> Paste JSON as Classes

and

Edit –> Paste Special –> Paste XML as Classes

Well, they deliver exactly what they promise. Take the following JSON Document for example:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Running “Paste JSON as Classes” gives you this:

    public class Rootobject
    {
        public Menu menu { get; set; }
    }

    public class Menu
    {
        public string id { get; set; }
        public string value { get; set; }
        public Popup popup { get; set; }
    }

    public class Popup
    {
        public Menuitem[] menuitem { get; set; }
    }

    public class Menuitem
    {
        public string value { get; set; }
        public string onclick { get; set; }
    }

Yup, this is amazing!

Let’s take an XML example, as well:

<menu>
    <header>Adobe SVG Viewer</header>
    <item action="Open" id="Open">Open</item>
    <item action="OpenNew" id="OpenNew">Open New</item>
    <separator/>
    <item action="ZoomIn" id="ZoomIn">Zoom In</item>
</menu>

gives you:

  /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class menu
    {

        private object[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("header", typeof(string))]
        [System.Xml.Serialization.XmlElementAttribute("item", typeof(menuItem))]
        [System.Xml.Serialization.XmlElementAttribute("separator", typeof(object))]
        public object[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class menuItem
    {

        private string actionField;

        private string idField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string action
        {
            get
            {
                return this.actionField;
            }
            set
            {
                this.actionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

 

*bangingmyheadagainstthewall*