package net.northking.cctp.element.internal.utils;


import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

import java.util.EnumSet;
import java.util.Set;

/**
 * JsonUtils
 *
 * @author fengxiang.zhang
 * @date 2022/4/13
 * @since 1.0
 */
public class JsonUtils {

    private final static JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
    private final static JacksonMappingProvider mappingProvider = new JacksonMappingProvider();

    static {
        Configuration.setDefaults(new Configuration.Defaults() {

            @Override
            public Set<Option> options() {
                return EnumSet.noneOf(Option.class);
            }

            @Override
            public MappingProvider mappingProvider() {
                return mappingProvider;
            }

            @Override
            public JsonProvider jsonProvider() {
                return jsonProvider;
            }
        });
    }

    public static Object document(String responseBody) {
        return jsonProvider.parse(responseBody);
    }

    public static Object path(Object document, String path) {
        return JsonPath.read(document, path);
    }

}
