rocks.voss.toniebox.beans.Transformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toniebox-api Show documentation
Show all versions of toniebox-api Show documentation
API to handle request for the Toniebox
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();
}
}