This repository has been archived on 2021-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
geoportal-service/src/main/java/org/gcube/application/geoportal/service/utils/Serialization.java

44 lines
1.3 KiB
Java

package org.gcube.application.geoportal.service.utils;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class Serialization {
public static ObjectMapper mapper;
static {
mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.registerModule(new JavaTimeModule());
}
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
return mapper.readerFor(clazz).readValue(jsonString);
}
public static String write(Object toWrite) throws JsonProcessingException {
return mapper.writeValueAsString(toWrite);
}
// As DB String
public static String asString(Collection<?> coll) {
if(coll==null||coll.isEmpty()) return "";
StringBuilder builder=new StringBuilder();
for(Object t : coll) {
builder.append(t.toString() +",");
}
return builder.substring(0, builder.lastIndexOf(","));
}
public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
}