Search in sources :

Example 1 with Fn

use of io.vertx.up.func.Fn in project vertx-zero by silentbalanceyh.

the class Jackson method searchData.

private static <T> T searchData(final JsonObject data, final Class<T> clazz, final String... pathes) {
    if (null == data || Values.ZERO == pathes.length) {
        return null;
    }
    /**
     * 1. Get current node  *
     */
    final JsonObject current = data;
    /**
     * 2. Extract current input key *
     */
    final String path = pathes[Values.IDX];
    /**
     * 3. Continue searching if key existing, otherwise terminal. *
     */
    return Fn.getSemi(current.containsKey(path) && null != current.getValue(path), Jackson.LOGGER, () -> {
        final Object curVal = current.getValue(path);
        T result = null;
        if (Values.ONE == pathes.length) {
            /**
             * 3.1. Get the end node. *
             */
            if (clazz == curVal.getClass()) {
                result = (T) curVal;
            }
        } else {
            /**
             * 3.2. Address the middle search *
             */
            if (Types.isJObject(curVal)) {
                final JsonObject continueNode = current.getJsonObject(path);
                /**
                 * 4.Extract new key *
                 */
                final String[] continueKeys = Arrays.copyOfRange(pathes, Values.ONE, pathes.length);
                result = Jackson.searchData(continueNode, clazz, continueKeys);
            }
        }
        return result;
    }, Fn::nil);
}
Also used : Fn(io.vertx.up.func.Fn) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)1 Fn (io.vertx.up.func.Fn)1