We saw how to parse JSON using GSON API, in this post we would take a look at how the same GSON API can be used to create JSON from the Java objects. Please read this to get an idea of what GSON is. Diving right into the topic of this example, we would make use of the toJson method of com.google.gson.Gson class. One of the overloaded versions of toJson() takes in an source of type Object and the type information which is of class java.lang.reflect.Type.
also read:
- JSON Tutorials
- Java EE Tutorials
- Design Patterns Tutorials
- Java File IO Tutorials
Lets define the model which would hold the data:
class Book{ @SerializedName("isbn") String isbn; @SerializedName("title") String title; @SerializedName("publication") String publication; @SerializedName("authors") String authors; public Book ( String isbn, String title, String publication, String authors) { this.isbn = isbn; this.title = title; this.publication = publication; this.authors = authors; } }
We populate a list with some values for this model bean and this list would be converted to JSON output. Note the use of @SerializedName annotation in the model class. This indicates the name used for the attribute in the json output. We populate the list of books using:
private static List<Book> populateBooks(){ List<Book> myBooks = new ArrayList<Book>(); Book book = new Book("007163360X", "Java Programming", "McGraw-Hill Osborne", "Dr (Poornachandra) Sarang"); myBooks.add(book); book = new Book("1849516626", "Apache Tomcat 7 Essentials", "Tanuj Khare", "Packt Press"); myBooks.add(book); book = new Book("007179431X", "Web Developer's Cookbook", "Robin Nixon", "McGraw-Hill Osborne"); myBooks.add(book); book = new Book("9781935182962", "Practical Unit Testing", "Ken Rimple & Srini Penchikala", "Manning"); myBooks.add(book); return myBooks; }
Once we have a list, we need to serialize it to JSON, for that we need to setup a Type information for generic List, this is not possible out of the box in Java and hence we would make use of com.google.gson.reflect.TypeToken class in the GSON API, to create the type information like:
// Get the type information for the List<Book> Type listType = new TypeToken<List<Book>>(){}.getType();
Next up is to use this type information along with the list of books and pass it to the toJson method to obtain the JSON output.
The complete code:
import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class JsonCreatorDemo { /** * Populate the Books information into a List * @return */ private static List<Book> populateBooks(){ List<Book> myBooks = new ArrayList<Book>(); Book book = new Book("007163360X", "Java Programming", "McGraw-Hill Osborne", "Dr (Poornachandra) Sarang"); myBooks.add(book); book = new Book("1849516626", "Apache Tomcat 7 Essentials", "Tanuj Khare", "Packt Press"); myBooks.add(book); book = new Book("007179431X", "Web Developer's Cookbook", "Robin Nixon", "McGraw-Hill Osborne"); myBooks.add(book); book = new Book("9781935182962", "Practical Unit Testing", "Ken Rimple & Srini Penchikala", "Manning"); myBooks.add(book); return myBooks; } public static void main(String [] args){ List<Book> myBooks = populateBooks(); Gson myGson = new Gson(); // Get the type information for the List<Book> Type listType = new TypeToken<List<Book>>(){}.getType(); // Serialize the List of books into JSON System.out.println(myGson.toJson(myBooks,listType)); } } class Book{ @SerializedName("isbn") String isbn; @SerializedName("title") String title; @SerializedName("publication") String publication; @SerializedName("authors") String authors; public Book ( String isbn, String title, String publication, String authors) { this.isbn = isbn; this.title = title; this.publication = publication; this.authors = authors; } }
JSON Output:
[ { "isbn":"007163360X", "title":"Java Programming", "publication":"McGraw-Hill Osborne", "authors":"Dr (Poornachandra) Sarang" }, { "isbn":"1849516626", "title":"Apache Tomcat 7 Essentials", "publication":"Tanuj Khare", "authors":"Packt Press" }, { "isbn":"007179431X", "title":"Web Developeru0027s Cookbook", "publication":"Robin Nixon", "authors":"McGraw-Hill Osborne" }, { "isbn":"9781935182962", "title":"Practical Unit Testing", "publication":"Ken Rimple u0026 Srini Penchikala", "authors":"Manning" } ]