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()])));
}
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);
}
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");
}
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());
}
}
Aggregations