Created
May 18, 2017 16:29
-
-
Save francisnnumbi/38ebf1d13c41143fef4ea23859f01521 to your computer and use it in GitHub Desktop.
simplifying gson handling
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 fnn.smirl.simple; | |
import java.io.*; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonIOException; | |
import com.google.gson.JsonSyntaxException; | |
/** | |
* Simplified serialization and deserialization of file using Gson API | |
* | |
* @author Francis Nduba Numbi | |
* @version 1.1 | |
* @since 2017 | |
*/ | |
public class Serializer { | |
private Gson gson; | |
/** | |
* Default constructor | |
*/ | |
public Serializer() { | |
gson = new GsonBuilder() | |
.enableComplexMapKeySerialization() | |
.serializeNulls() | |
.setPrettyPrinting().create(); | |
} | |
/** | |
* | |
* @param builder | |
*/ | |
public Serializer(GsonBuilder builder) { | |
gson = builder.create(); | |
} | |
/** | |
* | |
* @param gson | |
*/ | |
public Serializer(Gson gson) { | |
this.gson = gson; | |
} | |
/** | |
* get the builder in order to add more functionnalities or | |
* | |
* @param builder new builder to which this Serializer will be attached | |
*/ | |
public void setBuilder(GsonBuilder builder) { | |
gson = builder.create(); | |
} | |
/** | |
* | |
* @param <T> | |
* @param file | |
* @param objectTString | |
* @param classOfT | |
* @return boolean if the serialization was successful or not | |
*/ | |
public <T> boolean serialize(File file, String objectTString, Class<T> classOfT) { | |
return serialize(file.getAbsolutePath(), objectTString, classOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param filename | |
* @param objectTString | |
* @param classOfT | |
* @return | |
*/ | |
public <T> boolean serialize(String filename, String objectTString, Class<T> classOfT) { | |
T p = gson.fromJson(objectTString, classOfT); | |
return serialize(filename, p, classOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param file | |
* @param objectOfT | |
* @param classOfT | |
* @return | |
*/ | |
public <T> boolean serialize(File file, T objectOfT, Class<T> classOfT) { | |
return serialize(file.getAbsolutePath(), objectOfT, classOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param filename | |
* @param objectOfT | |
* @param classOfT | |
* @return | |
*/ | |
public <T> boolean serialize(String filename, T objectOfT, Class<T> classOfT) { | |
FileWriter w; | |
try { | |
w = new FileWriter(filename); | |
w.write(gson.toJson(objectOfT)); | |
w.close(); | |
return true; | |
} catch (IOException ioe) { | |
return false; | |
} | |
} | |
/** | |
* | |
* @param <T> | |
* @param file | |
* @param classOfT | |
* @return | |
*/ | |
public <T> String deserializeToString(File file, Class<T> classOfT) { | |
return deserializeToString(file.getAbsolutePath(), classOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param filename | |
* @param classOfT | |
* @return | |
*/ | |
public <T> String deserializeToString(String filename, Class<T> classOfT) { | |
T obj = deserialize(filename, classOfT); | |
return gson.toJson(obj); | |
} | |
/** | |
* | |
* @param <T> | |
* @param file | |
* @param classOfT | |
* @return | |
*/ | |
public <T> T deserialize(File file, Class<T> classOfT) { | |
return deserialize(file.getAbsolutePath(), classOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param filename | |
* @param classOfT | |
* @return | |
*/ | |
public <T> T deserialize(String filename, Class<T> classOfT) { | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader(filename)); | |
return gson.fromJson(reader, classOfT); | |
} catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) { | |
return null; | |
} | |
} | |
/** | |
* | |
* @param <T> | |
* @param objectOfT | |
* @return | |
*/ | |
public <T> String toJson(T objectOfT) { | |
return gson.toJson(objectOfT); | |
} | |
/** | |
* | |
* @param <T> | |
* @param objectTString | |
* @param classOfT | |
* @return | |
*/ | |
public <T> T fromJson(String objectTString, Class<T> classOfT) { | |
return gson.fromJson(objectTString, classOfT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment