2019-10-04 15:55:45 -04:00
|
|
|
package com.chameleonvision.util;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-11-12 19:28:46 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
|
|
|
|
|
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
|
|
|
|
|
import java.io.File;
|
2019-09-19 14:07:42 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
|
|
public class FileHelper {
|
|
|
|
|
private FileHelper() {} // no construction, utility class
|
|
|
|
|
|
|
|
|
|
public static void CheckPath(String path) {
|
|
|
|
|
if (path.equals("")) return;
|
|
|
|
|
Path realPath = Path.of(path);
|
|
|
|
|
CheckPath(realPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CheckPath(Path path) {
|
|
|
|
|
if (!Files.exists(path)) {
|
|
|
|
|
try {
|
|
|
|
|
Files.createDirectories(path);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-19 12:43:38 -05:00
|
|
|
|
2019-11-21 05:32:19 -05:00
|
|
|
public static void Serializer(Path path, Object object) throws IOException {
|
2019-11-12 19:28:46 +02:00
|
|
|
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfBaseType(Object.class).build();
|
|
|
|
|
ObjectMapper objectMapper = JsonMapper.builder().activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT).build();
|
2019-11-21 05:32:19 -05:00
|
|
|
objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File(path.toString()), object);
|
2019-11-12 19:28:46 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-19 12:43:38 -05:00
|
|
|
public static <T> T DeSerializer(Path path, Class<T> ref) throws IOException {
|
2019-11-21 05:32:19 -05:00
|
|
|
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfBaseType(ref).build();
|
|
|
|
|
ObjectMapper objectMapper = JsonMapper.builder().activateDefaultTyping(ptv).build();
|
|
|
|
|
File jsonFile = new File(path.toString());
|
|
|
|
|
if (jsonFile.exists() && jsonFile.length() > 0) {
|
|
|
|
|
T readObject = objectMapper.readValue(jsonFile, ref);
|
|
|
|
|
return readObject;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2019-11-12 19:28:46 +02:00
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|