Search in sources :

Example 41 with JsonDocument

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

the class GenerateInspectionsMojo method generateJsonInstanceInspection.

private void generateJsonInstanceInspection(String fileName) throws MojoFailureException {
    try {
        Path path = Paths.get(fileName);
        String schema = new String(Files.readAllBytes(path));
        JsonDocument d = new JsonInspectionService().inspectJsonDocument(schema);
        String name = path.getFileName().toString();
        String outputName = name.substring(0, name.length() - 5);
        writeToJsonFile(DEFAULT_OUTPUT_FILE_PREFIX + "-" + outputName, d);
    } catch (Exception e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) JsonInspectionService(io.atlasmap.json.inspect.JsonInspectionService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JsonDocument(io.atlasmap.json.v2.JsonDocument) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 42 with JsonDocument

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

the class GenerateInspectionsMojo method generateJsonSchemaInspection.

private void generateJsonSchemaInspection(String fileName) throws MojoFailureException {
    try {
        Path path = Paths.get(fileName);
        String schema = new String(Files.readAllBytes(path));
        JsonDocument d = new JsonInspectionService().inspectJsonSchema(schema);
        String name = path.getFileName().toString();
        String outputName = name.substring(0, name.length() - 5);
        writeToJsonFile(DEFAULT_OUTPUT_FILE_PREFIX + "-" + outputName, d);
    } catch (Exception e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) JsonInspectionService(io.atlasmap.json.inspect.JsonInspectionService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) JsonDocument(io.atlasmap.json.v2.JsonDocument) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 43 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 44 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 45 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)73 JsonField (io.atlasmap.json.v2.JsonField)65 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)38 Test (org.junit.jupiter.api.Test)32 Test (org.junit.Test)29 Field (io.atlasmap.v2.Field)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)6 IOException (java.io.IOException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 JsonInspectionService (io.atlasmap.json.inspect.JsonInspectionService)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)2 JsonFields (io.atlasmap.json.v2.JsonFields)2 JsonInspectionRequest (io.atlasmap.json.v2.JsonInspectionRequest)2 JsonInspectionResponse (io.atlasmap.json.v2.JsonInspectionResponse)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2