Introduction
This article is about a cool feature in Visual Studio that helps save the effort of a developer to generate class a structure from a JSON or XML string.
There is cases application written using the .NET Framework written by a developer that receives a JSON or XML string from a web service or from another third party application. After receiving JSON or XML string data there is a need for further processing of the received data that requires deserialization of the received JSON or XML in a .Net object. And for that a .Net class structure must be created to match the JSON or XML string data so that the JSON or XML string can be deserialized correctly.
Problem Statement
For example, a program receives the following JSON string by calling a web service or third-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 the need for deserializing the received JSON or XML string to a .Net object so further processing can be done based on the received data or on the received data.
Approach 1: Do it manually
In this approach the developer must understand JSON or XML so that he/she can understand the received JSON or XML string and its structure. Once getting the details of JSON or XML and the received JSON or XML string, the developer must convert the JSON or XML string into a meaningful class structure so desterilization can be done without a problem.
So the problem with this approach is so much work must be done by the developer, like:
- Requires knowledge of JSON or XML.
- Requires an understanding of the JSON or XML string sent by the exteranl program.
- Creates class strucutre for the JSON or XML structure that is a trial and error approch because usually the created structure doesn't work in the first go.
- If a JSON or XML string has a complex structure then its difficult and requires time to create the class structure to deserialize the JSON or XML string.
Approach 2: Automated using Visual Studio
This approach uses Visual Studio to generate a class just by copying and pasting the JSON or XML string.
The following is the procedure to generate the class:
- Copy JSON or XML string
JSON
XML
- Go to Edit > Paste Sepcial > Paste JSON As Classes or Paste XML As Classes.
- Visual Studio generates a class structure for the developer as in the following:
The following is an example of the class structure created by copying and pasting a 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 {
-
- privateemployeesEmployee[] employeeField;
-
-
- [System.Xml.Serialization.XmlElementAttribute("employee")]
- publicemployeesEmployee[] employee {
- get {
- returnthis.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 {
- returnthis.firstNameField;
- }
- set {
- this.firstNameField = value;
- }
- }
-
-
- public string lastName {
- get {
- returnthis.lastNameField;
- }
- set {
- this.lastNameField = value;
- }
- }
- }
As you see in the preceding code it will generate the correct attributes for the XML in the class so the deserialization is done without problems.
The advantages of this solution are:
- No need to understand JSON or XML
- No effort require for creating the class
Summary
The Visual Studio feature for creating a class based on a string is really helpful for developers because an understanding of the JSON or XML string and the creation of the class structure requires effort and is sometimes very frustrating.