package com.kyexpress.dsp.common.utils;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public final class JsonNodeUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(JsonNodeUtils.class);
private static ObjectMapper objectMapper = new ObjectMapper();
public static final String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
public static final FastDateFormat DATE_TIME_FORMAT = FastDateFormat.getInstance(DATE_TIME_FORMAT_PATTERN);
public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance(DATE_FORMAT_PATTERN);
static{
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
public static void setObjectMapper(final ObjectMapper objectMapper) {
JsonNodeUtils.objectMapper = objectMapper;
}
public static String writeValueAsString(final Object value) {
if (value == null) {
return null;
}
try {
return objectMapper.writeValueAsString(value);
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
return null;
}
public static <T> T readValue(final JsonNode jsonNode, final Class<T> valueType) {
if (jsonNode == null) {
return null;
}
try {
return objectMapper.readValue(jsonNode.traverse(), valueType);
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
return null;
}
public static JsonNode readTree(final String content) {
if (StringUtils.isEmpty(content)) {
return NullNode.getInstance();
}
try {
return objectMapper.readTree(content);
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
return NullNode.getInstance();
}
public static JsonNode readTree(final File file) {
if (file == null || !file.isFile()) {
return NullNode.getInstance();
}
try {
return objectMapper.readTree(file);
} catch (IOException e) {
LOGGER.warn(e.getMessage(), e);
}
return NullNode.getInstance();
}
public static boolean isArray(final JsonNode jsonNode) {
if (jsonNode == null) {
return false;
}
return jsonNode.isArray();
}
public static boolean isNotArray(final JsonNode jsonNode) {
return !isArray(jsonNode);
}
public static boolean isObject(final JsonNode jsonNode) {
if (jsonNode == null) {
return false;
}
return jsonNode.isObject();
}
public static boolean isNotObject(final JsonNode jsonNode) {
return !isObject(jsonNode);
}
public static boolean isNull(final JsonNode jsonNode) {
if (jsonNode == null) {
return true;
}
return jsonNode.isNull();
}
public static boolean isNotNull(final JsonNode jsonNode) {
return !isNull(jsonNode);
}
public static long asLong(final JsonNode data, final String key) {
return asLong(data, key, 0L);
}
public static long asLong(final JsonNode data, final String key, final long defaultValue) {
if (data == null) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
return jsonNode.asLong(defaultValue);
}
public static Long asLong(final JsonNode data, final String key, final Long defaultValue) {
if (data == null) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
if (jsonNode instanceof NumericNode) {
return Long.valueOf(jsonNode.asLong());
}
if (jsonNode instanceof BooleanNode) {
return Long.valueOf(jsonNode.asLong());
}
try {
return Long.valueOf(new BigDecimal(jsonNode.asText()).longValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static String asText(final JsonNode data) {
if (isNull(data)) {
return "";
}
return data.asText("");
}
public static String asText(final JsonNode data, final String key) {
return asText(data, key, "");
}
public static String asText(final JsonNode data, final String key, final String defaultValue) {
if (data == null) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
return jsonNode.asText(defaultValue);
}
public static int asInt(final JsonNode data, final String key) {
return asInt(data, key, 0);
}
public static int asInt(final JsonNode data, final String key, final int defaultValue) {
if (data == null) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
return jsonNode.asInt(defaultValue);
}
public static Integer asInt(final JsonNode data, final String key, final Integer defaultValue) {
if (data == null) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
if (jsonNode instanceof NumericNode) {
return Integer.valueOf(jsonNode.asInt());
}
if (jsonNode instanceof BooleanNode) {
return Integer.valueOf(jsonNode.asInt());
}
try {
return Integer.valueOf(new BigDecimal(jsonNode.asText()).intValue());
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Number asNumber(JsonNode data, String key, Number defaultValue) {
if (isNull(data)) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
final Number value = jsonNode.numberValue();
if (value != null) {
return value;
}
try {
return new BigDecimal(jsonNode.asText());
} catch (Exception e) {
return defaultValue;
}
}
public static Timestamp asTimestamp(final JsonNode data, final String key, final Timestamp defaultValue) {
if (isNull(data)) {
return defaultValue;
}
final JsonNode jsonNode = data.get(key);
if (isNull(jsonNode)) {
return defaultValue;
}
final Number numberValue = jsonNode.numberValue();
if (numberValue != null) {
return new Timestamp(numberValue.longValue());
}
final String textValue = StringUtils.trimToEmpty(jsonNode.asText());
if (NumberUtils.isDigits(textValue)) {
return new Timestamp(NumberUtils.toLong(textValue, 0));
}
if (textValue.length() == DATE_TIME_FORMAT_PATTERN.length()) {
try {
return new Timestamp(DATE_TIME_FORMAT.parse(textValue).getTime());
} catch (ParseException e) {
LOGGER.error(e.getMessage(), e);
}
}
if (textValue.length() == DATE_FORMAT_PATTERN.length()) {
try {
return new Timestamp(DATE_FORMAT.parse(textValue).getTime());
} catch (ParseException e) {
LOGGER.error(e.getMessage(), e);
}
}
return defaultValue;
}
public static JsonNode get(final JsonNode data, final String key) {
if (data == null) {
return NullNode.getInstance();
}
final JsonNode jsonNode = data.get(key);
if (jsonNode == null) {
return NullNode.getInstance();
}
return jsonNode;
}
public static JsonNode get(final JsonNode data, final int index) {
if (data == null) {
return NullNode.getInstance();
}
final JsonNode jsonNode = data.get(index);
if (jsonNode == null) {
return NullNode.getInstance();
}
return jsonNode;
}
public static ObjectNode putObject(final ObjectNode objectNode, final String fieldName) {
final JsonNode jsonNode = objectNode.get(fieldName);
if (jsonNode instanceof ObjectNode) {
return (ObjectNode) jsonNode;
}
return objectNode.putObject(fieldName);
}
public static ObjectNode putObject(final ArrayNode arrayNode, final int index) {
final JsonNode jsonNode = arrayNode.get(index);
if (jsonNode instanceof ObjectNode) {
return (ObjectNode) jsonNode;
}
while (arrayNode.size() <= index) {
arrayNode.addNull();
}
final ObjectNode objectNode = arrayNode.objectNode();
arrayNode.set(index, objectNode);
return objectNode;
}
public static ArrayNode putArray(final ObjectNode objectNode, final String fieldName) {
final JsonNode jsonNode = objectNode.get(fieldName);
if (jsonNode instanceof ArrayNode) {
return (ArrayNode) jsonNode;
}
return objectNode.putArray(fieldName);
}
private JsonNodeUtils() {
}
}