mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-22 01:11:40 +00:00
added json serializer and de serializer
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package com.chameleonvision.util;
|
||||
|
||||
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;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -22,4 +27,18 @@ public class FileHelper {
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user