Filter for JSON files that contain part of an array

I have a function that allows me to filter specific JSON files using another JSON object as a filter.

See code:

public Map<String, Entry<JsonObject, Long>> loadFilter(Coll<?> coll, JsonObject filter){
    // Create Ret
    Map<String, Entry<JsonObject, Long>> ret = null;

    // Get Directory
    File directory = getDirectory(coll);
    if ( ! directory.isDirectory()) return ret;

    // Find All
    File[] files = directory.listFiles(JsonFileFilter.get());

    // Create Ret
    ret = new LinkedHashMap<String, Entry<JsonObject, Long>>(files.length);

    // Filter rules
    Set<Map.Entry<String, JsonElement>> filterRules = filter.entrySet();

    // For Each Found
    for (File file : files)
    {
        // Get ID
        String id = idFromFile(file);

        // Get Entry
        Entry<JsonObject, Long> entry = loadFile(file);

        // Trying to fix a weird condition causing a NPE error
        if(entry == null) continue;
        if(entry.getKey() == null) continue;

        // Compare the files with the given filter
        Set<Map.Entry<String, JsonElement>> fileEntries = entry.getKey().entrySet();
        if (fileEntries.containsAll(filterRules)) {
            // Add found data to return list
            ret.put(id, entry);
        }
    }

    return ret;
}

Imagine I have the following JSON:

{
    "objects": [
        "object1",
        "object2"
    ],
}

What I'm trying to do is filter out any files in which the array objects contain object1. I do not need object 2, I want to filter out files that have at least object1 in the array of objects.

Nothing works in the code below:

JsonObject filter = new JsonObject();
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("object1"));
filter.add("objects", array);
Map<String, Entry<JsonObject, Long>> result = loadFilter(coll, filter); // nothing

Any help is assigned.

+4
source share
2 answers

Your code

if (fileEntries.containsAll(filterRules)) {

, , , , .

, Gson, .

:

private static boolean checkJsonPredicate(JsonElement element, JsonElement predicate) {
    if (predicate == null) {
        return true;
    }

    if (element == null || predicate.getClass() != element.getClass()) {
        return false;
    }

    if (predicate.isJsonObject()) {
        return predicate.getAsJsonObject().entrySet().stream()
                .allMatch(e -> checkJsonPredicate(element.getAsJsonObject().get(e.getKey()), e.getValue()));
    }

    if (predicate.isJsonArray()) {
        return StreamSupport.stream(predicate.getAsJsonArray().spliterator(), false)
                .allMatch(element.getAsJsonArray()::contains);
    }

    return predicate.equals(element);
}

Stream API , element JSON predicate JSON.

( , ). , , .

+2

, , objects.

, , , 1.

objects .

-

 Gson gson = new Gson();

 // Filter object rule
 List<JsonElement> objectRules = gson.fromJson(filter.get("objects"), ArrayList.class);
 // Compare the files with the given object filter
 List<JsonElement> objectEntries = gson.fromJson(entry.getKey().get("objects"), ArrayList.class);

 if (objectEntries.containsAll(objectRules)) {...}
0

Source: https://habr.com/ru/post/1685771/


All Articles