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