Search in sources :

Example 1 with BStringArray

use of org.ballerinalang.model.values.BStringArray in project carbon-apimgt by wso2.

the class GetKeys method execute.

@Override
public BValue[] execute(Context ctx) {
    List<String> keys = new ArrayList<String>();
    BJSON json = (BJSON) getRefArgument(ctx, 0);
    JsonNode node = json.value();
    if (node.getNodeType() != JsonNodeType.OBJECT) {
        return getBValues(new BStringArray());
    }
    Iterator<String> keysItr = ((ObjectNode) node).fieldNames();
    while (keysItr.hasNext()) {
        keys.add(keysItr.next());
    }
    return getBValues(new BStringArray(keys.toArray(new String[keys.size()])));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) BJSON(org.ballerinalang.model.values.BJSON) BStringArray(org.ballerinalang.model.values.BStringArray)

Example 2 with BStringArray

use of org.ballerinalang.model.values.BStringArray in project carbon-apimgt by wso2.

the class GetKeysTestCase method testGetKeys.

@Test(description = "Get keys from a JSON object")
public void testGetKeys() {
    BValue[] args = {};
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "testGetKeys", args);
    Assert.assertTrue(returns[0] instanceof BStringArray);
    BStringArray keys = (BStringArray) returns[0];
    Assert.assertEquals(keys.size(), 3);
    Assert.assertEquals(keys.get(0), "fname");
    Assert.assertEquals(keys.get(1), "lname");
    Assert.assertEquals(keys.get(2), "age");
    Assert.assertTrue(returns[1] instanceof BStringArray);
    Assert.assertEquals(((BStringArray) returns[1]).size(), 0);
    Assert.assertTrue(returns[2] instanceof BStringArray);
    Assert.assertEquals(((BStringArray) returns[2]).size(), 0);
    Assert.assertTrue(returns[3] instanceof BStringArray);
    Assert.assertEquals(((BStringArray) returns[3]).size(), 0);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BStringArray(org.ballerinalang.model.values.BStringArray) Test(org.testng.annotations.Test)

Example 3 with BStringArray

use of org.ballerinalang.model.values.BStringArray in project carbon-apimgt by wso2.

the class ListFilesTestCase method testListJsonFiles.

@Test(description = "Returns a string array of json file names")
public void testListJsonFiles() {
    BValue[] args = {};
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "listJsonFiles", args);
    Assert.assertTrue(returns[0] instanceof BStringArray);
    Assert.assertEquals(((BStringArray) returns[0]).size(), 0);
    Assert.assertTrue(returns[1] instanceof BStringArray);
    BStringArray d1Files = (BStringArray) returns[1];
    Assert.assertEquals(d1Files.size(), 1);
    Assert.assertEquals(d1Files.get(0), "a.json");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BStringArray(org.ballerinalang.model.values.BStringArray) Test(org.testng.annotations.Test)

Example 4 with BStringArray

use of org.ballerinalang.model.values.BStringArray in project carbon-apimgt by wso2.

the class ListFiles method execute.

@Override
public BValue[] execute(Context context) {
    String directoryPath = getStringArgument(context, 0);
    File directory = new File(directoryPath);
    File[] fList = directory.listFiles();
    List<String> list = new ArrayList<String>();
    if (fList != null) {
        for (File file : fList) {
            if (file.isFile() && file.getName().toLowerCase(Locale.ENGLISH).endsWith(".json")) {
                list.add(file.getName());
            }
        }
        Collections.sort(list, Comparator.naturalOrder());
        BStringArray balArray = new BStringArray();
        int i = 0;
        while (i < list.size()) {
            balArray.add(i, list.get(i));
            i = i + 1;
        }
        return getBValues(balArray);
    } else {
        return getBValues(new BStringArray());
    }
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) BStringArray(org.ballerinalang.model.values.BStringArray)

Aggregations

BStringArray (org.ballerinalang.model.values.BStringArray)4 ArrayList (java.util.ArrayList)2 BValue (org.ballerinalang.model.values.BValue)2 Test (org.testng.annotations.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 File (java.io.File)1 BJSON (org.ballerinalang.model.values.BJSON)1