use of io.atlasmap.json.v2.JsonField in project atlasmap by atlasmap.
the class JsonSchemaInspectorTest method doInspectJsonSchemaCalendar.
private void doInspectJsonSchemaCalendar(String instance) throws Exception {
JsonDocument document = inspectionService.inspectJsonSchema(instance);
List<Field> fields = document.getFields().getField();
JsonField f = (JsonField) fields.get(0);
assertEquals("dtstart", f.getName());
assertEquals("/dtstart", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(1);
assertEquals("dtend", f.getName());
assertEquals("/dtend", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(2);
assertEquals("summary", f.getName());
assertEquals("/summary", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(3);
assertEquals("location", f.getName());
assertEquals("/location", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(4);
assertEquals("url", f.getName());
assertEquals("/url", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(5);
assertEquals("duration", f.getName());
assertEquals("/duration", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(6);
assertEquals("rdate", f.getName());
assertEquals("/rdate", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(7);
assertEquals("rrule", f.getName());
assertEquals("/rrule", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(8);
assertEquals("category", f.getName());
assertEquals("/category", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(9);
assertEquals("description", f.getName());
assertEquals("/description", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonComplexType) fields.get(10);
assertEquals("geo", f.getName());
assertEquals("/geo", f.getPath());
assertEquals(FieldType.COMPLEX, f.getFieldType());
List<JsonField> geofields = ((JsonComplexType) f).getJsonFields().getJsonField();
f = geofields.get(0);
assertEquals("latitude", f.getName());
assertEquals("/geo/latitude", f.getPath());
assertEquals(FieldType.NUMBER, f.getFieldType());
f = geofields.get(1);
assertEquals("longitude", f.getName());
assertEquals("/geo/longitude", f.getPath());
assertEquals(FieldType.NUMBER, f.getFieldType());
}
use of io.atlasmap.json.v2.JsonField in project atlasmap by atlasmap.
the class JsonSchemaInspectorTest method inspectJsonSchemaAddress.
@Test
public void inspectJsonSchemaAddress() throws Exception {
final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/address.json")));
JsonDocument document = inspectionService.inspectJsonSchema(schema);
List<Field> fields = document.getFields().getField();
JsonField f = (JsonField) fields.get(0);
assertEquals("post-office-box", f.getName());
assertEquals("/post-office-box", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(1);
assertEquals("extended-address", f.getName());
assertEquals("/extended-address", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(2);
assertEquals("street-address", f.getName());
assertEquals("/street-address", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(3);
assertEquals("locality", f.getName());
assertEquals("/locality", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(4);
assertEquals("region", f.getName());
assertEquals("/region", f.getPath());
assertEquals(FieldType.COMPLEX, f.getFieldType());
JsonComplexType c = (JsonComplexType) f;
assertEquals(true, c.isEnumeration());
List<String> regions = new ArrayList<>(Arrays.asList("NA", "EMEA", "LATAM", "APAC"));
for (JsonEnumField e : c.getJsonEnumFields().getJsonEnumField()) {
if (!regions.remove(e.getName())) {
fail("Unknown enum value: " + e.getName());
}
;
}
if (!regions.isEmpty()) {
fail("Not found: " + regions);
}
f = (JsonField) fields.get(5);
assertEquals("postal-code", f.getName());
assertEquals("/postal-code", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
f = (JsonField) fields.get(6);
assertEquals("country-name", f.getName());
assertEquals("/country-name", f.getPath());
assertEquals(FieldType.STRING, f.getFieldType());
}
use of io.atlasmap.json.v2.JsonField in project atlasmap by atlasmap.
the class JsonFieldReader method populateChildFields.
private void populateChildFields(AtlasInternalSession session, JsonNode node, FieldGroup fieldGroup, AtlasPath path) throws AtlasException {
List<Field> newChildren = new ArrayList<>();
for (Field child : fieldGroup.getField()) {
AtlasPath childPath = new AtlasPath(child.getPath());
JsonNode childNode = node.get(childPath.getLastSegment().getName());
if (childPath.getLastSegment().getCollectionType() != CollectionType.NONE) {
FieldGroup childGroup = populateCollectionItems(session, (ArrayNode) childNode, child);
newChildren.add(childGroup);
} else {
if (child instanceof FieldGroup) {
populateChildFields(session, childNode, (FieldGroup) child, childPath);
} else {
Object value = handleValueNode(session, childNode, (JsonField) child);
child.setValue(value);
}
newChildren.add(child);
}
}
fieldGroup.getField().clear();
fieldGroup.getField().addAll(newChildren);
}
use of io.atlasmap.json.v2.JsonField in project atlasmap by atlasmap.
the class JsonFieldReader method populateCollectionItems.
private FieldGroup populateCollectionItems(AtlasInternalSession session, JsonNode node, Field field) throws AtlasException {
if (!node.isArray()) {
throw new AtlasException(String.format("Couldn't find JSON array for field %s:%s", field.getDocId(), field.getPath()));
}
FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
ArrayNode arrayNode = (ArrayNode) node;
for (int i = 0; i < arrayNode.size(); i++) {
AtlasPath itemPath = new AtlasPath(group.getPath());
List<SegmentContext> segments = itemPath.getSegments(true);
itemPath.setCollectionIndex(segments.size() - 1, i);
if (field instanceof FieldGroup) {
FieldGroup itemGroup = AtlasJsonModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
populateChildFields(session, arrayNode.get(i), itemGroup, itemPath);
group.getField().add(itemGroup);
} else {
JsonField itemField = AtlasJsonModelFactory.cloneField((JsonField) field, false);
itemField.setPath(itemPath.toString());
Object value = handleValueNode(session, arrayNode.get(i), itemField);
itemField.setValue(value);
group.getField().add(itemField);
}
}
return group;
}
use of io.atlasmap.json.v2.JsonField in project atlasmap by atlasmap.
the class JsonSchemaInspector 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().enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES);
JsonNode rootNode = objectMapper.readTree(schema);
Map<String, JsonNode> definitionMap = new HashMap<>();
populateDefinitions(rootNode, definitionMap);
JsonField rootNodeType = getJsonFieldBuilder("", rootNode, null, definitionMap, new HashSet<>(), false).build();
if (rootNodeType instanceof JsonComplexType && ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().size() != 0) {
if (rootNodeType.getCollectionType() == null || rootNodeType.getCollectionType() == CollectionType.NONE) {
jsonDocument.getFields().getField().addAll(((JsonComplexType) rootNodeType).getJsonFields().getJsonField());
} else {
// taking care of topmost collection
jsonDocument.getFields().getField().add(rootNodeType);
}
} 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);
}
}
Aggregations