JSON to C# class conversion using Paste Special in Visual Studio


Wednesday, 25 February 2015

Don't you just hate it when you stumble over a feature in Visual Studio that's been available for years, and realize that it could have saved you many hours of coding?

If you're regularly serializing or deserializing JSON data, you've most likely hand-coded a class containing your JSON schema to make life a little easier.

The Web Tools in Visual Studio contain a simple tool to do this automatically in a single click.

How to use this tool?

All you need to do is copy your JSON data to the clipboard, then open a C# class file. Once open, go to Edit/Paste Special/Paste JSON As Classes

 

Example JSON snippet

{
   "uri":"sip:[email protected]@contoso.com",
   "sourceNetwork":"SameEnterprise",
   "type":"User",
   "name":"Lene Aaling",
   "_links":{
      "self":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]"},
      "contactPhoto":{"href":"/ucwa/oauth/v1/applications/101551302053/photos/[email protected]"},
       "contactPresence":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]/presence"},
       "contactLocation":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]/location"},
       "contactNote":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]/note"},
       "contactSupportedModalities":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]@contoso.com/supportedMedia"},
       "contactPrivacyRelationship":{"href":"/ucwa/oauth/v1/applications/101551302053/people/[email protected]@contoso.com/privacyRelationship"}
    },
    "rel":"contact",
    "etag":"4061867540"
}

 

Example C# class generated

 public class Rootobject
        {
            public string uri { get; set; }
            public string sourceNetwork { get; set; }
            public string type { get; set; }
            public string name { get; set; }
            public _Links _links { get; set; }
            public string rel { get; set; }
            public string etag { get; set; }
        }
        public class _Links
        {
            public Self self { get; set; }
            public Contactphoto contactPhoto { get; set; }
            public Contactpresence contactPresence { get; set; }
            public Contactlocation contactLocation { get; set; }
            public Contactnote contactNote { get; set; }
            public Contactsupportedmodalities contactSupportedModalities { get; set; }
            public Contactprivacyrelationship contactPrivacyRelationship { get; set; }
        }
        public class Self
        {
            public string href { get; set; }
        }
        public class Contactphoto
        {
            public string href { get; set; }
        }
        public class Contactpresence
        {
            public string href { get; set; }
        }
        public class Contactlocation
        {
            public string href { get; set; }
        }
        public class Contactnote
        {
            public string href { get; set; }
        }
        public class Contactsupportedmodalities
        {
            public string href { get; set; }
        }
        public class Contactprivacyrelationship
        {
            public string href { get; set; }
        }

It also works for VB.NET!

  Public Class Rootobject
        Public Property uri As String
        Public Property sourceNetwork As String
        Public Property type As String
        Public Property name As String
        Public Property _links As _Links
        Public Property rel As String
        Public Property etag As String
    End Class
    Public Class _Links
        Public Property self As Self
        Public Property contactPhoto As Contactphoto
        Public Property contactPresence As Contactpresence
        Public Property contactLocation As Contactlocation
        Public Property contactNote As Contactnote
        Public Property contactSupportedModalities As Contactsupportedmodalities
        Public Property contactPrivacyRelationship As Contactprivacyrelationship
    End Class
    Public Class Self
        Public Property href As String
    End Class
    Public Class Contactphoto
        Public Property href As String
    End Class
    Public Class Contactpresence
        Public Property href As String
    End Class
    Public Class Contactlocation
        Public Property href As String
    End Class
    Public Class Contactnote
        Public Property href As String
    End Class
    Public Class Contactsupportedmodalities
        Public Property href As String
    End Class
    Public Class Contactprivacyrelationship
        Public Property href As String
    End Class

Caveats and Observations

The code doesn't seem to filter out or warn against JSON element names that are C# function names or keyword, but you'd see those fairly quickly in the editor as an error and could easily fix them. Apparently it's meant to prepend them with an underscore (_) but I've noticed this is not always consistent.

The Past JSON As Classes option isn't available from within the code editor's context menu, which seems odd, as normal Paste is.

Tags

Private
Share with: 

Don't you just hate it when you stumble over a feature in Visual Studio that's been available for years, and realise that it could have saved you many hours of coding?


Support this Site



Popular Articles

What is Kutamo?
Kilimanjaro 2023
Kilimanjaro 2015
Kilimanjaro 2013
Australian Postcodes
New Zealand Postcodes
Worldwide City Database

Recent Articles

Favourite Links

Kutamo Studios
ErrLog.IO
Kutamo
AfterTheCloud
Kilimanjar 2023