wolfgang ziegler


„make stuff and blog about it“

Paste JSON / XML As Classes

April 10, 2013

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.

Usage

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.

image

Let’s use some sample data from the json.org web site to demonstrate this feature.

{
"glossary":{
"title":"example glossary",
"GlossDiv":{
"title":"S",
"GlossList":{
"GlossEntry":{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":{
"para":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML"
]
},
"GlossSee":"markup"
}
}
}
}
}

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.

   1: public class Rootobject
   2: {
   3:   public Glossary glossary { get; set; }
   4: }
   5:  
   6: public class Glossary
   7: {
   8:   public string title { get; set; }
   9:   public Glossdiv GlossDiv { get; set; }
  10: }
  11:  
  12: public class Glossdiv
  13: {
  14:   public string title { get; set; }
  15:   public Glosslist GlossList { get; set; }
  16: }
  17:  
  18: public class Glosslist
  19: {
  20:   public Glossentry GlossEntry { get; set; }
  21: }
  22:  
  23: public class Glossentry
  24: {
  25:   public string ID { get; set; }
  26:   public string SortAs { get; set; }
  27:   public string GlossTerm { get; set; }
  28:   public string Acronym { get; set; }
  29:   public string Abbrev { get; set; }
  30:   public Glossdef GlossDef { get; set; }
  31:   public string GlossSee { get; set; }
  32: }
  33:  
  34: public class Glossdef
  35: {
  36:   public string para { get; set; }
  37:   public string[] GlossSeeAlso { get; set; }
  38: }

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.

   1:  
   2: /// <remarks/>
   3: [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
   4: [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
   5: public partial class catalog
   6: {
   7:  
   8:   private catalogBook[] bookField;
   9:  
  10:   /// <remarks/>
  11:   [System.Xml.Serialization.XmlElementAttribute("book")]
  12:   public catalogBook[] book
  13:   {
  14:     get
  15:     {
  16:       return this.bookField;
  17:     }
  18:     set
  19:     {
  20:       this.bookField = value;
  21:     }
  22:   }
  23: }
  24:  
  25: /// <remarks/>
  26: [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  27: public partial class catalogBook
  28: {
  29:  
  30:   private string authorField;
  31:  
  32:   private string titleField;
  33:  
  34:   private string genreField;
  35:  
  36:   private decimal priceField;
  37:  
  38:   private System.DateTime publish_dateField;
  39:  
  40:   private string descriptionField;
  41:  
  42:   private string idField;
  43:  
  44:   /// <remarks/>
  45:   public string author
  46:   {
  47:     get
  48:     {
  49:       return this.authorField;
  50:     }
  51:     set
  52:     {
  53:       this.authorField = value;
  54:     }
  55:   }
  56:  
  57:   /// <remarks/>
  58:   public string title
  59:   {
  60:     get
  61:     {
  62:       return this.titleField;
  63:     }
  64:     set
  65:     {
  66:       this.titleField = value;
  67:     }
  68:   }
  69:  
  70:   /// <remarks/>
  71:   public string genre
  72:   {
  73:     get
  74:     {
  75:       return this.genreField;
  76:     }
  77:     set
  78:     {
  79:       this.genreField = value;
  80:     }
  81:   }
  82:  
  83:   /// <remarks/>
  84:   public decimal price
  85:   {
  86:     get
  87:     {
  88:       return this.priceField;
  89:     }
  90:     set
  91:     {
  92:       this.priceField = value;
  93:     }
  94:   }
  95:  
  96:   /// <remarks/>
  97:   [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
  98:   public System.DateTime publish_date
  99:   {
 100:     get
 101:     {
 102:       return this.publish_dateField;
 103:     }
 104:     set
 105:     {
 106:       this.publish_dateField = value;
 107:     }
 108:   }
 109:  
 110:   /// <remarks/>
 111:   public string description
 112:   {
 113:     get
 114:     {
 115:       return this.descriptionField;
 116:     }
 117:     set
 118:     {
 119:       this.descriptionField = value;
 120:     }
 121:   }
 122:  
 123:   /// <remarks/>
 124:   [System.Xml.Serialization.XmlAttributeAttribute()]
 125:   public string id
 126:   {
 127:     get
 128:     {
 129:       return this.idField;
 130:     }
 131:     set
 132:     {
 133:       this.idField = value;
 134:     }
 135:   }
 136: }
 137:  


TypeScript

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.

   1: interface Rootobject {
   2:     glossary: Glossary;
   3: }
   4:  
   5: interface Glossary {
   6:     title: string;
   7:     GlossDiv: Glossdiv;
   8: }
   9:  
  10: interface Glossdiv {
  11:     title: string;
  12:     GlossList: Glosslist;
  13: }
  14:  
  15: interface Glosslist {
  16:     GlossEntry: Glossentry;
  17: }
  18:  
  19: interface Glossentry {
  20:     ID: string;
  21:     SortAs: string;
  22:     GlossTerm: string;
  23:     Acronym: string;
  24:     Abbrev: string;
  25:     GlossDef: Glossdef;
  26:     GlossSee: string;
  27: }
  28:  
  29: interface Glossdef {
  30:     para: string;
  31:     GlossSeeAlso: string[];
  32: }

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.

Requirements

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.