Convert Java Collection / List / String[] Array Into JSON

In this example, we are going to convert a Java collection/list/string[] array into appropriate JSON String using GSON library

Note

I have already written an article about GSON, Convert Java Object To / From JSON, GSON Example, Please see it

Convert Java Object To / From JSON, GSON Example

On this Simple GSON Example explains about converting a Java Object to JSON string nad JSON string to Java Object Using Google Gson JSON library

Google Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Reference -> code.google.com/p/google-gson/

Here I am showing an example about How to convert a Java Object into JSON string and vice versa using Google Gson.

Convert JSON String To Java Object, Jettison Example

On this Jettison Example explains about converting a JSON string to java object Using Jettison JSON library

Jettison is a collection of Java APIs (like STaX and DOM) which read and write JSON. This allows nearly transparent enablement of JSON based web services in services frameworks like CXF or XML serialization frameworks like XStream

Reference -> jettison.codehaus.org/

Here I am showing an example about How to convert a JSON string into Java Object using Jettison library.

Convert Java Object To / From JSON, EclipseLink MOXy Example

On this EclipseLink MOXy Example explains about converting a java object to JSON string and JSON string to java Object Using EclipseLink MOXy library.

EclipseLink MOXy component enables Java developers to efficiently bind Java classes to XML Schemas. MOXy implements JAXB allowing developers to provide their mapping information through annotations as well as providing support for storing the mappings in XML format. The many advanced mappings enable developers to handle the complex XML structures without having to mirror the schema in their Java class model.

Reference -> www.eclipse.org/eclipselink/moxy.php

Here I am showing an example about How to convert a Java Object to JSON string and vice versa using EclipseLink MOXy.

Convert Java Object To / From JSON, Jackson Example

Convert Java Object To / From JSON, Jackson Example explains about converting a Java Object to JSON string, JSON string to Java Object Using Jackson JSON library

Consider an example where you need to pass the request as JSON string, In this situation you can easily serialize the Java Object to JSON string and vice versa

According to Jackson documentation, Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.

Reference -> https://github.com/FasterXML/jackson/

Here I am showing an example about How to convert a Java Object to JSON string and vice versa using Jackson processor.

CXF Convert Number To String

CXF Convert Number To String explains about resolving the issue on serialization when the value of a string is integer when using jettison library

Jettison is an open source java library for processing JSON format. It is the default library bundled with CXF framework, the problem with jettison is that if value of a string is integer type, it automatically convert that string to integer type. By removing the quotes between that number

if the string values is "12345"
{"Student":{"name":"12345"}}
it automatically convert to below value
you can see that quotes are removed even the type of that object is string
{"Student":{"name":12345}}

Resolve CXF JSON Single Element Array Issue

Resolve CXF JSON Single Element Array Issue explains about How to resolve the issue on array serialization when using jettison library

Jettison is an open source java library for processing JSON format. It is the default library bundled with CXF framework, the problem with jettison is that if an array / list having multiple items jettison serialize the array correctly, but it have single item it will serialize incorrectly. you can see the below example

	length = 1: field = value
	length > 1: field = [value1, value2, ...]
	

you can see if it have one item enclosing bracket is missing. In order to avoid this issue, I am going to use Jackson library by replacing Jettison