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

com.github.shoothzj.javatool.util.YamlUtil Maven / Gradle / Ivy

There is a newer version: 3.1.15
Show newest version
package com.github.shoothzj.javatool.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.io.Resources;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.List;

/**
 * @author akka
 */
@Slf4j
public class YamlUtil {

    private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory());

    public static String toYaml(Object o) {
        try {
            return MAPPER.writeValueAsString(o);
        } catch (Exception e) {
            log.error("json process error, exception is ", e);
        }
        return "";
    }

    public static  T relativePathToObject(String relativePath, Class type) {
        return toObject(Resources.getResource(relativePath), type);
    }

    public static  T relativePathToRefer(String relativePath, TypeReference reference) {
        return toRefer(Resources.getResource(relativePath), reference);
    }

    public static  List relativePathToList(String relativePath, TypeReference> typeReference) {
        return toList(Resources.getResource(relativePath), typeReference);
    }

    public static  T toObject(URL yaml, Class type) {
        try {
            return MAPPER.readValue(yaml, type);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
        }
        return null;
    }

    public static  T toRefer(URL yaml, TypeReference reference) {
        try {
            return MAPPER.readValue(yaml, reference);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
            return null;
        }
    }

    public static  List toList(URL yaml, TypeReference> typeReference) {
        try {
            return MAPPER.readValue(yaml, typeReference);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
            return null;
        }
    }

    public static  T toObject(String yaml, Class type) {
        try {
            return MAPPER.readValue(yaml, type);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
        }
        return null;
    }

    public static  T toRefer(String yaml, TypeReference reference) {
        try {
            return MAPPER.readValue(yaml, reference);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
            return null;
        }
    }

    public static  List toList(String yaml, TypeReference> typeReference) {
        try {
            return MAPPER.readValue(yaml, typeReference);
        } catch (Exception e) {
            log.error("yaml process error, exception is ", e);
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy