Converting a java object to json
Gson is quite powerful when you simply want to build json from a java object. It does not require anything fancy, just pass the java object and you are done. However, it does allow customizations. You can control what goes inside the json from the java object and you can also control the names of the json properties. But more about that later. The example below simply takes a java object and spits out a json string.
package
com.dharamart.json.gson;
import
com.google.gson.Gson;
/**
* Example Demonstrating java serialization to json. The example tries to create
* a subset of json at
* http://dharamart.org/api/get/albums.json?api_key=60
* BLHNQCAOUFPIBZ&limit=5 The Albums object contains a list of Dataset objects.
* Each Dataset object is an album
*
*/
public
class
dharamart2 {
public
static
void
main(String[] args) {
Albums albums =
new
Albums();
albums.setTitle(
"Example"
);
Dataset dataset =
new
Dataset();
dataset.setAlbum_title(
"album1"
);
albums.setDataset(
new
Dataset[] { dataset });
// create the gson object
Gson gson =
new
Gson();
// use the gson objected created to convert albums to json
// representation
System.out.println(gson.toJson(albums));
// prints
// {"title":"Example","dataset":[{"album_title":"album1"}]}
}
}
Albums class
package
com.dharamart.json.gson;
public
class
Albums {
private
String title;
private
Dataset[] dataset;
public
void
setTitle(String title) {
this
.title = title;
}
public
void
setDataset(Dataset[] dataset) {
this
.dataset = dataset;
}
public
String getTitle() {
return
title;
}
public
Dataset[] getDataset() {
return
dataset;
}
}
Dataset class
package
com.dharamart.json.gson;
import
java.util.HashMap;
import
java.util.Map;
public
class
Dataset {
private
String album_id;
private
String album_title;
public
String getAlbum_id() {
return
album_id;
}
public
void
setAlbum_id(String album_id) {
this
.album_id = album_id;
}
public
String getAlbum_title() {
return
album_title;
}
public
void
setAlbum_title(String album_title) {
this
.album_title = album_title;
}
}
Building a json from Primitive types and String
If you dont want to build json from a java object but want to build it from scratch then look at the example below. In this example we also show some of the features that can be configured using the GsonBuilder class. Chiefly, the class shows the following:
- Serializing Null- By Default, nulls are not serialized, however that can be changed by setting serializeNulls() in the GsonBuilder.
- FieldNamingPolicy-Field names can be specified to follow a particular strategy. i.e. UPPER_CAMEL_CASE, UPPER_CAMEL_CASE_WITH_SPACES, LOWER_CASE_WITH_UNDERSCORES, LOWER_CASE_WITH_DASHES.
- Pretty Printing - For better visual appearance the json can be printed with proper indentation. This should not be used when sending data.
package
com.dharamart.json.gson;
import
com.google.gson.FieldNamingPolicy;
import
com.google.gson.Gson;
import
com.google.gson.GsonBuilder;
import
com.google.gson.JsonArray;
import
com.google.gson.JsonObject;
public
class
CreateJson {
public
static
void
main(String[] args) {
// create the albums object
JsonObject albums =
new
JsonObject();
// add a property calle title to the albums object
albums.addProperty(
"title"
,
"album1"
);
// create an array called datasets
JsonArray datasets =
new
JsonArray();
// create a dataset
JsonObject dataset =
new
JsonObject();
// add the property album_id to the dataset
dataset.addProperty(
"album_id"
,
1
);
// add the property album_year to the dataset
dataset.addProperty(
"album_year"
,
1996
);
datasets.add(dataset);
albums.add(
"dataset"
, datasets);
// create the gson using the GsonBuilder. Set pretty printing on. Allow
// serializing null and set all fields to the Upper Camel Case
Gson gson =
new
GsonBuilder().setPrettyPrinting().serializeNulls().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
System.out.println(gson.toJson(albums));
/* prints
{
"title": "album1",
"dataset": [
{
"album_id": 1,
"album_year": 1996
}
]
}
*/
}
}
No comments:
Post a Comment
Dharamart.blogspot.in