Visual Studio 2012 – Paste Special Feature

category: [ Visual Studio ] tags: [ JSON ] [ Visual Studio ] [ Visual Studio 2013 ] [ XML ]
created: 29 Oct 2013 @ 18:48 modified: 02 Aug 2018 @ 19:34


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

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

This will also work for XML so taking the XML

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

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.

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

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.

ABOUT ME

Gordon Beeming works at Nologo Studios in the sunny city of Durban, South Africa. He is the Lead for the Data and Services Team and has a strong focus on Developer Efficiencies and R&D. When he's not hacking away at a keyboard in Visual Studio he'll generally be relaxing with his family or hitting the black top getting in some mileage. He is a Visual Studio ALM Ranger and Visual Studio ALM MVP.

Follow me on Strava