
backtrace.io.helpers.BacktraceSerializeHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of backtrace-java Show documentation
Show all versions of backtrace-java Show documentation
Backtrace's integration with Java applications allows customers to capture and report handled and unhandled java exceptions.
The newest version!
package backtrace.io.helpers;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* Helper class for serialize and deserialize objects
*/
public class BacktraceSerializeHelper {
/**
* Serialize given object to JSON string
*
* @param object object which will be serialized
* @return serialized object in JSON string format
*/
public static String toJson(Object object) {
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
return gson.toJson(object);
}
/**
* Deserialize the specified Json into an object of the specified class
*
* @param the type of the desired object
* @param json the string from which the object is to be deserialized
* @param type the class of T
* @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}
* or if {@code json} is empty.
*/
public static T fromJson(String json, Class type) {
return new Gson().fromJson(json, type);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy