Search in sources :

Example 16 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument in project atlasmap by atlasmap.

the class InstanceInspector method handleValueEntry.

private void handleValueEntry(JsonDocument jsonDocument, Map.Entry<String, JsonNode> jsonNodeEntry, JsonComplexType parent, int index) {
    JsonNode theNode = jsonNodeEntry.getValue();
    String nodeKey = jsonNodeEntry.getKey();
    JsonField field = AtlasJsonModelFactory.createJsonField();
    if (nodeKey != null) {
        field.setName(nodeKey);
        if (parent != null) {
            LOG.trace("HANDLING AN VALUE NODE WITH PARENT ---> " + parent.getName() + " WITH INDEX OF " + index);
            if (index > 0 && (parent.getCollectionType() != null && parent.getCollectionType().compareTo(CollectionType.ARRAY) == 0)) {
                field.setPath(parent.getPath().concat("/").concat(nodeKey).concat("[").concat(String.valueOf(index)).concat("]"));
            } else if (index > 0 && (parent.getCollectionType() != null && parent.getCollectionType().compareTo(CollectionType.LIST) == 0)) {
                field.setPath(parent.getPath().concat("[").concat(String.valueOf(index)).concat("]/").concat(nodeKey));
            } else {
                field.setPath(parent.getPath().concat("/").concat(nodeKey));
            }
        } else {
            LOG.trace("HANDLING AN VALUE NODE WITH NO PARENT WITH INDEX OF " + index);
            field.setPath("/".concat(nodeKey));
        }
    }
    setNodeValueOnField(theNode, field);
    if (parent == null) {
        jsonDocument.getFields().getField().add(field);
    } else {
        parent.getJsonFields().getJsonField().add(field);
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 17 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument in project atlasmap by atlasmap.

the class InstanceInspector method handleObjectNode.

private void handleObjectNode(JsonDocument jsonDocument, JsonNode jsonNode, JsonComplexType parent, int index) throws IOException {
    LOG.trace("HANDLING AN OBJECT NODE " + jsonNode.fields().next().getKey() + " WITH PARENT ---> " + parent.getName() + " WITH INDEX OF " + index);
    Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
    while (fields.hasNext()) {
        Map.Entry<String, JsonNode> next = fields.next();
        String key = next.getKey();
        JsonNode node = next.getValue();
        if (node.isValueNode()) {
            handleValueEntry(jsonDocument, next, parent, index);
        } else if (node.isObject()) {
            LOG.trace("FOUND AN OBJECT NODE THAT IS A CONTAINER WITH KEY --> " + key + " WITH A PARENT INDEX OF " + index);
            JsonComplexType container = getJsonComplexType(parent, key, index);
            // rest index to zero when dealing with containers (we don't need an index on
            // containers)
            handleObjectNode(jsonDocument, next.getValue(), container, 0);
        } else if (node.isArray() && node.get(0).isObject()) {
            ArrayNode arrayNode = (ArrayNode) node;
            // index for children
            int innerIndex = 0;
            JsonComplexType deeperChild = getJsonComplexType(parent, key, index);
            if (parent.getCollectionType() == null) {
                deeperChild.setCollectionType(CollectionType.LIST);
            } else {
                deeperChild.setCollectionType(CollectionType.ARRAY);
            }
            for (JsonNode deeperJsonNode : arrayNode) {
                handleObjectNode(jsonDocument, deeperJsonNode, deeperChild, innerIndex);
                innerIndex++;
            }
        }
    }
}
Also used : JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Map(java.util.Map)

Example 18 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument in project atlasmap by atlasmap.

the class InstanceInspector method inspect.

public JsonDocument inspect(String instance) throws JsonInspectionException {
    if (instance == null || instance.isEmpty()) {
        throw new IllegalArgumentException("JSON instance cannot be null");
    }
    try {
        JsonDocument jsonDocument = AtlasJsonModelFactory.createJsonDocument();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(instance);
        if (rootNode.isArray()) {
            // TODO how do we handle a topmost array
            JsonComplexType field = JsonComplexTypeFactory.createJsonComlexField();
            field.setFieldType(null);
            field.setJsonFields(new JsonFields());
            field.setStatus(FieldStatus.UNSUPPORTED);
            field.setCollectionType(CollectionType.ARRAY);
            field.setValue(rootNode.toString());
            jsonDocument.getFields().getField().add(field);
        } else if (rootNode.isObject()) {
            Iterator<Map.Entry<String, JsonNode>> nodes = rootNode.fields();
            while (nodes.hasNext()) {
                Map.Entry<String, JsonNode> entry = nodes.next();
                if (entry.getValue().isObject()) {
                    LOG.trace("NODE IS AN OBJECT --> " + entry.getKey() + " WITH ---> " + entry.getValue().size() + " FIELDS");
                    // this is a complex type
                    JsonComplexType parent = getJsonComplexTypeFromEntry(entry);
                    jsonDocument.getFields().getField().add(parent);
                    handleObjectNode(jsonDocument, entry.getValue(), parent, 0);
                } else if (entry.getValue().isArray()) {
                    // this is a complex type as an ARRAY
                    LOG.trace("NODE IS AN ARRAY --> " + entry.getKey() + " WITH ---> " + entry.getValue().size() + " CHILDREN");
                    JsonComplexType parent = getJsonComplexTypeFromEntry(entry);
                    parent.setCollectionType(CollectionType.ARRAY);
                    jsonDocument.getFields().getField().add(parent);
                    handleArrayNode(jsonDocument, (ArrayNode) entry.getValue(), parent, entry.getKey(), 0);
                } else if (entry.getValue().isValueNode()) {
                    LOG.trace("NODE IS A VALUE --> " + entry.getKey() + " WITH ---> " + entry.getValue().size() + " CHILDREN");
                    handleValueEntry(jsonDocument, entry, null, 0);
                }
            }
        }
        return jsonDocument;
    } catch (IOException e) {
        throw new JsonInspectionException(e);
    }
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) Iterator(java.util.Iterator) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) JsonDocument(io.atlasmap.json.v2.JsonDocument) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 19 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument in project atlasmap by atlasmap.

the class SchemaInspector method inspect.

public JsonDocument inspect(String schema) throws JsonInspectionException {
    if (schema == null || schema.isEmpty()) {
        throw new IllegalArgumentException("JSON schema cannot be null");
    }
    try {
        JsonDocument jsonDocument = AtlasJsonModelFactory.createJsonDocument();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(schema);
        Map<String, JsonNode> definitionMap = new HashMap<>();
        populateDefinitions(rootNode, definitionMap);
        JsonField rootNodeType = getJsonFieldBuilder(null, rootNode, null, definitionMap).build();
        if (rootNodeType.getCollectionType() == CollectionType.LIST) {
            LOG.warn("Topmost array is not supported");
            if (rootNodeType instanceof JsonComplexType) {
                ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().clear();
            }
            rootNodeType.setStatus(FieldStatus.UNSUPPORTED);
            jsonDocument.getFields().getField().add(rootNodeType);
        } else if (rootNodeType instanceof JsonComplexType && ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().size() != 0) {
            jsonDocument.getFields().getField().addAll(((JsonComplexType) rootNodeType).getJsonFields().getJsonField());
        } else if (rootNodeType.getFieldType() == FieldType.COMPLEX) {
            LOG.warn("No simple type nor property is defined for the root node. It's going to be empty");
        } else {
            jsonDocument.getFields().getField().add(rootNodeType);
        }
        return jsonDocument;
    } catch (Exception e) {
        throw new JsonInspectionException(e);
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) HashMap(java.util.HashMap) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonDocument(io.atlasmap.json.v2.JsonDocument) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 20 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument in project atlasmap by atlasmap.

the class JsonDocumentInspectionServiceTest method inspectJsonDocumentEscapedCharsInKeys.

@Test
public void inspectJsonDocumentEscapedCharsInKeys() throws Exception {
    final String instance = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/keys-with-escaped-characters.json")));
    JsonDocument document = inspectionService.inspectJsonDocument(instance);
    Assert.assertNotNull(document);
    Assert.assertThat(document.getFields().getField().size(), Is.is(7));
    for (int i = 0; i < document.getFields().getField().size(); i++) {
        JsonField field = (JsonField) document.getFields().getField().get(i);
        if (i == 0) {
            Assert.assertThat(field.getName(), Is.is("'booleanField'"));
            Assert.assertThat(field.getValue(), Is.is(false));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.BOOLEAN));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 1) {
            Assert.assertThat(field.getName(), Is.is("\"charField\""));
            Assert.assertThat(field.getValue(), Is.is("a"));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.STRING));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 2) {
            Assert.assertThat(field.getName(), Is.is("\\doubleField"));
            Assert.assertThat(field.getValue(), Is.is(-27152745.3422));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.DOUBLE));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 3) {
            Assert.assertThat(field.getName(), Is.is("floatField\t"));
            Assert.assertThat(field.getValue(), Is.is(-63988281.00));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.DOUBLE));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 4) {
            Assert.assertThat(field.getName(), Is.is("intField\n"));
            Assert.assertThat(field.getValue(), Is.is(8281));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.INTEGER));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 5) {
            Assert.assertThat(field.getName(), Is.is("shortField"));
            Assert.assertThat(field.getValue(), Is.is(81));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.INTEGER));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        } else if (i == 6) {
            Assert.assertThat(field.getName(), Is.is("longField"));
            Assert.assertThat(field.getValue(), Is.is(3988281));
            Assert.assertThat(field.getPath(), Is.is("/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.INTEGER));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        }
    }
// printDocument(document);
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Aggregations

JsonDocument (io.atlasmap.json.v2.JsonDocument)34 JsonField (io.atlasmap.json.v2.JsonField)31 Test (org.junit.Test)29 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Field (io.atlasmap.v2.Field)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonDocumentInspectionService (io.atlasmap.json.inspect.JsonDocumentInspectionService)1 JsonFields (io.atlasmap.json.v2.JsonFields)1 JsonInspectionRequest (io.atlasmap.json.v2.JsonInspectionRequest)1 JsonInspectionResponse (io.atlasmap.json.v2.JsonInspectionResponse)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ApplicationPath (javax.ws.rs.ApplicationPath)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1