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.sugaronrest.ErrorResponse.java

/**
 * Gets formatted error response based on UniRest error
 *
 * @param jsonResponse Response object from UniRest
 * @return ErrorResponse object//from  w  ww  . ja va2s .co  m
 */
public static ErrorResponse fromJson(String jsonResponse) {
    ObjectMapper mapper = JsonObjectMapper.getMapper();
    try {
        ErrorResponse errorResponse = mapper.readValue(jsonResponse, ErrorResponse.class);
        if (errorResponse != null) {
            if (errorResponse.getNumber() <= 0) {
                return null;
            }

            errorResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
        }

        return errorResponse;
    } catch (IOException e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.hybridbpm.ui.component.chart.util.DiagrammeUtil.java

public static <T> T stringToObject(String json, Class<T> clazz) {
    T result = null;/*from w ww  .j  av  a 2  s .  co  m*/
    try {
        ObjectMapper mapper = new ObjectMapper();
        //            mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        result = (T) mapper.readValue(json, clazz);
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, ex.getMessage(), ex);
    }
    return result;
}

From source file:edu.usf.cutr.open311client.utils.Open311Parser.java

/**
 * Parses json string result for service description
 *
 * @param json string result//from  w ww  . java 2  s .  c  o  m
 * @return ServiceDescription object
 */
public static ServiceDescription parseServiceDescription(String json) {

    if (json == null) {
        return new ServiceDescription();
    }

    ObjectMapper om = createObjectMapper();
    ServiceDescription serviceDescription = new ServiceDescription();
    try {
        serviceDescription = om.readValue(json, ServiceDescription.class);
        serviceDescription.setResultCode(Open311Constants.RESULT_OK);
    } catch (IOException e) {
        logger.error(e);
    }
    return serviceDescription;
}

From source file:io.fabric8.devops.ProjectConfigs.java

private static <T> T parseYaml(File file, Class<T> clazz) throws IOException {
    ObjectMapper mapper = createObjectMapper();
    return mapper.readValue(file, clazz);
}

From source file:io.fabric8.devops.ProjectConfigs.java

private static <T> T parseYaml(InputStream inputStream, Class<T> clazz) throws IOException {
    ObjectMapper mapper = createObjectMapper();
    return mapper.readValue(inputStream, clazz);
}

From source file:io.fabric8.devops.ProjectConfigs.java

private static <T> T parseYaml(String yaml, Class<T> clazz) throws IOException {
    ObjectMapper mapper = createObjectMapper();
    return mapper.readValue(yaml, clazz);
}

From source file:com.dell.asm.asmcore.asmmanager.util.razor.RazorUtil.java

/**
 * Parse the return value of the razor nodes GET api, e.g.
 * http://RAZOR_HOST/api/collections/nodes
 * into a list of node names.// ww w  .  j  a v a2s  . com
 *
 * @param mapper ObjectMapper to parse JSON
 * @param json Json nodes data
 * @return List of node names
 */
public static List<String> parseNodeNamesJson(ObjectMapper mapper, String json) throws IOException {
    List<String> ret = new ArrayList<>();
    Map repoMap = mapper.readValue(json, Map.class);
    List list = (List) repoMap.get("items");
    for (Object o : list) {
        Map elem = (Map) o;
        String name = (String) elem.get(NODES_NAME_KEY);
        if (StringUtils.isEmpty(name)) {
            LOGGER.warn("Invalid node element " + elem + " in " + json);
        } else {
            ret.add(name);
        }
    }
    return ret;
}

From source file:com.github.dockerjava.test.serdes.JSONTestHelper.java

/**
 * Performs roundtrip test for the specified class.
 * /*w  ww  .  j  a v a2 s.  c om*/
 * @param <TClass>
 *            Item class
 * @param item
 *            Item to be checked
 * @param asclass
 *            Class to be used during conversions
 * @return Deserialized object after the roundtrip
 * @throws IOException
 *             JSON Conversion error
 * @throws AssertionError
 *             Validation error
 */
public static <TClass> TClass testRoundTrip(TClass item, Class<TClass> asclass)
        throws IOException, AssertionError {
    ObjectMapper mapper = new ObjectMapper();

    String serialized1 = mapper.writeValueAsString(item);
    JsonNode json1 = mapper.readTree(serialized1);
    TClass deserialized1 = mapper.readValue(serialized1, asclass);
    String serialized2 = mapper.writeValueAsString(deserialized1);
    JsonNode json2 = mapper.readTree(serialized2);
    TClass deserialized2 = mapper.readValue(serialized2, asclass);

    assertEquals(json2, json1, "JSONs must be equal after the second roundtrip");
    return deserialized2;
}

From source file:de.alpharogroup.xml.json.JsonTransformer.java

/**
 * Transforms the given json string into a java object.
 *
 * @param <T>//  w w w. j a va  2s.com
 *            the generic type of the return type
 * @param jsonString
 *            the json string
 * @param clazz
 *            the clazz of the generic type
 * @param newMapper
 *            flag that indicates if a new ObjectMapper should be created. if true a new
 *            ObjectMapper will be created otherwise the ObjectMapper from this class will be
 *            returned.
 * @return the object
 * @throws JsonParseException
 *             If an error occurs when parsing the string into Object
 * @throws JsonMappingException
 *             the If an error occurs when mapping the string into Object
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static <T> T toObject(final String jsonString, final Class<T> clazz, final boolean newMapper)
        throws JsonParseException, JsonMappingException, IOException {
    final ObjectMapper mapper = getObjectMapper(newMapper);
    final T object = mapper.readValue(jsonString, clazz);
    return object;
}

From source file:org.mule.tools.rhinodo.impl.NodeModuleImplBuilder.java

public static <U, V> Map<U, V> getPackageJSONMap(File packageJson) {
    ObjectMapper objectMapper = new ObjectMapper();
    Map<U, V> map;// w w  w  .j a v  a2s  . com
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(packageJson);
        map = objectMapper.readValue(inputStream, Map.class);
    } catch (IOException e) {
        throw new IllegalArgumentException("Error: trying to parse package.json.");
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return map;
}