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());
}
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}}"));
}
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);
}
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);
}
}
}
}
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);
}
}
Aggregations