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:org.apache.tika.language.translate.Lingo24Translator.java

@Override
public String translate(String text, String sourceLanguage, String targetLanguage) throws Exception {
    if (!this.isAvailable)
        return text;
    Response response = client.accept(MediaType.APPLICATION_JSON).query("user_key", userKey)
            .query("source", sourceLanguage).query("target", targetLanguage).query("q", text).get();
    BufferedReader reader = new BufferedReader(
            new InputStreamReader((InputStream) response.getEntity(), "UTF-8"));
    String line = null;//from www  .  ja  v a 2s.c  o  m
    StringBuffer responseText = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        responseText.append(line);
    }

    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonResp = mapper.readTree(responseText.toString());
    if (jsonResp.findValuesAsText("errors").isEmpty()) {
        return jsonResp.findValuesAsText("translation").get(0);
    } else {
        throw new TikaException(jsonResp.findValue("errors").get(0).asText());
    }
}

From source file:com.sg2net.utilities.ListaCAP.json.ComuneDeserializer.java

@Override
public Comune deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jsonParser);
    JsonNode codiceIstatNode = root.get("codiceIstat");
    String codiceIstat = codiceIstatNode.asText();
    JsonNode codiceCatastaleNode = root.get("codiceCatastale");
    String codiceCatastale = codiceCatastaleNode.asText();
    JsonNode nomeNode = root.get("nome");
    String nome = nomeNode.asText();
    JsonNode provinciaNode = root.get("provincia");
    String provincia = provinciaNode.asText();
    JsonNode codiciCapNode = root.get("codiciCap");
    Collection<String> codiciCap = new ArrayList<>();
    Iterator<JsonNode> capNodes = codiciCapNode.elements();
    while (capNodes.hasNext()) {
        JsonNode codiceCapNode = capNodes.next();
        String codiceCap = codiceCapNode.asText();

        codiciCap.add(codiceCap);/*  w ww .j a  v a2s. c  om*/
    }
    Comune comune = new Comune(codiceIstat, codiceCatastale, nome, provincia);
    comune.setCodiciCap(codiciCap);
    logger.trace("comune =" + comune + " deserializzato from json");
    return comune;
}

From source file:org.opennms.test.system.api.utils.RestClient.java

public String getDisplayVersion() {
    final WebTarget target = getTarget().path("info");
    final String json = getBuilder(target).get(String.class);

    final ObjectMapper mapper = new ObjectMapper();
    try {/*from  w  w  w  . java  2 s .  c  om*/
        JsonNode actualObj = mapper.readTree(json);
        return actualObj.get("displayVersion").asText();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.caratarse.auth.client.CaratarseAuthClient0100Test.java

private JsonNode toTree(String json) throws IOException {
    ObjectMapper m = new ObjectMapper();
    JsonNode userTree = m.readTree(json);
    return userTree;
}

From source file:org.caratarse.auth.client.CaratarseAuthClient0100Test.java

private JsonNode toTree(InputStream json) throws IOException {
    ObjectMapper m = new ObjectMapper();
    JsonNode userTree = m.readTree(json);
    return userTree;
}

From source file:org.n52.car.io.schema.Validation.java

private JsonNode createNode(Reader json) throws JsonProcessingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readTree(json);
}

From source file:eu.mondo.driver.mongo.util.MStatementDeserializer.java

@Override
public MStatement deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    String objectString = null;//from www  .j a va2s .  com
    String predicateString = null;
    String subjectString = null;

    BigInteger subjectBI;
    BigInteger predicateBI;
    BigInteger objectBI;

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(jp);

    subjectString = root.path("subject").textValue();
    predicateString = root.path("predicate").textValue();
    objectString = root.path("object").textValue();

    String subjectBIS = root.path("subjectBI").textValue();
    String predicateBIS = root.path("predicateBI").textValue();
    String objectBIS = root.path("objectBI").textValue();
    subjectBI = "".equals(subjectBIS) || subjectBIS == null ? new BigInteger("0", 10)
            : new BigInteger(subjectBIS, 16);
    predicateBI = "".equals(predicateBIS) || predicateBIS == null ? new BigInteger("0", 10)
            : new BigInteger(predicateBIS, 16);
    objectBI = "".equals(objectBIS) || objectBIS == null ? new BigInteger("0", 10)
            : new BigInteger(objectBIS, 16);

    //        URI subject = new URIImpl(subjectString);
    //        URI predicate = new URIImpl(predicateString);
    //
    //        Value object;
    //        try {
    //            object = new URIImpl(objectString);
    //        } catch (Exception e) {
    //            object = ValueFactoryImpl.getInstance().createLiteral(objectString);
    //        }

    jp.close();

    MStatement statement = new MStatement();
    statement.setSubject(subjectString);
    statement.setPredicate(predicateString);
    statement.setObject(objectString);
    statement.setSubjectBI(subjectBI);
    statement.setPredicateBI(predicateBI);
    statement.setObjectBI(objectBI);

    return statement;
}

From source file:org.n52.io.extension.MetadataJsonEntitiyTest.java

@Test
public void givenMetadataJsonEntity_whenSerialize_ValueAsJsonNode()
        throws JsonProcessingException, IOException {
    MetadataJsonEntity entity = new MetadataJsonEntity();
    entity.setPkid(1L);//from   w  w  w. jav a 2s .  c o  m
    entity.setName("some_metadata");
    entity.setSeriesId(1L);
    entity.setType("json");
    entity.setValue("{\"key\":\"value\",\"object\":{\"key1\":\"string\",\"key2\":42}}");

    ObjectMapper om = new ObjectMapper();
    String jsonString = om.writeValueAsString(entity);
    JsonNode jsonNode = om.readTree(jsonString);
    JsonNode at = jsonNode.path("value").path("object");
    Assert.assertTrue(at.isObject());
}

From source file:org.onosproject.segmentrouting.web.TunnelWebResource.java

/**
 * Create a new segment routing tunnel./*from   w  ww  .  jav a  2 s .  co  m*/
 *
 * @param input JSON stream for tunnel to create
 * @return status of the request - OK if the tunnel is created,
 * INTERNAL_SERVER_ERROR if the JSON is invalid or the tunnel cannot be created
 * @throws IOException if the JSON is invalid
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createTunnel(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input);
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
    srService.createTunnel(tunnelInfo);

    return Response.ok().build();
}

From source file:org.onosproject.segmentrouting.web.TunnelWebResource.java

/**
 * Delete a segment routing tunnel./*www .  j a  v a  2s . c  o  m*/
 *
 * @param input JSON stream for tunnel to delete
 * @return status of the request - OK if the tunnel is removed,
 * INTERNAL_SERVER_ERROR otherwise
 * @throws IOException if JSON is invalid
 */
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
public Response removeTunnel(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode tunnelJson = (ObjectNode) mapper.readTree(input);
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    Tunnel tunnelInfo = TUNNEL_CODEC.decode(tunnelJson, this);
    srService.removeTunnel(tunnelInfo);

    return Response.ok().build();
}