
com.crawljax.plugins.crawloverview.model.Serializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crawloverview-plugin Show documentation
Show all versions of crawloverview-plugin Show documentation
Generates an HTML report with a snapshot overview of what is crawled.
The newest version!
package com.crawljax.plugins.crawloverview.model;
import com.crawljax.core.plugin.Plugin;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.slf4j.LoggerFactory;
public class Serializer {
private static final ObjectMapper MAPPER;
static {
MAPPER = new ObjectMapper();
MAPPER.getSerializationConfig()
.getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE);
MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.getDefault()));
MAPPER.registerModule(new GuavaModule());
SimpleModule testModule = new SimpleModule("Plugin serialiezr");
testModule.addSerializer(new JsonSerializer() {
@Override
public void serialize(Plugin plugin, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeString(plugin.getClass().getSimpleName());
}
@Override
public Class handledType() {
return Plugin.class;
}
});
MAPPER.registerModule(testModule);
}
private Serializer() {}
/**
* Serialize the object JSON. When an error occures return a string with the given error.
*/
public static String toPrettyJson(Object o) {
try {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(o);
} catch (JsonProcessingException e) {
LoggerFactory.getLogger(Serializer.class)
.error(
"Could not serialize the object. This will be ignored and the error will be written instead. Object was {}",
o,
e);
return "\"" + e.getMessage() + "\"";
}
}
public static T deserialize(String value, TypeReference clasz) throws IOException {
return MAPPER.readValue(value, clasz);
}
public static OutPutModel read(String json) throws JsonParseException, JsonMappingException, IOException {
return MAPPER.readValue(json, OutPutModel.class);
}
public static OutPutModel read(File file) throws IOException {
return MAPPER.readValue(file, OutPutModel.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy