Код: Выделить всё
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
public class JsonHelper {
private static final ObjectMapper jsonMapper = jsonMapper();
private JsonHelper() {}
public static ObjectMapper jsonMapper() {
return JsonMapper
.builder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.addModule(new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
}
public static \ String writeAsString(T object) {
try {
return jsonMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new AssertionError("Error serializing object: " + e.getMessage(), e);
}
}
public static \ T readFromJson(String json, Class\ clazz) {
try {
return jsonMapper.readValue(json, clazz);
} catch (IOException e) {
throw new AssertionError("Error reading value from json: " + e.getMessage(), e);
}
}
}
Код: Выделить всё
implementation 'tools.jackson.core:jackson-databind:3.1.3'
.....
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;
import java.io.IOException;
public class JsonHelper {
private static final ObjectMapper jsonMapper = jsonMapper();
private JsonHelper() {}
public static ObjectMapper jsonMapper() {
return JsonMapper
.builder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.addModule(new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
}
public static \ String writeAsString(T object) {
try {
return jsonMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new AssertionError("Error serializing object: " + e.getMessage(), e);
}
}
public static \ T readFromJson(String json, Class\ clazz) {
try {
return jsonMapper.readValue(json, clazz);
} catch (IOException e) {
throw new AssertionError("Error reading value from json: " + e.getMessage(), e);
}
}
}
Код: Выделить всё
Cannot resolve method 'serializationInclusion' in 'Builder'
Cannot resolve symbol 'JavaTimeModule'
Cannot resolve symbol 'WRITE_DATES_AS_TIMESTAMPS'