com.aeontronix.enhancedmule.tools.util.JsonHelper Maven / Gradle / Ivy
/*
* Copyright (c) Aeontronix 2019
*/
package com.aeontronix.enhancedmule.tools.util;
import com.aeontronix.commons.StringUtils;
import com.aeontronix.enhancedmule.tools.anypoint.AnypointClient;
import com.aeontronix.enhancedmule.tools.anypoint.AnypointObject;
import com.aeontronix.enhancedmule.tools.anypoint.InvalidJsonException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
public class JsonHelper implements Serializable {
private ObjectMapper jsonMapper = createMapper();
private AnypointClient client;
public JsonHelper() {
}
public JsonHelper(AnypointClient client) {
this.client = client;
}
public static void processVariables(ObjectNode json, HashMap vars) {
LinkedList nodes = new LinkedList<>();
nodes.add(json);
while (!nodes.isEmpty()) {
final JsonNode node = nodes.removeFirst();
if (node != null && !node.isNull()) {
if (node instanceof ObjectNode) {
final Iterator> fields = node.fields();
while (fields.hasNext()) {
Map.Entry field = fields.next();
final JsonNode n = field.getValue();
if (n instanceof TextNode) {
((ObjectNode) node).replace(field.getKey(), new TextNode(StringUtils.substituteVariables(n.textValue(), vars)));
} else if (n instanceof ArrayNode) {
for (JsonNode children : node) {
nodes.addLast(children);
}
} else if (n instanceof ObjectNode) {
nodes.addLast(n);
}
}
} else if (node instanceof ArrayNode) {
final int size = node.size();
for (int i = 0; i < size; i++) {
JsonNode child = node.get(i);
if (child instanceof TextNode) {
final TextNode replacement = new TextNode(StringUtils.substituteVariables(child.textValue(), vars));
((ArrayNode) node).remove(i);
((ArrayNode) node).insert(i, replacement);
} else {
nodes.addLast(child);
}
}
for (JsonNode children : node) {
nodes.addLast(children);
}
}
}
}
}
public static Object getCaseInsensitive(ObjectNode node, String name) {
final Iterator names = node.fieldNames();
while (names.hasNext()) {
String n = names.next();
if(n.equalsIgnoreCase(name)) {
return node.get(n);
}
}
return null;
}
public AnypointClient getClient() {
return client;
}
public void setClient(AnypointClient client) {
this.client = client;
}
public ObjectMapper getJsonMapper() {
return jsonMapper;
}
public byte[] toJson(Object obj) {
try (ByteArrayOutputStream tmp = new ByteArrayOutputStream()) {
jsonMapper.writeValue(tmp, obj);
return tmp.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public Map toJsonMap(String json) {
try {
return jsonMapper.readValue(json, new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy