Created
October 20, 2016 13:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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