Pranay Rana: Visual Studio Generate Class From JSON or XML

Sunday, July 26, 2015

Visual Studio Generate Class From JSON or XML

Introduction 

Below Article is on, one of the cool feature available in visual studio which helps/save effort of developer to generate class structure from json/xml string.

There is cases application written using .Net Framework written by developer received json/xml string from the web service or from the other 3rd party application it calling. After receiving xml/json string data there is need of further processing received data, which requires to deserialize received xml/json in .Net object. And for that .Net class structure need to be created which match with json/xml string data so that json/xml string deserialize correctly.

Problem Statement

For example:

Program receives following json string by calling webservice or 3rd party program.

JSON

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

XML

  <employees>
    <employee>
        <firstName>John</firstName> <lastName>Doe</lastName>
    </employee>
    <employee>
        <firstName>Anna</firstName> <lastName>Smith</lastName>
    </employee>
    <employee>
        <firstName>Peter</firstName> <lastName>Jones</lastName>
    </employee>
</employees>

And now there is need for deserializing received json/xml string to .Net object, so further process will be done based on received data or on received data.

Approach 1: Do it manually

In this approach developer need to have knowledge of json/xml so that he/she can understand received json/xml string and it structure. Once getting detail of xml/json and received xml/json string, developer has to convert json/xml string into meaningful class structure so desterilization done without any issue/error.

So problem with this approach is lot of work need to be done by developer like
  1. Require knowledge of json/xml
  2. Understand jso/xmln string sent by exteranl program
  3. create class strucutre for json/xml structure, which is trial and error approch because most of the time created structure doesn't work in first go
  4. If json/xml string having complex structure than its difficult as well as require time to create class structure to deserialize json/xml string
Approach 2 : Automated using Visual Studio

In this approach make use of Visual studio to generate class just by copy pasting xml/json string.

Following steps need to be done for generating class
  1. Copy json/xml string

    JSON


    XML


  2. Go to Edit>Past Sepcial > Paste JSON As Classes or Paste XML As Classes



  3. Visual studio generate Class structure for developer as below

    Below is example of class structure created by copy and pasting JSON string

        public class EmployeeList
        {
            public Employee[] employees { get; set; }
        }
    
        public class Employee
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
        }
    
    

    Below is example of class structure created by copy and pasting XML string

    /// 
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class employees
        {
    
            private employeesEmployee[] employeeField;
    
            /// 
            [System.Xml.Serialization.XmlElementAttribute("employee")]
            public employeesEmployee[] employee
            {
                get
                {
                    return this.employeeField;
                }
                set
                {
                    this.employeeField = value;
                }
            }
        }
    
        /// 
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class employeesEmployee
        {
    
            private string firstNameField;
    
            private string lastNameField;
    
            /// 
            public string firstName
            {
                get
                {
                    return this.firstNameField;
                }
                set
                {
                    this.firstNameField = value;
                }
            }
    
            /// 
            public string lastName
            {
                get
                {
                    return this.lastNameField;
                }
                set
                {
                    this.lastNameField = value;
                }
            }
        }
    



As you see in above code it will generate correct attribute for xml on class so deserialization done without any issue/error.

Advantage of this solution
  1. No need to understand xml/json
  2. No effort require for creating class
Summary

Visual studio feature of creating class based on string really helpful to developer because understanding json/xml string and creating class structure requires efforts and sometime very frustrating.

No comments:

Post a Comment