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

rocks.voss.toniebox.beans.Transformer Maven / Gradle / Ivy

package rocks.voss.toniebox.beans;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;

public class Transformer {
    private static Logger log = Logger.getLogger(Transformer.class.getName());

    public static  T createBean(Class clazz, InputStream stream) throws IOException {
        String json = IOUtils.toString(stream, "UTF-8");
        return createBean(clazz, json);
    }

    public static  T createBean(Class clazz, String json) throws IOException {
        log.debug("Json: " + json);
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readValue(json, JsonNode.class);
        return mapper.treeToValue(jsonNode, clazz);
    }

    public static JsonNode getJson(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode returnJson = mapper.valueToTree(object);
        log.debug("Json: " + returnJson.toString());
        return returnJson;
    }

    public static String getJsonString(Object object) {
        return getJson(object).toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy