com.xlrit.gears.base.util.JsonUtils Maven / Gradle / Ivy
package com.xlrit.gears.base.util;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static final ObjectMapper mapper = new ObjectMapper();
public static boolean isNullNode(JsonNode node) {
return node == null || node.isNull();
}
public static boolean isAbsentValue(JsonNode node) {
return node == null
|| node.isNull()
|| node.isMissingNode()
|| node.isTextual() && node.asText().isEmpty();
}
public static String jsonAsText(JsonNode value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
return value.asText();
}
}
public static String jsonAsPretty(JsonNode value) {
try {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value);
} catch (JsonProcessingException e) {
return jsonAsText(value);
}
}
public static Stream stream(JsonNode arrayNode) {
return StreamSupport.stream(arrayNode.spliterator(), false);
}
}