Search in sources :

Example 11 with JsonComplexType

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

the class InstanceInspector method getJsonComplexType.

private JsonComplexType getJsonComplexType(JsonComplexType parent, String aKey, int index) {
    JsonComplexType jsonComplexType = JsonComplexTypeFactory.createJsonComlexField();
    jsonComplexType.setJsonFields(new JsonFields());
    jsonComplexType.setName(aKey);
    jsonComplexType.setStatus(FieldStatus.SUPPORTED);
    if (index > 0) {
        jsonComplexType.setPath(parent.getPath().concat("[").concat(String.valueOf(index)).concat("]/").concat(aKey));
    } else {
        jsonComplexType.setPath(parent.getPath().concat("/").concat(aKey));
    }
    parent.getJsonFields().getJsonField().add(jsonComplexType);
    return jsonComplexType;
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 12 with JsonComplexType

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

the class InstanceInspector method getJsonComplexTypeFromEntry.

private JsonComplexType getJsonComplexTypeFromEntry(Map.Entry<String, JsonNode> entry) {
    JsonComplexType parent = JsonComplexTypeFactory.createJsonComlexField();
    parent.setJsonFields(new JsonFields());
    parent.setName(entry.getKey());
    parent.setStatus(FieldStatus.SUPPORTED);
    parent.setPath("/".concat(entry.getKey()));
    return parent;
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 13 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType 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 14 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType 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 15 with JsonComplexType

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

the class JsonDocumentInspectionServiceTest method inspectFlatPrimitiveWithRoot.

@Test
public void inspectFlatPrimitiveWithRoot() throws Exception {
    final String instance = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/flatprimitive-base-rooted.json")));
    JsonDocument document = inspectionService.inspectJsonDocument(instance);
    Assert.assertNotNull(document);
    Assert.assertThat(document.getFields().getField().size(), Is.is(1));
    JsonComplexType root = (JsonComplexType) document.getFields().getField().get(0);
    Assert.assertNotNull(root);
    Assert.assertThat(root.getName(), Is.is("SourceFlatPrimitive"));
    Assert.assertThat(root.getPath(), Is.is("/SourceFlatPrimitive"));
    Assert.assertThat(root.getFieldType(), Is.is(FieldType.COMPLEX));
    Assert.assertThat(root.getStatus(), Is.is(FieldStatus.SUPPORTED));
    Assert.assertThat(root.getJsonFields().getJsonField().size(), Is.is(8));
    for (int i = 0; i < root.getJsonFields().getJsonField().size(); i++) {
        JsonField field = root.getJsonFields().getJsonField().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("/SourceFlatPrimitive/".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("/SourceFlatPrimitive/".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("/SourceFlatPrimitive/".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"));
            Assert.assertThat(field.getValue(), Is.is(-63988281.00));
            Assert.assertThat(field.getPath(), Is.is("/SourceFlatPrimitive/".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"));
            Assert.assertThat(field.getValue(), Is.is(8281));
            Assert.assertThat(field.getPath(), Is.is("/SourceFlatPrimitive/".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("/SourceFlatPrimitive/".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("/SourceFlatPrimitive/".concat(field.getName())));
            Assert.assertThat(field.getFieldType(), Is.is(FieldType.INTEGER));
            Assert.assertThat(field.getStatus(), Is.is(FieldStatus.SUPPORTED));
        }
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Aggregations

JsonComplexType (io.atlasmap.json.v2.JsonComplexType)21 JsonField (io.atlasmap.json.v2.JsonField)18 JsonDocument (io.atlasmap.json.v2.JsonDocument)17 Test (org.junit.Test)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 JsonFields (io.atlasmap.json.v2.JsonFields)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Field (io.atlasmap.v2.Field)2 Map (java.util.Map)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Ignore (org.junit.Ignore)1