top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's pictureNishaa V

Serialization and De-Serialization of JSON using Jackson JSON library

What is a JSON ?


JavaScript Object Notification(JSON) is a lightweight format used for storing and transmitting data. It is commonly used in web applications , Application Programming Interfaces (API's), any types of electronic data exchanges. It is in a human readable format that is language independent. JSON was derived from JavaScript but many of the programming languages have the ability to generate and parse JSON format.


Characteristics of JSON:


Human readable format that uses a combination of key-value pairs and arrays.

Structured format where curly braces hold objects and square brackets hold arrays.

Lightweight and text based format so its very small in size.

Datatypes supported are boolean, strings, numbers, arrays, objects and null.

Language independent that makes it available to be parsed and generated by various programming languages.


Example of a JSON file:

[

{

"userAddress":{

"plotNumber":"26-Y",

"street":"angeline",

"state":"Virginia",

"country":"USA",

"zipCode":20967

},

"user_first_name":"John",

"user_last_name":"Smith",

"user_contact_number":5432312345,

"user_email_id":"john@smith.com"

}]


What is Serialization and DeSerialization?


Serialization is the process of converting Java objects into JSON whereas Deserialization is a process of converting JSON into Java objects.

In the context of JSON, serialization involves converting native data structures (such as objects, arrays, and primitives) into a JSON string representation and deserialization involves taking a JSON string and converting it into native data structures or objects of the programming language.


How Jackson Library achieves this Serialization and Deserialization?


  1. Data Binding (POJO Mapping): Data binding, also known as POJO mapping, involves converting JSON data to Java objects (deserialization) and vice versa (serialization). Using this approach, you can work with JSON data through Java objects, which makes data handling more straightforward. Deserialization: During deserialization, Jackson analyzes JSON key-value pairs and matches them to fields and properties of Java objects. It automatically instantiates Java classes and fills in their fields based on the JSON data, leveraging reflection for this process. Serialization: For serialization, Jackson explores the Java object structure and creates a JSON string representation. This representation includes the values of the object's fields and properties.

  2. Streaming API: Jackson offers a basic streaming API for handling JSON data as a stream of tokens. This approach is beneficial for large JSON documents or when performance and memory efficiency matter. Using the streaming API, you can parse JSON incrementally, dealing with tokens one by one, avoiding the need to store the entire document in memory. This involves sequentially reading tokens like objects, arrays, strings, and numbers from a source (a file or network stream) and responding to them. The streaming API requires more effort to use than data binding, but it offers enhanced performance and reduced memory usage, particularly for large JSON documents.

  3. Tree Model (ObjectNode and ArrayNode): Jackson offers a tree-like data model for JSON, similar to XML's DOM. This model organizes JSON as a hierarchical tree of "ObjectNode" and "ArrayNode" objects, providing a familiar interface for manipulating JSON data. Using the tree model, developers can freely create, edit, and explore JSON data using standard tree traversal methods. This is especially helpful when creating or modifying JSON data dynamically, without needing to convert it to Java objects. However, the tree model may not be the most efficient option for large JSON documents compared to data binding or streaming APIs, as it requires maintaining an in-memory tree structure, which can introduce overhead.



What are the pros and cons of each method?

The optimal JSON processing method with Jackson varies based on factors such as: Unique application needs , Performance efficiency, Memory availability, Personal preferences. Different methods offer specific pros and cons, so it's important to select the most suitable approach for your situation.


Data Binding (POJO Mapping)

Benefits:

Ease of Use: Converting JSON data to Java objects and back is uncomplicated, especially for manageable data.

Oject-Oriented Programming: JSON data can be handled using ordinary Java objects, simplifying manipulation and interaction.

Drawbacks:

Reduced Performance:Mapping done through reflection can slow down the process, particularly with large or complex JSON structures.

Memory Consumption: Data binding can require more memory, especially when dealing with intricate object networks or extensive object collections.


Streaming API

Benefits:

Memory Usage:Processing JSON as a stream uses less memory, making it ideal for large JSON files or devices with limited memory.

Speed: Streaming APIs are often faster than data binding, particularly with large JSON files, due to lower memory usage and fewer objects created.

Drawbacks:

Complexity: Working with streaming APIs requires understanding JSON structure and often includes writing more code than with data binding.

Limited Flexibility: Certain operations, such as randomly accessing or modifying JSON data, can be more complex with streaming APIs compared to other methods.


Tree Model (ObjectNode and ArrayNode)

Advantages:

Flexibility: Allows for easy creation, modification, and navigation of JSON data in memory, offering more control than data binding or streaming.

Familiarity: Familiar concept for developers with experience in tree-structured APIs like XML DOM. Disadvantages:

Memory Usage: Storing JSON as a tree can require a lot of memory, particularly for large datasets.

Performance: Manipulating the tree structure can be slower than other methods, especially for complex operations or large amounts of data.


For small JSON data, data binding offers simplicity and ease of use, especially when performance and memory usage are not crucial factors. However, for large JSON documents and situations where managing memory usage or having granular control over parsing and generating JSON is essential, consider using the streaming API or tree model. The optimal choice depends on the specific requirements and limitations of your application.











32 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page