Thursday, August 8, 2013

Web Essentials Goodness - Paste XML/JSON As Classes

I recently have been working on project where I have been evaluating some vendor APIs. Across the multiple vendors the responses have been in either XML or JSON. Since I am most familiar with .NET development, I have been creating my testing clients in C# after verifying the responses from the API with Fiddler. So in many cases once I have seen the XML/JSON output, I want to be able to download that data into my application as a class. Thankfully, the Web Essentials Visual Studio extension by Mads Kristensen, includes two excellent options. Paste XML As Classes and Paste JSON As Classes. With these two options ( from the File->Paste Special menu) when you have a class open you can paste any JSON or XML object and the extension will generate a class that corresponds to the document for you. In some cases (especially with XML) you may need to perform some additional clean up of local variable names, etc. But in the long run this has saved me lots of keystrokes. Below are a couple of examples.

JSON Object
Corresponding Class created with Paste JSON as Classes

XML Object

Corresponding Class created with Paste XML as Classes

As I stated earlier, the variable names in the classes created for this XML object could use a little clean up, but with a good refactoring tool, this should not be a difficult task and something that I find much more enjoyable than creating the entire file by hand.

Hope that you find this useful and that it can save you some time.