Search in sources :

Example 16 with JsonField

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

the class SchemaInspectorTest method inspectFlatPrimitiveNoRoot.

@Test
public void inspectFlatPrimitiveNoRoot() throws Exception {
    final String instance = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/flatprimitive-base-unrooted.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(instance);
    assertNotNull(document);
    assertEquals(5, document.getFields().getField().size());
    List<Field> fields = document.getFields().getField();
    JsonField field = (JsonField) fields.get(0);
    assertEquals("booleanField", field.getName());
    assertEquals("/booleanField", field.getPath());
    assertEquals(FieldType.BOOLEAN, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(1);
    assertEquals("stringField", field.getName());
    assertEquals("/stringField", field.getPath());
    assertEquals(FieldType.STRING, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(2);
    assertEquals("numberField", field.getName());
    assertEquals("/numberField", field.getPath());
    assertEquals(FieldType.NUMBER, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(3);
    assertEquals("intField", field.getName());
    assertEquals("/intField", field.getPath());
    assertEquals(FieldType.INTEGER, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
    field = (JsonField) fields.get(4);
    assertEquals("nullField", field.getName());
    assertEquals("/nullField", field.getPath());
    assertEquals(FieldType.NONE, field.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, field.getStatus());
}
Also used : Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Example 17 with JsonField

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

the class JsonFieldWriterTest method testWriteSimpleObjectWithRoot.

@Test
public void testWriteSimpleObjectWithRoot() throws Exception {
    JsonField field1 = AtlasJsonModelFactory.createJsonField();
    field1.setPath("/car/brand");
    field1.setValue("Mercedes");
    field1.setFieldType(FieldType.STRING);
    write(field1);
    JsonField field2 = AtlasJsonModelFactory.createJsonField();
    field2.setPath("/car/doors");
    field2.setValue(5);
    field2.setFieldType(FieldType.INTEGER);
    write(field2);
    Assert.assertThat(writer.getRootNode().toString(), Is.is("{\"car\":{\"brand\":\"Mercedes\",\"doors\":5}}"));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Test(org.junit.Test)

Example 18 with JsonField

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

the class JsonFieldWriterTest method writeInteger.

public void writeInteger(String path, Integer value) throws Exception {
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setValue(value);
    field.setStatus(FieldStatus.SUPPORTED);
    field.setFieldType(FieldType.INTEGER);
    field.setPath(path);
    write(field);
}
Also used : JsonField(io.atlasmap.json.v2.JsonField)

Example 19 with JsonField

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

the class JsonFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    JsonField jsonField = JsonField.class.cast(session.head().getSourceField());
    if (rootNode == null) {
        throw new AtlasException("document is not set");
    }
    if (jsonField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    JsonNode valueNode = null;
    AtlasPath path = new AtlasPath(jsonField.getPath());
    if (path.getSegments().size() >= 1) {
        if (rootNode.size() == 1 && !path.getSegments().get(0).startsWith(rootNode.fieldNames().next())) {
            // peel off a rooted object
            valueNode = rootNode.elements().next();
        } else {
            valueNode = rootNode;
        }
        // need to walk the path....
        for (String nodeName : path.getSegments()) {
            if (valueNode == null) {
                break;
            }
            valueNode = getValueNode(valueNode, nodeName);
        }
    }
    if (valueNode == null) {
        return;
    }
    if (valueNode.isNull()) {
        jsonField.setValue(null);
    // we can't detect field type if it's null node
    } else {
        if (jsonField.getFieldType() != null) {
            // mapping is overriding the fieldType
            try {
                Object convertedValue = conversionService.convertType(valueNode.asText(), jsonField.getFormat(), jsonField.getFieldType(), null);
                jsonField.setValue(convertedValue);
            } catch (AtlasConversionException e) {
                AtlasUtil.addAudit(session, jsonField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", valueNode.asText(), jsonField.getFieldType()), jsonField.getPath(), AuditStatus.ERROR, valueNode.asText());
            }
        } else {
            if (valueNode.isTextual()) {
                handleTextualNode(valueNode, jsonField);
            } else if (valueNode.isNumber()) {
                handleNumberNode(valueNode, jsonField);
            } else if (valueNode.isBoolean()) {
                handleBooleanNode(valueNode, jsonField);
            } else if (valueNode.isContainerNode()) {
                handleContainerNode(valueNode, jsonField);
            } else if (valueNode.isNull()) {
                jsonField.setValue(null);
            } else {
                LOG.warn(String.format("Detected unsupported json type for field p=%s docId=%s", jsonField.getPath(), jsonField.getDocId()));
                jsonField.setValue(valueNode.toString());
                jsonField.setFieldType(FieldType.UNSUPPORTED);
            }
        }
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) AtlasException(io.atlasmap.api.AtlasException)

Example 20 with JsonField

use of io.atlasmap.json.v2.JsonField 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)

Aggregations

JsonField (io.atlasmap.json.v2.JsonField)63 Test (org.junit.Test)45 JsonDocument (io.atlasmap.json.v2.JsonDocument)30 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)15 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)10 Head (io.atlasmap.spi.AtlasInternalSession.Head)8 AtlasMapping (io.atlasmap.v2.AtlasMapping)7 Mapping (io.atlasmap.v2.Mapping)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Field (io.atlasmap.v2.Field)4 Validation (io.atlasmap.v2.Validation)4 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)2 DefaultAtlasConversionService (io.atlasmap.core.DefaultAtlasConversionService)2 AtlasJsonModelFactory (io.atlasmap.json.v2.AtlasJsonModelFactory)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2 AtlasModuleMode (io.atlasmap.spi.AtlasModuleMode)2 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)2 DataSource (io.atlasmap.v2.DataSource)2 DataSourceType (io.atlasmap.v2.DataSourceType)2 FieldType (io.atlasmap.v2.FieldType)2