use of com.fasterxml.jackson.databind.JsonMappingException in project torodb by torodb.
the class FilterListDeserializer method deserialize.
@Override
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
public FilterList deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
FilterList filterList = new FilterList();
JsonNode node = jp.getCodec().readTree(jp);
if (node instanceof ObjectNode) {
Iterator<Entry<String, JsonNode>> databaseEntriesIterator = node.fields();
while (databaseEntriesIterator.hasNext()) {
Entry<String, JsonNode> databaseEntry = databaseEntriesIterator.next();
try {
Map<String, List<IndexFilter>> collections = new HashMap<>();
if (databaseEntry.getValue() instanceof ObjectNode) {
readCollectionObject(jp, (ObjectNode) databaseEntry.getValue(), collections);
} else if (databaseEntry.getValue() instanceof ArrayNode) {
ArrayNode collectionsArray = (ArrayNode) databaseEntry.getValue();
Iterator<JsonNode> collectionsIterator = collectionsArray.elements();
int position = 0;
while (collectionsIterator.hasNext()) {
try {
JsonNode collection = collectionsIterator.next();
if (collection instanceof ObjectNode) {
readCollectionObject(jp, (ObjectNode) collection, collections);
} else if (collection instanceof ArrayNode) {
throw new JsonMappingException("wrong filter format: collection value inside " + "database array can not be an array", jp.getCurrentLocation());
} else {
collections.put(collection.asText(), new ArrayList<>());
}
position++;
} catch (Exception e) {
throw JsonMappingException.wrapWithPath(e, collections, position);
}
}
}
filterList.put(databaseEntry.getKey(), collections);
} catch (Exception e) {
throw JsonMappingException.wrapWithPath(e, filterList, databaseEntry.getKey());
}
}
} else {
throw new JsonMappingException("wrong filter format: filter list was not an object", jp.getCurrentLocation());
}
return filterList;
}
use of com.fasterxml.jackson.databind.JsonMappingException in project torodb by torodb.
the class FilterListDeserializer method readCollectionObject.
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
private void readCollectionObject(JsonParser jp, ObjectNode collection, Map<String, List<IndexFilter>> collections) throws JsonProcessingException, JsonMappingException {
Iterator<Entry<String, JsonNode>> collectionEntriesIterator = collection.fields();
while (collectionEntriesIterator.hasNext()) {
List<IndexFilter> indexFilters = new ArrayList<>();
Map.Entry<String, JsonNode> collectionEntry = collectionEntriesIterator.next();
try {
if (collectionEntry.getValue() instanceof ObjectNode) {
readIndexFilter(jp, collectionEntry.getValue(), indexFilters);
} else if (collectionEntry.getValue() instanceof ArrayNode) {
Iterator<JsonNode> indexFiltersIterator = collectionEntry.getValue().elements();
int position = 0;
while (indexFiltersIterator.hasNext()) {
try {
JsonNode indexFilter = indexFiltersIterator.next();
if (indexFilter instanceof ObjectNode) {
readIndexFilter(jp, indexFilter, indexFilters);
} else {
throw new JsonMappingException("wrong filter format: index filter should be an " + "object", jp.getCurrentLocation());
}
position++;
} catch (Exception e) {
throw JsonMappingException.wrapWithPath(e, indexFilters, position);
}
}
}
collections.put(collectionEntry.getKey(), indexFilters);
} catch (Exception e) {
throw JsonMappingException.wrapWithPath(e, collections, collectionEntry.getKey());
}
}
}
use of com.fasterxml.jackson.databind.JsonMappingException in project torodb by torodb.
the class ConfigUtils method mergeParam.
public static ObjectNode mergeParam(ObjectMapper objectMapper, ObjectNode configRootNode, String pathAndProp, String value) throws Exception {
if (JsonPointer.compile(pathAndProp).equals(JsonPointer.compile("/"))) {
return (ObjectNode) objectMapper.readTree(value);
}
String path = pathAndProp.substring(0, pathAndProp.lastIndexOf("/"));
String prop = pathAndProp.substring(pathAndProp.lastIndexOf("/") + 1);
JsonPointer pathPointer = JsonPointer.compile(path);
JsonNode pathNode = configRootNode.at(pathPointer);
if (pathNode.isMissingNode() || pathNode.isNull()) {
JsonPointer currentPointer = pathPointer;
JsonPointer childOfCurrentPointer = null;
List<JsonPointer> missingPointers = new ArrayList<>();
List<JsonPointer> childOfMissingPointers = new ArrayList<>();
do {
if (pathNode.isMissingNode() || pathNode.isNull()) {
missingPointers.add(0, currentPointer);
childOfMissingPointers.add(0, childOfCurrentPointer);
}
childOfCurrentPointer = currentPointer;
currentPointer = currentPointer.head();
pathNode = configRootNode.at(currentPointer);
} while (pathNode.isMissingNode() || pathNode.isNull());
for (int missingPointerIndex = 0; missingPointerIndex < missingPointers.size(); missingPointerIndex++) {
final JsonPointer missingPointer = missingPointers.get(missingPointerIndex);
final JsonPointer childOfMissingPointer = childOfMissingPointers.get(missingPointerIndex);
final List<JsonNode> newNodes = new ArrayList<>();
if (pathNode.isObject()) {
((ObjectNode) pathNode).set(missingPointer.last().getMatchingProperty(), createNode(childOfMissingPointer, newNodes));
} else if (pathNode.isArray() && missingPointer.last().mayMatchElement()) {
for (int index = ((ArrayNode) pathNode).size(); index < missingPointer.last().getMatchingIndex() + 1; index++) {
((ArrayNode) pathNode).add(createNode(childOfMissingPointer, newNodes));
}
} else {
throw new RuntimeException("Cannot set param " + pathAndProp + "=" + value);
}
pathNode = newNodes.get(newNodes.size() - 1);
}
}
Object valueAsObject;
try {
valueAsObject = objectMapper.readValue(value, Object.class);
} catch (JsonMappingException jsonMappingException) {
throw JsonMappingException.wrapWithPath(jsonMappingException, configRootNode, path.substring(1) + "/" + prop);
}
if (pathNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) pathNode;
if (valueAsObject != null) {
JsonNode valueNode = objectMapper.valueToTree(valueAsObject);
objectNode.set(prop, valueNode);
} else {
objectNode.remove(prop);
}
} else if (pathNode instanceof ArrayNode) {
ArrayNode arrayNode = (ArrayNode) pathNode;
Integer index = Integer.valueOf(prop);
if (valueAsObject != null) {
JsonNode valueNode = objectMapper.valueToTree(valueAsObject);
arrayNode.set(index, valueNode);
} else {
arrayNode.remove(index);
}
}
return configRootNode;
}
use of com.fasterxml.jackson.databind.JsonMappingException in project jackson-databind by FasterXML.
the class TestUnwrappedWithTypeInfo method testDefaultUnwrappedWithTypeInfo.
/*
/**********************************************************
/* Tests, serialization
/**********************************************************
*/
// [Issue#81]
public void testDefaultUnwrappedWithTypeInfo() throws Exception {
Outer outer = new Outer();
outer.setP1("101");
Inner inner = new Inner();
inner.setP2("202");
outer.setInner(inner);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValueAsString(outer);
fail("Expected exception to be thrown.");
} catch (JsonMappingException ex) {
verifyException(ex, "requires use of type information");
}
}
use of com.fasterxml.jackson.databind.JsonMappingException in project jackson-databind by FasterXML.
the class TestInferredMutators method testDeserializationInference.
// for #195
public void testDeserializationInference() throws Exception {
final String JSON = "{\"x\":2}";
ObjectMapper mapper = new ObjectMapper();
// First: default case, inference enabled:
assertTrue(mapper.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS));
Point p = mapper.readValue(JSON, Point.class);
assertEquals(2, p.x);
// but without it, should fail:
mapper = new ObjectMapper();
mapper.disable(MapperFeature.INFER_PROPERTY_MUTATORS);
try {
p = mapper.readValue(JSON, Point.class);
fail("Should not succeeed");
} catch (JsonMappingException e) {
verifyException(e, "unrecognized field \"x\"");
}
}
Aggregations