feat:完善游戏房状态维护
This commit is contained in:
@@ -7,16 +7,37 @@ import com.google.gson.Gson;
|
||||
*/
|
||||
public class JsonUtils {
|
||||
|
||||
public static Gson gson;
|
||||
private static Gson gson;
|
||||
|
||||
public static String toJson(Object object) {
|
||||
public static Gson getGson() {
|
||||
if (gson == null) {
|
||||
gson = new Gson();
|
||||
}
|
||||
return gson;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析json
|
||||
*
|
||||
* @return 如果解析出错,则返回空对象
|
||||
*/
|
||||
public static <T> T fromJson(final String json, final Class<T> type) {
|
||||
try {
|
||||
if (json == null || type == null) {
|
||||
return null;
|
||||
}
|
||||
return getGson().fromJson(json, type);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String toJson(Object object) {
|
||||
String result = null;
|
||||
try {
|
||||
if (object != null) {
|
||||
result = gson.toJson(object);
|
||||
result = getGson().toJson(object);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
|
Reference in New Issue
Block a user