import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
public class JsonUtil {
private static final ObjectMapper objectMapper = configureObjectMapper();
private static ObjectMapper configureObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
mapper.registerModule(javaTimeModule);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
SimpleModule bigNumberModule = new SimpleModule();
bigNumberModule.addSerializer(java.math.BigInteger.class, ToStringSerializer.instance);
bigNumberModule.addSerializer(java.math.BigDecimal.class, ToStringSerializer.instance);
mapper.registerModule(bigNumberModule);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
public static String toJson(Object object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
}
public static <T> T fromJson(String json, Class<T> valueType) throws JsonProcessingException {
return objectMapper.readValue(json, valueType);
}
public static <T> T fromJson(String json, TypeReference<T> valueTypeRef) throws JsonProcessingException {
return objectMapper.readValue(json, valueTypeRef);
}
public static String toPrettyJson(Object object) throws JsonProcessingException {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
}
public static JsonNode jsonNodeOf(String json) throws JsonProcessingException {
return objectMapper.readTree(json);
}
public static <T> T readValueAtPath(String json, String path, Class<T> valueType) throws JsonProcessingException {
JsonNode jsonNode = objectMapper.readTree(json);
JsonNode nodeAtPath = jsonNode.at(path);
return objectMapper.treeToValue(nodeAtPath, valueType);
}
public static <T> List<T> fromJsonToList(String json, Class<T> valueType) throws JsonProcessingException {
return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, valueType));
}
public static <K, V> Map<K, V> fromJsonToMap(String json, Class<K> keyType, Class<V> valueType) throws JsonProcessingException {
return objectMapper.readValue(json, objectMapper.getTypeFactory().constructMapType(Map.class, keyType, valueType));
}
public static <T> T[] fromJsonToArray(String json, Class<T[]> valueType) throws JsonProcessingException {
return objectMapper.readValue(json, valueType);
}
}