All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.zenwave360.zdl.antlr.JSONPath Maven / Gradle / Ivy

package io.github.zenwave360.zdl.antlr;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;

import java.util.List;
import java.util.Set;

class JSONPath {

    private static final Configuration config = Configuration.defaultConfiguration();

    public static  T get(Object object, String jsonPath) {
        try {
            return (T) JsonPath.using(config).parse(object).read(jsonPath);
        } catch (PathNotFoundException e) {
            return null;
        }
    }

    public static  List getUnique(Object object, String jsonPath) {
        return get(object, jsonPath, List.of()).stream().filter(e -> e != null).distinct().toList();
    }

    public static  T get(Object object, String jsonPath, T defaultIfNull) {
        try {
            var value = JsonPath.using(config).parse(object).read(jsonPath);
            return value != null? (T) value : defaultIfNull;
        } catch (PathNotFoundException e) {
            return defaultIfNull;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy