Search in sources :

Example 1 with JsonFields

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

the class JsonFieldReaderTest method testBoundaryValue.

private AtlasInternalSession testBoundaryValue(String fileName, String fieldPath, FieldType fieldType, Object expectedObject) throws Exception {
    String filePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "jsonFields" + File.separator + fileName;
    String document = new String(Files.readAllBytes(Paths.get(filePath)));
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath(fieldPath);
    field.setFieldType(fieldType);
    AtlasInternalSession session = read(field);
    assertEquals(expectedObject, field.getValue());
    return session;
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession)

Example 2 with JsonFields

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

the class InstanceInspector method getJsonComplexType.

private JsonComplexType getJsonComplexType(JsonComplexType parent, String aKey, int index) {
    JsonComplexType jsonComplexType = JsonComplexTypeFactory.createJsonComlexField();
    jsonComplexType.setJsonFields(new JsonFields());
    jsonComplexType.setName(aKey);
    jsonComplexType.setStatus(FieldStatus.SUPPORTED);
    if (index > 0) {
        jsonComplexType.setPath(parent.getPath().concat("[").concat(String.valueOf(index)).concat("]/").concat(aKey));
    } else {
        jsonComplexType.setPath(parent.getPath().concat("/").concat(aKey));
    }
    parent.getJsonFields().getJsonField().add(jsonComplexType);
    return jsonComplexType;
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 3 with JsonFields

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

the class InstanceInspector method getJsonComplexTypeFromEntry.

private JsonComplexType getJsonComplexTypeFromEntry(Map.Entry<String, JsonNode> entry) {
    JsonComplexType parent = JsonComplexTypeFactory.createJsonComlexField();
    parent.setJsonFields(new JsonFields());
    parent.setName(entry.getKey());
    parent.setStatus(FieldStatus.SUPPORTED);
    parent.setPath("/".concat(entry.getKey()));
    return parent;
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 4 with JsonFields

use of io.atlasmap.json.v2.JsonFields 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);
    }
}
Also used : JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) Iterator(java.util.Iterator) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) JsonDocument(io.atlasmap.json.v2.JsonDocument) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with JsonFields

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

the class JsonFieldWriterTest method testWriteHighlyComplexObject.

@Test
@Ignore
public // with nested arrays.
void testWriteHighlyComplexObject() throws Exception {
    JsonComplexType items = JsonComplexTypeFactory.createJsonComlexField();
    items.setPath("/items");
    items.setJsonFields(new JsonFields());
    write(items);
    JsonComplexType item = JsonComplexTypeFactory.createJsonComlexField();
    item.setJsonFields(new JsonFields());
    item.setPath("/items/item");
    item.setCollectionType(CollectionType.LIST);
    items.getJsonFields().getJsonField().add(item);
    write(item);
    JsonField itemid = new JsonField();
    itemid.setPath("/items/item/id");
    itemid.setValue("0001");
    itemid.setFieldType(FieldType.STRING);
    itemid.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(itemid);
    write(itemid);
    JsonField type = new JsonField();
    type.setPath("/items/item/type");
    type.setValue("donut");
    type.setFieldType(FieldType.STRING);
    type.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(type);
    write(type);
    JsonField name = new JsonField();
    name.setPath("/items/item/name");
    name.setValue("Cake");
    name.setFieldType(FieldType.STRING);
    name.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(name);
    write(name);
    JsonField ppu = new JsonField();
    ppu.setPath("/items/item/ppu");
    ppu.setValue(0.55);
    ppu.setFieldType(FieldType.DOUBLE);
    ppu.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(ppu);
    write(ppu);
    JsonComplexType batters = JsonComplexTypeFactory.createJsonComlexField();
    batters.setJsonFields(new JsonFields());
    batters.setPath("/items/item/batters");
    items.getJsonFields().getJsonField().add(batters);
    write(batters);
    JsonComplexType batter = JsonComplexTypeFactory.createJsonComlexField();
    batter.setPath("/items/item/batters/batter");
    batter.setJsonFields(new JsonFields());
    batter.setStatus(FieldStatus.SUPPORTED);
    batter.setCollectionType(CollectionType.LIST);
    batters.getJsonFields().getJsonField().add(batter);
    write(batter);
    JsonField batter1Id = new JsonField();
    batter1Id.setPath("/items/item/batters/batter/id");
    batter1Id.setValue("1001");
    batter1Id.setFieldType(FieldType.STRING);
    batter1Id.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter1Id);
    write(batter1Id);
    JsonField batter1Type = new JsonField();
    batter1Type.setPath("/items/item/batters/batter/type");
    batter1Type.setValue("Regular");
    batter1Type.setFieldType(FieldType.STRING);
    batter1Type.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter1Type);
    write(batter1Type);
    JsonField batter2Id = new JsonField();
    batter2Id.setPath("/items/item/batters/batter[1]/id");
    batter2Id.setValue("1002");
    batter2Id.setFieldType(FieldType.STRING);
    batter2Id.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter2Id);
    write(batter2Id);
    JsonField batter2Type = new JsonField();
    batter2Type.setPath("/items/item/batters/batter[1]/type");
    batter2Type.setValue("Chocolate");
    batter2Type.setFieldType(FieldType.STRING);
    batter2Type.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter2Type);
    write(batter2Type);
    JsonField batter3Id = new JsonField();
    batter3Id.setPath("/items/item/batters/batter[2]/id");
    batter3Id.setValue("1003");
    batter3Id.setFieldType(FieldType.STRING);
    batter3Id.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter3Id);
    write(batter3Id);
    JsonField batter3Type = new JsonField();
    batter3Type.setPath("/items/item/batters/batter[2]/type");
    batter3Type.setValue("Blueberry");
    batter3Type.setFieldType(FieldType.STRING);
    batter3Type.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter3Type);
    write(batter3Type);
    JsonField batter4Id = new JsonField();
    batter4Id.setPath("/items/item/batters/batter[3]/id");
    batter4Id.setValue("1004");
    batter4Id.setFieldType(FieldType.STRING);
    batter4Id.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter4Id);
    write(batter4Id);
    JsonField batter4Type = new JsonField();
    batter4Type.setPath("/items/item/batters/batter[3]/type");
    batter4Type.setValue("Devil's Food");
    batter4Type.setFieldType(FieldType.STRING);
    batter4Type.setStatus(FieldStatus.SUPPORTED);
    batter.getJsonFields().getJsonField().add(batter4Type);
    write(batter4Type);
    JsonComplexType topping = JsonComplexTypeFactory.createJsonComlexField();
    topping.setJsonFields(new JsonFields());
    topping.setPath("/items/item/topping");
    topping.setCollectionType(CollectionType.ARRAY);
    items.getJsonFields().getJsonField().add(topping);
    write(topping);
    JsonField topping1Id = new JsonField();
    topping1Id.setPath("/items/item/topping/id");
    topping1Id.setValue("5001");
    topping1Id.setFieldType(FieldType.STRING);
    topping1Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping1Id);
    write(topping1Id);
    JsonField topping1Type = new JsonField();
    topping1Type.setPath("/items/item/topping/type");
    topping1Type.setValue("None");
    topping1Type.setFieldType(FieldType.STRING);
    topping1Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping1Type);
    write(topping1Type);
    JsonField topping2Id = new JsonField();
    topping2Id.setPath("/items/item/topping/id[1]");
    topping2Id.setValue("5002");
    topping2Id.setFieldType(FieldType.STRING);
    topping2Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping2Id);
    write(topping2Id);
    JsonField topping2Type = new JsonField();
    topping2Type.setPath("/items/item/topping/type[1]");
    topping2Type.setValue("Glazed");
    topping2Type.setFieldType(FieldType.STRING);
    topping2Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping2Type);
    write(topping2Type);
    JsonField topping3Id = new JsonField();
    topping3Id.setPath("/items/item/topping/id[2]");
    topping3Id.setValue("5005");
    topping3Id.setFieldType(FieldType.STRING);
    topping3Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping3Id);
    write(topping3Id);
    JsonField topping3Type = new JsonField();
    topping3Type.setPath("/items/item/topping/type[2]");
    topping3Type.setValue("Sugar");
    topping3Type.setFieldType(FieldType.STRING);
    topping3Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping3Type);
    write(topping3Type);
    JsonField topping4Id = new JsonField();
    topping4Id.setPath("/items/item/topping/id[3]");
    topping4Id.setValue("5007");
    topping4Id.setFieldType(FieldType.STRING);
    topping4Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping4Id);
    write(topping4Id);
    JsonField topping4Type = new JsonField();
    topping4Type.setPath("/items/item/topping/type[3]");
    topping4Type.setValue("Powdered Sugar");
    topping4Type.setFieldType(FieldType.STRING);
    topping4Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping4Type);
    write(topping4Type);
    JsonField topping5Id = new JsonField();
    topping5Id.setPath("/items/item/topping/id[4]");
    topping5Id.setValue("5006");
    topping5Id.setFieldType(FieldType.STRING);
    topping5Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping5Id);
    write(topping5Id);
    JsonField topping5Type = new JsonField();
    topping5Type.setPath("/items/item/topping/type[4]");
    topping5Type.setValue("Chocolate with Sprinkles");
    topping5Type.setFieldType(FieldType.STRING);
    topping5Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping5Type);
    write(topping5Type);
    JsonField topping6Id = new JsonField();
    topping6Id.setPath("/items/item/topping/id[5]");
    topping6Id.setValue("5003");
    topping6Id.setFieldType(FieldType.STRING);
    topping6Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping6Id);
    write(topping6Id);
    JsonField topping6Type = new JsonField();
    topping6Type.setPath("/items/item/topping/type[5]");
    topping6Type.setValue("Chocolate");
    topping6Type.setFieldType(FieldType.STRING);
    topping6Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping6Type);
    write(topping6Type);
    JsonField topping7Id = new JsonField();
    topping7Id.setPath("/items/item/topping/id[6]");
    topping7Id.setValue("5004");
    topping7Id.setFieldType(FieldType.STRING);
    topping7Id.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping7Id);
    write(topping7Id);
    JsonField topping7Type = new JsonField();
    topping7Type.setPath("/items/item/topping/type[6]");
    topping7Type.setValue("Maple");
    topping7Type.setFieldType(FieldType.STRING);
    topping7Type.setStatus(FieldStatus.SUPPORTED);
    topping.getJsonFields().getJsonField().add(topping7Type);
    write(topping7Type);
    // repeat complex
    JsonComplexType item1 = JsonComplexTypeFactory.createJsonComlexField();
    item1.setJsonFields(new JsonFields());
    item1.setPath("/items/item[1]");
    item1.setCollectionType(CollectionType.LIST);
    items.getJsonFields().getJsonField().add(item1);
    write(item1);
    JsonField itemId1 = new JsonField();
    itemId1.setPath("/items/item[1]/id");
    itemId1.setValue("0002");
    itemId1.setFieldType(FieldType.STRING);
    itemId1.setStatus(FieldStatus.SUPPORTED);
    item1.getJsonFields().getJsonField().add(itemId1);
    write(itemId1);
    JsonField type1 = new JsonField();
    type1.setPath("/items/item[1]/type");
    type1.setValue("donut");
    type1.setFieldType(FieldType.STRING);
    type1.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(type1);
    write(type1);
    JsonField name1 = new JsonField();
    name1.setPath("/items/item[1]/name");
    name1.setValue("Raised");
    name1.setFieldType(FieldType.STRING);
    name1.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(name1);
    write(name1);
    JsonField ppu2 = new JsonField();
    ppu2.setPath("/items/item[1]/ppu");
    ppu2.setValue(0.55);
    ppu2.setFieldType(FieldType.DOUBLE);
    ppu2.setStatus(FieldStatus.SUPPORTED);
    item.getJsonFields().getJsonField().add(ppu2);
    write(ppu2);
    JsonComplexType batters1 = JsonComplexTypeFactory.createJsonComlexField();
    batters1.setJsonFields(new JsonFields());
    batters1.setPath("/items/item[1]/batters");
    items.getJsonFields().getJsonField().add(batters1);
    write(batters1);
    JsonComplexType batter1 = JsonComplexTypeFactory.createJsonComlexField();
    batter1.setPath("/items/item[1]/batters/batter");
    batter1.setJsonFields(new JsonFields());
    batter1.setStatus(FieldStatus.SUPPORTED);
    batter1.setCollectionType(CollectionType.LIST);
    batters1.getJsonFields().getJsonField().add(batter1);
    write(batter1);
    // FIXME this writes to the items/item/batters/batter and not at
    // items/item[1]/batters/batter
    // JsonField batter1_1_id = new JsonField();
    // batter1_1_id.setPath("/items/item[1]/batters/batter/id");
    // batter1_1_id.setValue("1001");
    // batter1_1_id.setFieldType(FieldType.STRING);
    // batter1_1_id.setStatus(FieldStatus.SUPPORTED);
    // batter1.getJsonFields().getJsonField().add(batter1_1_id);
    // writer.write(batter1_1_id, root);
    // 
    // JsonField batter1_1_type = new JsonField();
    // batter1_1_type.setPath("/items/item[1]/batters/batter/type");
    // batter1_1_type.setValue("Regular");
    // batter1_1_type.setFieldType(FieldType.STRING);
    // batter1_1_type.setStatus(FieldStatus.SUPPORTED);
    // batter1.getJsonFields().getJsonField().add(batter1_1_type);
    // writer.write(batter1_1_type, root);
    System.out.println(prettyPrintJson(writer.getRootNode().toString()));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonFields(io.atlasmap.json.v2.JsonFields) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

JsonComplexType (io.atlasmap.json.v2.JsonComplexType)4 JsonFields (io.atlasmap.json.v2.JsonFields)4 JsonField (io.atlasmap.json.v2.JsonField)3 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonDocument (io.atlasmap.json.v2.JsonDocument)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1