Skip to content

Instantly share code, notes, and snippets.

@mkjsix
Created October 20, 2016 13:09
Show Gist options
  • Save mkjsix/601c1d51f0e6d1816ac53526cf249159 to your computer and use it in GitHub Desktop.
Save mkjsix/601c1d51f0e6d1816ac53526cf249159 to your computer and use it in GitHub Desktop.
JSONizable is a Java interface which uses Gson to serialize Java objects to JSON and viceversa
package com.softinstigate.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
* @author mturatti
*/
public interface JSONizable {
default String toJson(boolean prettyPrinting) {
Gson gson = prettyPrinting
? new GsonBuilder().setPrettyPrinting().create()
: new GsonBuilder().create();
return gson.toJson(this);
}
static <T> T fromJson(String json, Class<T> T) {
Gson gson = new Gson();
T object = gson.fromJson(json, T);
return object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment