Click here to Skip to main content
15,889,480 members
Articles / All Topics

Visual Studio 2012 – Paste Special Feature

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
18 Nov 2013CPOL1 min read 21.6K   1
Paste special feature in Visual Studio 2012

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 APIs.

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

C#
{"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 your classes is to 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:

C#
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:

C#
<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:

C#
/// <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:

C#
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, you now have many free hours to do other things.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SSW
South Africa South Africa

Comments and Discussions

 
QuestionTiny but very helpful feature Pin
sanjay.patolia18-Nov-13 21:31
sanjay.patolia18-Nov-13 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.