Objects to JSON and Back in Java

When using the Parse.com service for saving data in your application to the cloud, you may run into the problem of not being able to save objects of a class inside your Parse object. This could be solved using Relational Data, by linking Parse objects using their objectID, however we are going to focus on using JSON objects which is supported and written about in the Parse documentation.

In this quick tutorial we will be using Java as our programming language, but it is also possible to do this in any of the other programming languages supported by Parse.com, such as Javascript, Objective-C, or C#.

We will be creating a List of items. To do this we need to make an ItemDataModel. Each object will be created from this class.

public class ItemDataModel {

}

In the ItemDataModel.class we will create getters and setters for completed, title, description and dueDate.

private boolean completed;
private String title;
private String description;
private Date dueDate;

public ItemDataModel(){

}

public boolean isCompleted(){
    return this.completed;
}
public void setCompleted(boolean newCompleted){
    this.completed = newCompleted;
}

public String getTitle(){
    return this.title;
}
public void setTitle(String newTitle){
    this.title = newTitle;
}

public String getDescription(){
    return this.description;
}
public void setDescription(String newDescription){
    this.description = newDescription;
}

public Date getDueDate(){
    return dueDate;
}
public void setDueDate(Date newDueDate){
    this.dueDate = newDueDate;
}

We will be using the Google-GSON library, so make sure to include these imports:

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.json.JSONObject;

Now that we have everything set up, lets have a look at our method that will convert a List<ItemDataModel> to a JSON String.

public void ToGsonAndBack() throws JsonSyntaxException, IOException {

    Gson gson = new Gson();

    //Create List of items.
    List<ItemDataModel> items = new ArrayList();

    ItemDataModel item1 = new ItemDataModel();
    item1.setTitle("Some item 1.");
    item1.setDescription("Some item description.");
    items.add(item1);

    ItemDataModel item2 = new ItemDataModel();
    item2.setTitle("Some item 2.");
    items.add(item2);

    //Convert List of items to json string.
    String jsonItems = gson.toJson(items);
    Log.d("USERLOG", jsonItems);

    // create the type for the collection. In this case define that the collection is of type ItemDataModel
    Type collectionType = new TypeToken<Collection<ItemDataModel>>() {}.getType();
    List<ItemDataModel> itemsParsed = gson.fromJson(jsonItems, collectionType);
    for (ItemDataModel item : itemsParsed) {
        Log.d("USERLOG", "Title: " + item.getTitle());
        Log.d("USERLOG", "Description: " + item.getDescription());
    }
}

In our Parse.com object we can save the JSON string, and when we retrieve it we can convert it back into our List<ItemDataModel>.

ParseObject someObject = new ParseObject("someObject");
someObject.put("jsonItems", jsonItems);
someObject.saveInBackground();

That's it!