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.
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.spin.impl.json.jackson;
import static org.camunda.commons.utils.EnsureUtil.ensureNotNull;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.camunda.spin.SpinList;
import org.camunda.spin.impl.SpinListImpl;
import org.camunda.spin.impl.json.jackson.format.JacksonJsonDataFormat;
import org.camunda.spin.impl.json.jackson.format.JacksonJsonDataFormatMapper;
import org.camunda.spin.impl.json.jackson.query.JacksonJsonPathQuery;
import org.camunda.spin.json.SpinJsonDataFormatException;
import org.camunda.spin.json.SpinJsonException;
import org.camunda.spin.json.SpinJsonNode;
import org.camunda.spin.json.SpinJsonPathQuery;
import org.camunda.spin.spi.DataFormatMapper;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
/**
* Wrapper for a Jackson Json Tree Node.
*
* @author Thorben Lindhauer
* @author Stefan Hentschel
*/
public class JacksonJsonNode extends SpinJsonNode {
private static final JacksonJsonLogger LOG = JacksonJsonLogger.JSON_TREE_LOGGER;
protected final JsonNode jsonNode;
protected final JacksonJsonDataFormat dataFormat;
public JacksonJsonNode(JsonNode jsonNode, JacksonJsonDataFormat dataFormat) {
this.jsonNode = jsonNode;
this.dataFormat = dataFormat;
}
public String getDataFormatName() {
return dataFormat.getName();
}
public JsonNode unwrap() {
return jsonNode;
}
public String toString() {
StringWriter writer = new StringWriter();
writeToWriter(writer);
return writer.toString();
}
public void writeToWriter(Writer writer) {
dataFormat.getWriter().writeToWriter(writer, jsonNode);
}
/**
* fetch correct array index if index is less than 0
*
* ArrayNode will convert all negative integers into 0...
*
* @param index wanted index
* @return {@link Integer} new index
*/
protected Integer getCorrectIndex(Integer index) {
Integer size = jsonNode.size();
Integer newIndex = index;
// reverse walking through the array
if(index < 0) {
newIndex = size + index;
}
// the negative index would be greater than the size a second time!
if(newIndex < 0) {
throw LOG.indexOutOfBounds(index, size);
}
// the index is greater as the actual size
if(index > size) {
throw LOG.indexOutOfBounds(index, size);
}
return newIndex;
}
protected int lookupArray(JsonNode searchNode, int direction) {
if(!this.isArray()) {
throw LOG.unableToGetIndex(jsonNode.getNodeType().name());
}
int i = direction>0 ? 0 : jsonNode.size() - 1;
for (; i < jsonNode.size() && i >= 0; i += direction) {
if (jsonNode.get(i).equals(searchNode)){
return i;
}
}
return -1;
}
public Integer indexOf(Object searchObject) {
ensureNotNull("searchObject", searchObject);
JsonNode node = dataFormat.createJsonNode(searchObject);
int res = lookupArray(node, 1);
if(res == -1){
throw LOG.unableToFindProperty(node.asText());
}
return res;
}
public Integer lastIndexOf(Object searchObject) {
ensureNotNull("searchObject", searchObject);
JsonNode node = dataFormat.createJsonNode(searchObject);
int res = lookupArray(node, -1);
if(res == -1){
throw LOG.unableToFindProperty(node.asText());
}
return res;
}
public boolean contains(Object searchObject) {
ensureNotNull("searchObject", searchObject);
JsonNode node = dataFormat.createJsonNode(searchObject);
int res = lookupArray(node, 1);
return res != -1;
}
public boolean isObject() {
return jsonNode.isObject();
}
public boolean hasProp(String name) {
return jsonNode.has(name);
}
public SpinJsonNode prop(String name) {
ensureNotNull("name", name);
if(jsonNode.has(name)) {
JsonNode property = jsonNode.get(name);
return dataFormat.createWrapperInstance(property);
} else {
throw LOG.unableToFindProperty(name);
}
}
public SpinJsonNode prop(String name, String newProperty) {
ObjectNode node = (ObjectNode) jsonNode;
node.put(name, newProperty);
return this;
}
public SpinJsonNode prop(String name, Number newProperty) {
ObjectNode node = (ObjectNode) jsonNode;
// Numbers magic because Jackson has no native .put(Number value)
if (newProperty == null) {
node.putNull(name);
} else if(newProperty instanceof Long) {
node.put(name, newProperty.longValue());
} else if(newProperty instanceof Integer) {
node.put(name, newProperty.intValue());
} else if(newProperty instanceof Float) {
node.put(name, newProperty.floatValue());
} else {
// convert any other sub class of Number into Float
node.put(name, newProperty.floatValue());
}
return this;
}
public SpinJsonNode prop(String name, int newProperty) {
return prop(name, (Number) newProperty);
}
public SpinJsonNode prop(String name, float newProperty) {
return prop(name, (Number) newProperty);
}
public SpinJsonNode prop(String name, long newProperty) {
return prop(name, (Number) newProperty);
}
public SpinJsonNode prop(String name, boolean newProperty) {
return prop(name, (Boolean) newProperty);
}
public SpinJsonNode prop(String name, Boolean newProperty) {
ObjectNode node = (ObjectNode) jsonNode;
node.put(name, newProperty);
return this;
}
public SpinJsonNode prop(String name, List