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

46 lines
1.3 KiB
Java
Raw Normal View History

2020-11-18 18:06:38 +01:00
package org.gcube.application.geoportal.service.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
2021-09-02 18:05:59 +02:00
import com.fasterxml.jackson.databind.SerializationFeature;
2020-11-18 18:06:38 +01:00
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
2021-08-06 18:33:34 +02:00
import java.util.Iterator;
2020-11-18 18:06:38 +01:00
public class Serialization {
2020-12-18 18:24:20 +01:00
public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
2020-11-18 18:06:38 +01:00
public static ObjectMapper mapper;
static {
mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
2021-09-02 18:05:59 +02:00
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.registerModule(new JavaTimeModule());
2020-11-18 18:06:38 +01:00
}
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
return mapper.readerFor(clazz).readValue(jsonString);
}
2021-08-06 18:33:34 +02:00
public static <T> Iterator<T> readCollection(String jsonString, Class<T> clazz) throws IOException {
return mapper.readerFor(clazz).readValues(jsonString);
}
2020-11-19 17:38:56 +01:00
public static String write(Object toWrite) throws JsonProcessingException {
2020-11-26 16:08:41 +01:00
String toReturn= mapper.writeValueAsString(toWrite);
return toReturn;
}
2021-09-02 18:05:59 +02:00
2020-11-18 18:06:38 +01:00
}