Pasting arbitrary JSON or XML into Visual Studio and automatically having your C# (or VB.NET) classes generated out of it, is a neat little feature added by the “Microsoft Web Developer Tools” Visual Studio 2012 extension.
In the EDIT menu of Visual Studio 2012 you can find the Paste Special menu item which contains two sub items Paste JSON as Classes and Paste XML as Classes. Depending on the current contents of the clipboard, choose one of these items and Visual Studio handles the generation of the C# or VB.NET classes automatically.
After pasting this JSON string into Visual Studio using Paste JSON as Classes we get following C# code, ready to be processed using the Json.NET library.
With XML it works just the same way. Using the usual books.xml sample from MSDN we get these C# classes generated after using the Paste XML as Classes command. It even generates the Serialization attributes making serialization and deserialization work out of the box.
If you installed the TypeScript extension to Visual Studio 2012 you can even use the Paste JSON as Classes functionality in TypeScript (.ts) files (XML is not available yet). Using the above JSON sample data again, we get these TypeScript classes generated.
Being a rather new feature, “special pasting” is still unknown to many developers. Nonetheless it offers a huge productivity boost when in need of writing serialization classes for random JSON / XML data. Definitely a feature every .NET developer should be aware of.
As mentioned initially, you need to have the Microsoft Web Developer Tools for Visual Studio 2012 extension installed to have this feature available. This is an update to Visual Studio 2012 or Visual Studio Express 2012 for Web and can be installed using Web Platform Installer. Further installation instructions can be found on the the ASP.NET and Web Tools site.