I was recently tasked with building a web service that integrates with a 3rd party service in order to get new Contacts into a CRM organization. This web service utilized the popular JSON, which is a data interchange format, and widely used within many web applications and web services. More programmers are opting to use JSON over other formats such as XML due to its efficiency and human readability.
When working with JSON or XML, it is important to understand the process of serialization and deserialization. Serialization is the process of converting data objects into a format of your choice, such as JSON.
Deserialization is the process of converting the serialized data back into a data object for the program to easily read the values of each data member.
The process of serialization occurs within the source application once it’s ready to send to the target application while deserialization will occur within the target application once the data has been received. There are many frameworks and classes available to serialize and deserialize JSON.
In order to start using this framework, you will first need to install the NuGet Visual Studio extension (http://www.nuget.org/). Once installed, open your project in Solution Explorer. Right click on your project and select Manage NuGet Packages. Search for Json.NET and click Install. This will add the Newtonsoft.Json package with the NewtonSoft.Json.dll reference to your project.
Let’s see how the code works.
In the following example, I have set up a class for a data object called Contact:
After creating a Contact object with some sample data, serializing it to JSON will then require only one line of code:
The result can then be deserialized back to an object also using one line of code:
As you can see, there isn’t much coding involved using the JSON.net framework! It’s clean and simple. If you have any questions about this topic, feel free to contact Beringer today.