JSON is a data format that works well with JavaScript applications and has become the default choice while working with REST Data Services. In this example we will show how to convert a json to a java object using GSON and jackson , two popular open source libraries. In addition we will show how to convert json to an arraylist and array.
In the set up method we will create a java object called Plane and create constants that will be used when validating each library conversation from java to json.
Setup static class Plane {
private String planeType ;
public Plane () {
}
public String getPlaneType () {
return planeType ;
}
@Override
public String toString () {
return Objects . toStringHelper ( this ). add ( "plane type: " , planeType ). toString ();
}
}
private final String ARRAY_AS_JSON = "[{\"planeType\":\"Commercial Transport Planes\"},{\"planeType\":\"General Aviation Planes\"},{\"planeType\":\"Military Planes\"},{\"planeType\":\"Sea Planes\"},{\"planeType\":\"Special Purpose Planes\"}]" ;
private static final String JSON_OBJECT = "{\"planeType\":\"Sea Planes\"}" ;
GSON JSON to object @Test
public void unmarshall_json_to_object_with_gson () {
Gson gson = new Gson ();
Plane plane = gson . fromJson ( JSON_OBJECT , Plane . class );
logger . info ( plane );
assertEquals ( "Sea Planes" , plane . getPlaneType ());
}
Output
Plane { plane type: = Sea Planes }
JSON to list @Test
public void unmarshall_json_to_list_with_gson () {
Gson gson = new Gson ();
@SuppressWarnings ( "serial" )
List < Plane > planes = gson . fromJson ( ARRAY_AS_JSON , new TypeToken < List < Plane >>(){}. getType ());
logger . info ( planes );
assertEquals ( 5 , planes . size ());
}
Output
[
Plane { plane type: = Commercial Transport Planes },
Plane { plane type: = General Aviation Planes },
Plane { plane type: = Military Planes },
Plane { plane type: = Sea Planes },
Plane { plane type: = Special Purpose Planes }
]
JSON to array @Test
public void unmarshall_json_to_java_array_with_gson () {
Gson gson = new Gson ();
Plane [] planes = gson . fromJson ( ARRAY_AS_JSON , Plane []. class );
logger . info ( planes );
assertEquals ( 5 , planes . length );
}
Output
[
Plane { plane type: = Commercial Transport Planes },
Plane { plane type: = General Aviation Planes },
Plane { plane type: = Military Planes },
Plane { plane type: = Sea Planes },
Plane { plane type: = Special Purpose Planes }
]
Jackson JSON to object @Test
public void unmarshall_json_to_object_with_jackson () throws JsonParseException , JsonMappingException , IOException {
ObjectMapper objectMapper = new ObjectMapper ();
Plane plane = objectMapper . readValue ( JSON_OBJECT , Plane . class );
logger . info ( plane );
assertEquals ( "Sea Planes" , plane . getPlaneType ());
}
Output
Plane { plane type: = Sea Planes }
JSON to list @Test
public void unmarshall_json_to_list_with_jackson () throws JsonParseException , JsonMappingException , IOException {
ObjectMapper objectMapper = new ObjectMapper ();
List < Plane > planes = objectMapper . readValue ( ARRAY_AS_JSON ,
objectMapper . getTypeFactory (). constructCollectionType ( List . class , Plane . class ));
logger . info ( planes );
assertEquals ( 5 , planes . size ());
}
Output
[
Plane { plane type: = Commercial Transport Planes },
Plane { plane type: = General Aviation Planes },
Plane { plane type: = Military Planes },
Plane { plane type: = Sea Planes },
Plane { plane type: = Special Purpose Planes }
]
JSON to array @Test
public void unmarshall_json_to_java_array_with_jackson () throws JsonParseException , JsonMappingException , IOException {
ObjectMapper objectMapper = new ObjectMapper ();
Plane [] planes = objectMapper . readValue ( ARRAY_AS_JSON , JsontoJavaObject . Plane []. class );
logger . info ( Arrays . toString ( planes ));
assertEquals ( 5 , planes . length );
}
Output
[
Plane { plane type: = Commercial Transport Planes },
Plane { plane type: = General Aviation Planes },
Plane { plane type: = Military Planes },
Plane { plane type: = Sea Planes },
Plane { plane type: = Special Purpose Planes }
]
JSON to java object posted by Justin Musgrove on 03 September 2013
Tagged: java and java-json
Share on: Facebook Google+
All the code on this page is available on github:
JsontoJavaObject.java