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

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

Introduction

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

Prototype

public JsonNode readTree(URL source) throws IOException, JsonProcessingException 

Source Link

Document

Method to deserialize JSON content as tree expressed using set of JsonNode instances.

Usage

From source file:com.sqs.tq.fdc.Fixtures.java

private static JsonNode build(String s) {
    try {/* w w  w .j  a  v a2 s  .  com*/
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readTree(JSONHelper.sq2dq(s));
    } catch (Exception e) {
        throw new RuntimeException("invalid json? " + e.getMessage(), e);
    }
}

From source file:com.googlecode.batchfb.util.JSONUtils.java

/**
 * Parses a string into a JsonNode using the mapper
 *//*from   w  ww.j a v a  2 s. c om*/
public static JsonNode toNode(String value, ObjectMapper mapper) {
    try {
        return mapper.readTree(value);
    } catch (IOException ex) {
        throw new FacebookException(ex);
    }
}

From source file:org.apache.drill.exec.store.http.util.JsonConverter.java

private static JsonNode from(String content) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(content);
    return root;//from ww  w.  j ava  2s.  c  om
}

From source file:org.opendaylight.alto.core.northbound.route.endpointproperty.impl.EndpointpropertyRouteChecker.java

public static JsonNode checkJsonSyntax(String content) {
    JsonNode jsonContent;/*from w ww  .ja  v a2  s.  c  o m*/
    ObjectMapper mapper = new ObjectMapper();
    try {
        jsonContent = mapper.readTree(content);
    } catch (IOException e) {
        throw new AltoErrorSyntax();
    }
    if (null == jsonContent) {
        throw new AltoErrorSyntax();
    } else if (jsonContent.isNull()) {
        throw new AltoErrorMissingField(AltoNorthboundRouteEndpointproperty.FIELD_PROPERTIES);
    } else {
        return jsonContent;
    }
}

From source file:com.trusolve.ant.filters.JsonToYamlFilter.java

public static Reader readDocument(Reader in) throws JsonProcessingException, IOException {
    ObjectMapper jsonIn = new ObjectMapper();
    JsonNode jn = jsonIn.readTree(in);

    YAMLFactory yf = new YAMLFactory();
    StringWriter sw = new StringWriter();

    yf.createGenerator(sw).setCodec(new ObjectMapper(yf)).writeObject(jn);

    return new StringReader(sw.toString());
}

From source file:io.fabric8.maven.core.util.JSONUtil.java

public static boolean equals(JSONObject first, JSONObject second) {
    final ObjectMapper mapper = new ObjectMapper();

    try {/* w  w w  .j av a 2 s .c om*/
        final JsonNode tree1 = mapper.readTree(first.toString());
        final JsonNode tree2 = mapper.readTree(second.toString());
        return tree1.equals(tree2);
    } catch (IOException e) {
        return false;
    }
}

From source file:io.klerch.alexa.tellask.util.factory.AlexaSpeechletFactory.java

/**
 * Creates an AlexaSpeechlet from bytes of a speechlet request. It will extract the
 * locale from the request and uses it for creating a new instance of AlexaSpeechlet
 * @param serializedSpeechletRequest bytes of a speechlet request
 * @param speechletClass the class of your AlexaSpeechlet to instantiate
 * @param utteranceReader the reader AlexaSpeechlet should use when reading out utterances
 * @param <T> must extend AlexaSpeechlet
 * @return new instance of AlexaSpeechlet
 * @throws IOException thrown when something went wrong
 *//*from  w ww.j ava  2  s  .co  m*/
public static <T extends AlexaSpeechlet> T createSpeechletFromRequest(final byte[] serializedSpeechletRequest,
        final Class<T> speechletClass, final UtteranceReader utteranceReader) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();

    final JsonNode parser = mapper.readTree(serializedSpeechletRequest);
    final String locale = Optional.of(parser.path("request")).filter(node -> !node.isMissingNode())
            .map(node -> node.path("locale")).filter(node -> !node.isMissingNode()).map(JsonNode::textValue)
            .orElse(DEFAULT_LOCALE);

    try {
        return speechletClass.getConstructor(String.class, UtteranceReader.class).newInstance(locale,
                utteranceReader);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException
            | NoSuchMethodException e) {
        throw new IOException("Could not create Speechlet from speechlet request", e);
    }
}

From source file:software.uncharted.util.HTTPUtil.java

public static JsonNode postJSON(String url, JsonNode body) throws IOException {
    String response = post(url, body);
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(response);
}

From source file:org.kiji.rest.serializers.AvroToJsonStringSerializer.java

/**
 * Returns an encoded JSON object for the given Avro object.
 *
 * @param record is the record to encode
 * @return the JSON object representing this Avro object.
 *
 * @throws IOException if there is an error.
 *///ww  w . j a  v  a  2 s  . com
public static JsonNode getJsonNode(GenericContainer record) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(getJsonString(record));
}

From source file:com.almende.eve.config.YamlReader.java

/**
 * Load./* w w w . j  a  va 2 s.  c  o  m*/
 * 
 * @param is
 *            the is
 * @return the config
 */
public static Config load(final InputStream is) {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
        return Config.decorate((ObjectNode) mapper.readTree(is));
    } catch (final JsonProcessingException e) {
        LOG.log(Level.WARNING, "Couldn't parse Yaml file", e);
    } catch (final IOException e) {
        LOG.log(Level.WARNING, "Couldn't read Yaml file", e);
    }
    return null;
}