0 Comments

Generally the longest and most frustrating part of using a 3rd party API is making all the C# classes that the data will plugin to after you make requests into those API’s.

In Visual Studio 2013 2012 (JSON Support added in 2012 Update 2) this experience has changed, you are now able to paste JSON and XML into Visual Studio and have it automatically generate the classes for you. In this article I will be using a sample data from http://www.json.org/example.html.

First up you need to open a new application, I created a new console application. I’m going to be using the sample JSON below

Generating classes

{"menu": {
   "header": "SVG Viewer",
   "items": [
       {"id": "Open"},
       {"id": "OpenNew", "label": "Open New"},
       null,
       {"id": "ZoomIn", "label": "Zoom In"},
       {"id": "ZoomOut", "label": "Zoom Out"},
       {"id": "OriginalView", "label": "Original View"},
       null,
       {"id": "Quality"},
       {"id": "Pause"},
       {"id": "Mute"},
       null,
       {"id": "Find", "label": "Find..."},
       {"id": "FindAgain", "label": "Find Again"},
       {"id": "Copy"},
       {"id": "CopyAgain", "label": "Copy Again"},
       {"id": "CopySVG", "label": "Copy SVG"},
       {"id": "ViewSVG", "label": "View SVG"},
       {"id": "ViewSource", "label": "View Source"},
       {"id": "SaveAs", "label": "Save As"},
       null,
       {"id": "Help"},
       {"id": "About", "label": "About Adobe CVG Viewer..."}
   ]
}}

Now all that is required from this to create you classes is make sure the JSON is in your clipboard, place your cursor somewhere in a file, go to Edit >  Paste Special > Paste JSON as classes.

With this Simple example the classes below are generated

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

public class Menu
{
   public string header { get; set; }
   public Item[] items { get; set; }
}

public class Item
{
   public string id { get; set; }
   public string label { get; set; }
}

This will also work for XML so taking the XML

<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>
   <item action="ZoomOut" id="ZoomOut">Zoom Out</item>
   <item action="OriginalView" id="OriginalView">Original View</item>
   <separator/>
   <item action="Quality" id="Quality">Quality</item>
   <item action="Pause" id="Pause">Pause</item>
   <item action="Mute" id="Mute">Mute</item>
   <separator/>
   <item action="Find" id="Find">Find...</item>
   <item action="FindAgain" id="FindAgain">Find Again</item>
   <item action="Copy" id="Copy">Copy</item>
   <item action="CopyAgain" id="CopyAgain">Copy Again</item>
   <item action="CopySVG" id="CopySVG">Copy SVG</item>
   <item action="ViewSVG" id="ViewSVG">View SVG</item>
   <item action="ViewSource" id="ViewSource">View Source</item>
   <item action="SaveAs" id="SaveAs">Save As</item>
   <separator/>
   <item action="Help" id="Help">Help</item>
   <item action="About" id="About">About Adobe CVG Viewer...</item>
</menu>

and then going to Edit >  Paste Special > Paste XML as classes will generate

/// <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;
       }
   }
}

Loading generated JSON objects

To load the generated JSON objects all you need to do is use the JavaScriptSerializer as in the below 2 lines.

JavaScriptSerializer jss = new JavaScriptSerializer();
Rootobject obj = jss.Deserialize<Rootobject>(JsonInput.FromAPI);

When you run this piece of code you will notice that the object has been populated with the JSON.

image

Enjoy

Hope this is useful and that many people already know about this great feature and that if you have been manually creating classes that you now have many free hours to do other things with.