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-12 19:28:46 +02:00
|
|
|
public static void Serializer(Object object, Path path) throws IOException {
|
|
|
|
|
File file = new File(path.toString());
|
|
|
|
|
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfBaseType(Object.class).build();
|
|
|
|
|
ObjectMapper objectMapper = JsonMapper.builder().activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT).build();
|
|
|
|
|
objectMapper.writeValue(file,object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Object DeSerializer(Path path,Class<?> ref) throws IOException {
|
|
|
|
|
File file = new File(path.toString());
|
|
|
|
|
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder().allowIfBaseType(Object.class).build();
|
|
|
|
|
ObjectMapper objectMapper = JsonMapper.builder().activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT).build();
|
|
|
|
|
Object o = objectMapper.readValue(file, ref);
|
|
|
|
|
return o;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|