Example usage for com.fasterxml.jackson.databind ObjectMapper readValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper readValue.

Prototype

@SuppressWarnings("unchecked")
    public <T> T readValue(byte[] src, JavaType valueType)
            throws IOException, JsonParseException, JsonMappingException 

Source Link

Usage

From source file:com.tectonica.thirdparty.Jackson2.java

public static <T, P> T fromJson(Reader r, Class<T> clz, Class<P> paramClz, ObjectMapper mapper) {
    try {/*from w ww  .jav  a 2 s.  co  m*/
        JavaType jt = mapper.getTypeFactory().constructParametrizedType(clz, clz, paramClz);
        return mapper.readValue(r, jt);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.csc.fi.ioapi.utils.LDHelper.java

private static Map<String, Object> jsonObject(String json) {
    try {//  ww w.j av a2s  .  c o  m
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {
        });
    } catch (IOException ex) {
        System.out.println(ex.toString());
        return null;
    }
}

From source file:edu.illinois.ncsa.springdata.SpringData.java

/**
 * Converts a JSON string to an actual object. This will convert the string
 * to an object specified by classname. This code will remove any duplicates
 * from the objects created.//from w w w . ja  v  a  2s . c o m
 * 
 * @param json
 *            the json encoded object.
 * @param classname
 *            the classname to which the json object should be converted.
 * @return an object of the same type as the classname.
 * @throws IOException
 *             throws an IOException if the string could not be converted to
 *             an actual object.
 */
public static <T> T JSONToObject(String json, Class<T> classname) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return removeDuplicate((T) mapper.readValue(json.getBytes("UTF-8"), classname));
}

From source file:edu.illinois.ncsa.springdata.SpringData.java

/**
 * Converts a JSON inputstream to an actual object. This will convert the
 * inputstream to an object specified by classname. This code will remove
 * any duplicates from the objects created.
 * /*from  w  ww.j  av  a  2 s.c  o  m*/
 * @param is
 *            the inputstream with the json code.
 * @param classname
 *            the classname to which the json object should be converted.
 * @return an object of the same type as the classname.
 * @throws IOException
 *             throws an IOException if the string could not be converted to
 *             an actual object.
 */
public static <T> T JSONToObject(InputStream is, Class<T> classname) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return removeDuplicate((T) mapper.readValue(is, classname));
}

From source file:cn.org.once.cstack.cli.rest.JsonConverter.java

public static List<Message> getMessage(String response) {
    ObjectMapper mapper = new ObjectMapper();
    List<Message> messages = null;
    try {/*from w  w  w .  j  ava  2 s. c o m*/
        messages = mapper.readValue(response, new TypeReference<List<Message>>() {
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return messages;
}

From source file:architecture.ee.web.util.ParamUtils.java

/**
 * Json ? Map  ./*from  w ww. j a  v a 2  s  .  c o  m*/
 * @param request
 * @param name
 * @return
 */
public static Map getJsonParameter(HttpServletRequest request, String name) {
    try {
        String jsonString = getParameter(request, name);
        com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
        return mapper.readValue(jsonString, Map.class);
    } catch (Exception e) {
        return Collections.EMPTY_MAP;
    }
}

From source file:nl.esciencecenter.xnattool.DataSetConfig.java

public static DataSetConfig parseXML(String xml) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper xmlMapper = new XmlMapper();
    DataSetConfig value = xmlMapper.readValue(xml, DataSetConfig.class);
    value.postReadUpdate();/* ww w. ja va  2  s .  co m*/
    return value;
}

From source file:com.bna.ezrxlookup.util.JsonMapperUtil.java

/**
 * Parse JSON string and return a list of object type.
 * @param jsonString - input JSON string
 * @param rootName - root node name/*from   ww w.  ja v a 2s  . c om*/
 * @param type - object class type
 * @return list of object class
 */
public static <T> List<T> readJsonToList(String jsonString, String rootName, Class<T> clazz) throws Exception {
    List<T> objList = null;
    ObjectMapper objectMapper = new ObjectMapper();

    try {
        // get json content with root name
        JsonNode root = objectMapper.readTree(jsonString).get(rootName);
        TypeFactory tf = objectMapper.getTypeFactory();
        JavaType listOfObjs = tf.constructCollectionType(ArrayList.class, clazz);
        objList = objectMapper.readValue(root.traverse(), listOfObjs);
    } catch (Exception e) {
        throw e;
    }

    return objList;
}

From source file:fi.csc.mobileauth.shibboleth.rest.MobileServiceLoginHandler.java

private static StatusResponse getStatusResponse(String uri) {
    HttpMethod httpMethod = executeUriCall(uri);
    if (httpMethod == null) {
        log.debug("Could not execute call to URL {}", uri);
        return null;
    }/*from   w  ww  .j  a v a 2  s .  c  o m*/
    try {
        InputStream content = httpMethod.getResponseBodyAsStream();
        ObjectMapper objectMapper = new ObjectMapper();
        StatusResponse response = objectMapper.readValue(content, StatusResponse.class);
        content.close();
        log.debug("Status response eventId={}, errorMessage={}", response.getEventId(),
                response.getErrorMessage());
        return response;
    } catch (IOException e) {
        log.error("Could not obtain response from the REST service!", e);
        return null;
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:architecture.ee.web.util.ParamUtils.java

public static <T> T getJsonParameter(HttpServletRequest request, String name, Class<T> requiredType) {
    try {//ww  w  . j  a v  a2 s.co m
        String jsonString = getParameter(request, name);
        com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
        return (T) mapper.readValue(jsonString, requiredType);
    } catch (Exception e) {

        log.error(e);

        if (requiredType == List.class)
            return (T) Collections.EMPTY_LIST;
        else if (requiredType == Map.class) {
            return (T) Collections.EMPTY_MAP;
        }
        return null;
    }
}