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