Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.iris_events.asyncapi.runtime.io;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.iris_events.asyncapi.runtime.json.IrisObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Utilities methods for reading information from a Json Tree.
*
* @author [email protected]
*/
public final class JsonUtil {
private static final JsonNodeFactory factory = JsonNodeFactory.instance;
public static ObjectNode objectNode() {
return factory.objectNode();
}
public static ArrayNode arrayNode() {
return factory.arrayNode();
}
/**
* Constructor.
*/
private JsonUtil() {
}
/**
* Extract a string property from the given json tree. Returns null if no
* property exists or is not a text node.
*
* @param node JsonNode
* @param propertyName Property to extract
* @return String holding the value found for the property
*/
public static String stringProperty(JsonNode node, String propertyName) {
JsonNode propertyNode = node.get(propertyName);
if (propertyNode != null) {
return propertyNode.asText();
}
return null;
}
/**
* Sets the value of a property for a given json node. If the value is null,
* then the property is not written.
*
* @param node ObjectNode
* @param propertyName Property to be set
* @param propertyValue Value to be set
*/
public static void stringProperty(ObjectNode node, String propertyName, String propertyValue) {
if (propertyValue == null) {
return;
}
node.set(propertyName, factory.textNode(propertyValue));
}
/**
* Sets the value of a property for a given json node. If the value is null,
* then the property is not written.
*
* @param node ObjectNode
* @param propertyName Property to be set
* @param propertyValue Value to be set
* @param Type of the property value
*/
public static > void enumProperty(ObjectNode node, String propertyName, E propertyValue) {
if (propertyValue == null) {
return;
}
node.set(propertyName, factory.textNode(propertyValue.toString()));
}
/**
* Extract a boolean property from the given json tree. Returns null if no
* property exists or is not a boolean node.
*
* @param node JsonNode
* @param propertyName Property to extract
* @return Boolean containing the value extracted
*/
public static Optional booleanProperty(JsonNode node, String propertyName) {
JsonNode propertyNode = node.get(propertyName);
if (propertyNode != null) {
return Optional.of(propertyNode.asBoolean());
}
return Optional.empty();
}
/**
* Sets the value of a property for a given json node. If the value is null,
* then the property is not written.
*
* @param node ObjectNode
* @param propertyName Property to be set
* @param propertyValue Boolean value to be set
*/
public static void booleanProperty(ObjectNode node, String propertyName, Boolean propertyValue) {
if (propertyValue == null) {
return;
}
node.set(propertyName, factory.booleanNode(propertyValue));
}
/**
* Extract a integer property from the given json tree. Returns null if no
* property exists or is not a boolean node.
*
* @param node JsonNode
* @param propertyName Property to extract
* @return Integer containing the extracted value
*/
public static Integer intProperty(JsonNode node, String propertyName) {
JsonNode propertyNode = node.get(propertyName);
if (propertyNode != null) {
return propertyNode.asInt();
}
return null;
}
/**
* Sets the value of a property for a given json node. If the value is null,
* then the property is not written.
*
* @param node ObjectNode
* @param propertyName Property to be set
* @param propertyValue Integer value to be set
*/
public static void intProperty(ObjectNode node, String propertyName, Integer propertyValue) {
if (propertyValue == null) {
return;
}
node.set(propertyName, factory.numberNode(propertyValue));
}
/**
* Extract a BigDecimal property from the given json tree. Returns null if no
* property exists or is not a boolean node.
*
* @param node JsonNode
* @param propertyName Property to extract
* @return BigDecimal containing the extracted value
*/
public static BigDecimal bigDecimalProperty(JsonNode node, String propertyName) {
JsonNode propertyNode = node.get(propertyName);
if (propertyNode != null) {
return new BigDecimal(propertyNode.asText());
}
return null;
}
/**
* Sets the value of a property for a given json node. If the value is null,
* then the property is not written.
*
* @param node ObjectNode
* @param propertyName Property to be set
* @param propertyValue BigDecimal value to be set
*/
public static void bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue) {
if (propertyValue == null) {
return;
}
if (isIntegerValue(propertyValue)) {
node.set(propertyName, factory.numberNode(propertyValue.toBigInteger()));
} else {
node.set(propertyName, factory.numberNode(propertyValue));
}
}
private static boolean isIntegerValue(BigDecimal bd) {
return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0;
}
/**
* Reads the node as a Java object.This is typically expected to be a literal of
* some sort, as in the case of default values and examples. The node may be anything
* from a string to a javascript object.
*
* @param node the json node
* @return a java object
*/
public static Object readObject(JsonNode node) {
if (node == null) {
return null;
}
if (node.isBigDecimal()) {
return new BigDecimal(node.asText());
}
if (node.isBigInteger()) {
return new BigInteger(node.asText());
}
if (node.isBoolean()) {
return node.asBoolean();
}
if (node.isDouble()) {
return node.asDouble();
}
if (node.isFloat()) {
return node.asDouble();
}
if (node.isInt()) {
return node.asInt();
}
if (node.isLong()) {
return node.asLong();
}
if (node.isTextual()) {
return node.asText();
}
if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
List