All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.base.util.JsonUtils Maven / Gradle / Ivy

There is a newer version: 1.17.5
Show newest version
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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy