промежуточный. частичный рефакторинг с прицелом на библиотечную часть
This commit is contained in:
26
src/Common/Utils/CommonUtils.java
Normal file
26
src/Common/Utils/CommonUtils.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package Common.Utils;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
public class CommonUtils {
|
||||
public static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
|
||||
/*
|
||||
public static String jsonToPrettyFormat(String packed) {
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonObject json = parser.parse(packed).getAsJsonObject();
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
return gson.toJson(json);
|
||||
}
|
||||
*/
|
||||
public static <T> T jsonFromFile(File file, Class<T> json_class) throws Exception {
|
||||
return gson.fromJson(FileUtils.readFileToString(file, Charset.defaultCharset()), json_class);
|
||||
}
|
||||
public static void jsonToFile(Object json_object, File file) throws Exception {
|
||||
FileUtils.writeStringToFile(file, gson.toJson(json_object));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user