@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()); } } }
@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()]))); } }
static Map<String, String> toStringMap(BMap map) { Map<String, String> returnMap = new HashMap<>(); Set bIterator = map.keySet(); for (Object aKey : bIterator) { if (map.get(aKey) instanceof BStringArray) { returnMap.put(aKey.toString(), (((BStringArray) map.get(aKey)).get(0))); } else { returnMap.put(aKey.toString(), map.get(aKey).stringValue()); } } return returnMap; }