DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Containers Trend Report: Explore the current state of containers, containerization strategies, and modernizing architecture.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Introduction to JSON With Java
  • JSON-Based Serialized LOB Pattern
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns

Trending

  • Should I Start My Job Search Now?
  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • The Top 5 Tools for Automated Front-End Testing
  • How To Get Cell Data From an Excel Spreadsheet Using APIs in Java
  1. DZone
  2. Coding
  3. Languages
  4. Which Is the Right Java Abstraction for JSON

Which Is the Right Java Abstraction for JSON

This overview tackles three categories of Java abstractions when it comes to JSON data. Check out their respective strengths and weaknesses.

Theodore Ravindranath user avatar by
Theodore Ravindranath
·
Dec. 06, 17 · Tutorial
Like (12)
Save
Tweet
Share
18.1K Views

Join the DZone community and get the full member experience.

Join For Free

Here's a simple definition of JSON:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. Its job is to deliver data payload between the client and the server.

When our server receives a JSON string, we are supposed to parse the data and load it into apt data structures depending on the server-side language. So, assuming we are using Java on the server side, we are supposed to parse it into apt data structures for Java. 

We want to try and figure out which is the right Java abstraction for JSON. To do that, let's discuss the options for receiving the data in Java. Broadly, there are three categories based on the target data structure:

  1.  A POJO object hierarchy 

  2. A Java collection object, such as a HashMap

  3. A JSON-specific library object like JSONObject

POJO Object Hierarchy 

This is the most Java-ish way of doing it — having a statically typed hierarchy of Java objects and using a reflection-based JSON parsing library to load the data into your POJO objects. 

Advantages

  1. Static typing: Your entire API format and data model are well-defined. 

  2. Standard Collections: Relationships between objects are represented as standard Java util collections.

  3. Clear separation: Your server code is completely isolated from the JSON protocol.

Difficulties

  1. Static typing is seen as more verbose and difficult for developers.

  2. There are some coding conventions to be followed for automatic mapping of fields. This creates a small learning curve.

  3. Perception (misconception?) of poor performance.

  4. This may require the creation of a hierarchy of transfer objects in case your domain model can itself not be directly serialized to JSON format.

Java Collection Object

This is the second most Java-ish way of doing it — using a JSON parsing library to load the data into HashMaps and ArrayLists.

Advantages

  1. Standard collections: Relationships between objects are represented as standard Java util collections.

  2. Clear separation: Your server code is completely isolated from the JSON protocol.

  3. Easy to begin with: Like always, HashMaps look easier than static typing.

Difficulties

  1. There seem to be no popular libraries that primarily target this approach. The same parsing libraries used in (1) above also allow you to do this.

Disadvantages

  1. You lose out on static typing and traceability.

JSON-Specific Library Object

This is the most non-static way of doing it. Instead of converting the JSON data into your domain model, JSONObject encourages you to keep the JSON ties and these protocol-specific objects creep into your business layer.

Advantages

  1. This seems to be the most beginner-friendly approach, and "easy" to use in the beginning.

  2. The mapping is very straightforward.

  3. This approach is made popular due to the in-built support for JSONObject in Android. 

Disadvantages

  1. Dynamic typing: Your lose out on static typing and traceability.

  2. Non-standard Collections: Relationships between objects are also represented as JSONObject and JSONArray. This is not a small issue. Because JSONObject and JSONArray do not play well with Java's standard Collections, it encourages more and more methods to directly support input parameters or return values as JSONObject/JSONArray.

  3. No separation: Your server code continues to carry the tag of the JSON protocol even inside the business logic code.

Which method do you use currently? What are your experiences with your current approach and which approach would you recommend?

JSON Java (programming language) Object (computer science) Data (computing) Abstraction (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to JSON With Java
  • JSON-Based Serialized LOB Pattern
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: