gcube-sdi-suite/gcube-geoserver-client/src/main/java/org/gcube/spatial/data/clients/geoserver/SerializationUtils.java

65 lines
1.8 KiB
Java

package org.gcube.spatial.data.clients.geoserver;
import java.io.IOException;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.DeserializationFeature;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.SerializationFeature;
import org.gcube.spatial.data.clients.geoserver.model.FeatureTypeInfo;
import org.json.simple.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SerializationUtils {
static ObjectMapper mapper=new ObjectMapper();
static Configuration JSON_PATH_ALWAYS_LIST_CONFIG=
Configuration.builder().options(Option.ALWAYS_RETURN_LIST,Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();
static{
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
}
public static final <T> T fromJSONString(String json,Class<T> clazz) throws IOException{
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
if(clazz.getPackage().equals(FeatureTypeInfo.class.getPackage())) {
log.debug("Unable to read normally, trying adjusting object. JSON IS {}",json,e);
if(clazz.isAssignableFrom(FeatureTypeInfo.class)) {
DocumentContext ctx=
JSONObject obj=mapper.readValue(json,JSONObject.class);
obj.get("attributes")
}
}
else throw e;
}
}
public static final String toJSONString(Object obj) throws JsonProcessingException {
return mapper.writeValueAsString(obj);
}
public static DocumentContext parseContext(String json) {
return JsonPath.using(JSON_PATH_ALWAYS_LIST_CONFIG).parse(json);
}
}