Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


You must login to ask a question.

You must login to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

RTSALL Latest Articles

 Define the Concept of JSON Serialization

<strong> Define the Concept of JSON Serialization</strong>

JSON Serialization is the process of converting an object (like a class, list, or data structure in a programming language) into a JSON string format so it can be easily stored, transferred, or sent over the internet.

1. Deserialization using System.Web.Script.Serialization.JavaScriptSerializer

The JavaScriptSerializer.Deserialize<T>(input) method attempts to deserialize a string of valid JSON into an object of the specified type<T>, using the default mappings natively supported by JavaScriptSerializer.
Example:

using System.Collections; 
using System.Web.Script.Serialization; 
// …
string rawJSON = "{\"Name\":\"Fibonacci Sequence\",\"Numbers\":[0, 1, 1, 2, 3, 5, 8, 13]}";
JavaScriptSerializer JSS = new JavaScriptSerializer();
Dictionary parsedObj = JSS.Deserialize>(rawJSON);
string name = parsedObj["Name"].toString(); 
ArrayList numbers = (ArrayList)parsedObj["Numbers"]

2. Serialization using Json.NET

[JsonObject("person")] 
public class Person 
{
 [JsonProperty("name")] 
public string EmployeeName { get; set; } 
[JsonProperty("age")] 
public int EmployeeAge { get; set; } 
[JsonIgnore] 
public string Address { get; set; }
 }
Employee employee = new Employee { EmployeeName = "Harry", EmployeeAge = 27, Address = "New Delhi" }; string rawJson = JsonConvert.SerializeObject(employee);
Console.WriteLine(rawJson); // {"name":"Harry","age":27}

Note:-  How properties (and classes) can be decorated with attributes to change their presence in the resulting JSON string or remove them from the JSON string (JsonIgnore). 

3. Serialization-Deserialization using Newtonsoft.Json

Unlike the other helpers, this one uses static class helpers to serialize and deserialize, hence it is a little bit easier than the others to use.

using Newtonsoft.Json; 
var rawJSON = "{\"Name\":\"Fibonacci Sequence\",\"Numbers\":[0, 1, 1, 2, 3, 5, 8, 13]}"; 
var fibo = JsonConvert.DeserializeObject>(rawJSON); 
var rawJSON2 = JsonConvert.SerializeObject(fibo);

4. Json.NET with JsonSerializerSettings using Serialization

This serializer has some better features that the default .net json serializer doesn’t have, for example, Null value handling, you just need to create the JsonSerializerSettings :
For Example:

public static string Serialize(T obj)
{
string result = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});
return result;

What is deserialize and serialize in JSON?

In simple words, JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

For example: When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.

Say, you have an object:

{foo: [1, 4, 7, 10], bar: "baz"}

serializing into JSON will convert it into a string:

'{"foo":[1,4,7,10],"bar":"baz"}'

which can be stored or sent through the wire anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: “baz”}.

Define JSON

In a few words, JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and arrays (or other serializable values). It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.

Related Posts

Leave a comment

You must login to add a new comment.