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:org.wso2.emm.agent.utils.CommonUtils.java

/**
 * Revoke currently enforced policy.//from w ww  . j  a va 2 s. com
 * @param context - Application context.
 */
public static void revokePolicy(Context context) throws AndroidAgentException {
    String payload = Preference.getString(context, Constants.PreferenceFlag.APPLIED_POLICY);
    PolicyOperationsMapper operationsMapper = new PolicyOperationsMapper();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

    PolicyRevokeHandler revokeHandler = new PolicyRevokeHandler(context);

    try {
        if (payload != null) {
            List<org.wso2.emm.agent.beans.Operation> operations = mapper.readValue(payload,
                    mapper.getTypeFactory().constructCollectionType(List.class,
                            org.wso2.emm.agent.beans.Operation.class));
            for (org.wso2.emm.agent.beans.Operation op : operations) {
                op = operationsMapper.getOperation(op);
                revokeHandler.revokeExistingPolicy(op);
            }
            Preference.putString(context, Constants.PreferenceFlag.APPLIED_POLICY, null);
        }
    } catch (IOException e) {
        throw new AndroidAgentException("Error occurred while parsing stream", e);
    }
}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Transforme un json en object java/*from   w  w  w  .j av  a 2  s . com*/
 *
 * @param <T>
 * @param cls
 * @param json
 * @return
 */
protected static <T> T getJava(Class<T> cls, String json) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(json, cls);
    } catch (IOException ex) {
    }
    return null;
}

From source file:io.orchestrate.client.AbstractOperation.java

@SuppressWarnings("unchecked")
static <T> KvObject<T> jsonToKvObject(final ObjectMapper objectMapper, final JsonNode jsonNode,
        final Class<T> clazz) throws IOException {
    // parse the PATH structure (e.g.):
    // {"collection":"coll","key":"aKey","ref":"someRef"}
    final JsonNode path = jsonNode.get("path");
    final String collection = path.get("collection").asText();
    final String key = path.get("key").asText();
    final String ref = path.get("ref").asText();
    final KvMetadata metadata = new KvMetadata(collection, key, ref);

    // parse result structure (e.g.):
    // {"path":{...},"value":{}}
    final JsonNode valueNode = jsonNode.get("value");
    final String rawValue = valueNode.toString();

    final T value;
    if (clazz == String.class) {
        // don't deserialize JSON data
        value = (T) rawValue;// ww w . j  a  va2 s .co  m
    } else {
        value = objectMapper.readValue(rawValue, clazz);
    }

    return new KvObject<T>(metadata, value, rawValue);
}

From source file:edumsg.shared.JsonMapper.java

public HashMap<String, String> deserialize() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(json, new TypeReference<HashMap<String, String>>() {
    });/*ww w. j  a v a 2 s . c o m*/
}

From source file:org.mayocat.shop.taxes.configuration.TaxesSettingsTest.java

@Test
public void testModeDeserialization() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    TaxesSettings mode = mapper.readValue("{ \"mode\": { \"value\" : \"incl\" }}", TaxesSettings.class);
    Assert.assertEquals(Mode.INCLUSIVE_OF_TAXES, mode.getMode().getValue());
}

From source file:com.dataartisans.streamexample.JsonExtractor.java

@Override
public void flatMap(String input, Collector<Edit> out) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Edit edit = mapper.readValue(input, Edit.class);
    if (edit.parse()) {
        out.collect(edit);/*from  w  ww.  jav  a  2s.  co m*/
    }
}

From source file:org.mayocat.configuration.internal.AbstractConfigurationTest.java

protected Map<String, Serializable> loadConfiguration(String uri) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(loadJSON(uri), new TypeReference<Map<String, Object>>() {
    });/*from www.  j  a v a  2s  .  co m*/
}

From source file:com.orange.clara.cloud.truststore.TrustStoreStorePropertyJsonReader.java

@Override
public TrustStoreProperty read(String json) {
    try {//from  w ww. j  a  v  a  2  s .  c  o m
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(json, TrustStoreProperty.class);

    } catch (Exception e) {
        String message = String.format("Invalid truststore property.");
        throw new IllegalStateException(message, e);
    }

}

From source file:org.apache.ambari.view.web.model.dto.serializer.StringToJsonNodeSerializer.java

@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    gen.writeObject(mapper.readValue(value, JsonNode.class));
}

From source file:com.dell.asm.asmcore.asmmanager.util.discovery.DeviceTypeCheckUtil.java

private static DiscoverDeviceType checkEM(String ipAddress, InfrastructureDevice device) {
    String emHtml = String.format("https://%s:3033/em/EnterpriseManager", ipAddress);
    String loginURL = String.format("https://%s:3033/api/rest/ApiConnection/Login", ipAddress);
    String sResponse = null;/*from  w w  w .j a  v  a  2s.co  m*/
    try {
        sResponse = getHTML(emHtml);
    } catch (IOException ioe) {
        LOGGER.debug("GET html request for " + emHtml + " failed:", ioe);
    }
    try {
        if (sResponse != null
                && (sResponse.contains("Enterprise Manager") || sResponse.contains("Storage Manager"))) {
            CredentialEntity cred = getCredentialDAO().findById(device.getEmCredentialId());
            String username = StringUtils.isEmpty(cred.getDomain()) ? cred.getUsername()
                    : cred.getDomain() + "\\" + cred.getUsername();
            String response = httpPost(loginURL, username, cred.getPasswordData().getString());

            if (StringUtils.isNotEmpty(response)) {
                device.setDiscoveryResponse(response);

                ObjectMapper mapper = new ObjectMapper();
                Map params = mapper.readValue(response, Map.class);
                Boolean connected = (Boolean) params.get("connected");
                if (Boolean.TRUE.equals(connected)) {
                    return DiscoverDeviceType.EM;
                } else {
                    LOGGER.debug("Login passed but property connected is not true. Response: " + response);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.debug("POST login request for " + loginURL + " failed:", e);
    }
    return DiscoverDeviceType.UNKNOWN;
}