Search in sources :

Example 71 with JsonMappingException

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) FilterList(com.torodb.packaging.config.model.protocol.mongo.FilterList) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 72 with JsonMappingException

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());
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Iterator(java.util.Iterator) IndexFilter(com.torodb.packaging.config.model.protocol.mongo.FilterList.IndexFilter) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMap(java.util.HashMap) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 73 with JsonMappingException

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 74 with JsonMappingException

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");
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 75 with JsonMappingException

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\"");
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)140 IOException (java.io.IOException)68 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 JsonParseException (com.fasterxml.jackson.core.JsonParseException)52 Test (org.junit.Test)31 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)19 HashMap (java.util.HashMap)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 File (java.io.File)9 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 List (java.util.List)7 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 Response (javax.ws.rs.core.Response)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5