Click here to Skip to main content
11,576,635 members (57,093 online)
Click here to Skip to main content

Tagged as

Visual Studio 2012 – Paste Special Feature

, 18 Nov 2013 CPOL 10.4K
Rate this:
Please Sign up or sign in to vote.
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

{"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:

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, 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)

Share

About the Author

Gordon W Beeming
Software Developer Derivco
South Africa South Africa
Gordon Beeming is a Software Developer at Derivco in the sunny city of Durban, South Africa. He spends most his time hacking away at the keyboard in Visual Studio or with his family relaxing. He is a Visual Studio ALM Rangers, Visual Studio ALM MVP and Friend of Red Gate. His blog is at 31og.com and you can follow him on Twitter at twitter.com/gordonbeeming

http://31og.com

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 19 Nov 2013
Article Copyright 2013 by Gordon W Beeming
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid