Example usage for com.fasterxml.jackson.databind JsonNode toString

List of usage examples for com.fasterxml.jackson.databind JsonNode toString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode toString.

Prototype

public abstract String toString();

Source Link

Usage

From source file:gate.corpora.twitter.TweetUtils.java

public static Object process(JsonNode node) {
    /* JSON types: number, string, boolean, array, object (dict/map),
     * null.  All map keys are strings.//  w  w w.  j  a  v  a  2s  .c om
     */

    if (node.isBoolean()) {
        return node.asBoolean();
    }
    if (node.isDouble()) {
        return node.asDouble();
    }
    if (node.isInt()) {
        return node.asInt();
    }
    if (node.isTextual()) {
        return node.asText();
    }

    if (node.isNull()) {
        return null;
    }

    if (node.isArray()) {
        List<Object> list = new ArrayList<Object>();
        for (JsonNode item : node) {
            list.add(process(item));
        }
        return list;
    }

    if (node.isObject()) {
        FeatureMap map = Factory.newFeatureMap();
        Iterator<String> keys = node.fieldNames();
        while (keys.hasNext()) {
            String key = keys.next();
            map.put(key, process(node.get(key)));
        }
        return map;
    }

    return node.toString();
}

From source file:org.springframework.cloud.cloudfoundry.discovery.CloudFoundryDiscoveryClient.java

public CloudFoundryDiscoveryClient(CloudFoundryClient cloudFoundryClient, Environment environment) {

    this.cloudFoundryClient = cloudFoundryClient;

    String vcapApplication = environment.getProperty("VCAP_APPLICATION");

    try {//from   ww  w.  j  av  a2  s .c  o m
        JsonNode jsonNode = objectMapper.readTree(vcapApplication);
        JsonNode appNameNode = jsonNode.get("application_name");

        this.vcapApplicationName = appNameNode.toString().replaceAll("\"", "");

        log.debug("Current ServiceInstance information...");
        log.debug("\tvcapApplicationName: " + this.vcapApplicationName);

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint.java

public void processResult(JsonNode response) throws NoSuchMethodException {

    logger.trace("Response : {}", response.toString());
    CallContext returnCtxt = methodContext.get(response.get("id").asText());
    if (returnCtxt == null) {
        return;//from   w w  w .j a v  a  2 s  .c om
    }

    if (ListenableFuture.class == returnCtxt.getMethod().getReturnType()) {
        TypeToken<?> retType = TypeToken.of(returnCtxt.getMethod().getGenericReturnType())
                .resolveType(ListenableFuture.class.getMethod("get").getGenericReturnType());
        JavaType javaType = TypeFactory.defaultInstance().constructType(retType.getType());

        JsonNode result = response.get("result");
        Object result1 = objectMapper.convertValue(result, javaType);
        JsonNode error = response.get("error");
        if (error != null && !error.isNull()) {
            logger.error("Error : {}", error.toString());
        }

        returnCtxt.getFuture().set(result1);

    } else {
        throw new UnexpectedResultException("Don't know how to handle this");
    }
}

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

/**
 * {@inheritDoc}//from   w w w  .  jav a 2s  . c om
 */
@Override
public SchemaOption deserialize(JsonParser parser, DeserializationContext context) throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    JsonNode schemaNode = mapper.readTree(parser);
    SchemaOption returnSchema = null;
    if (schemaNode.isInt()) {
        returnSchema = new SchemaOption(schemaNode.asLong());
    } else {
        String schemaString = schemaNode.toString();
        returnSchema = new SchemaOption(schemaString);
    }

    return returnSchema;
}

From source file:ws.wamp.jawampa.transport.netty.WampSerializationHandler.java

@Override
protected void encode(ChannelHandlerContext ctx, WampMessage msg, List<Object> out) throws Exception {
    ByteBuf msgBuffer = Unpooled.buffer();
    ByteBufOutputStream outStream = new ByteBufOutputStream(msgBuffer);
    ObjectMapper objectMapper = serialization.getObjectMapper();
    try {//from ww w .  j  a  v  a 2  s  . c o m
        JsonNode node = msg.toObjectArray(objectMapper);
        objectMapper.writeValue(outStream, node);

        if (logger.isDebugEnabled()) {
            logger.debug("Serialized Wamp Message: {}", node.toString());
        }

    } catch (Exception e) {
        msgBuffer.release();
        return;
    }

    if (serialization.isText()) {
        TextWebSocketFrame frame = new TextWebSocketFrame(msgBuffer);
        out.add(frame);
    } else {
        BinaryWebSocketFrame frame = new BinaryWebSocketFrame(msgBuffer);
        out.add(frame);
    }
}

From source file:org.zalando.stups.swagger.codegen.YamlToJson.java

protected String getYamlFileContentAsJson() throws IOException {
    String data = "";
    if (yamlInputPath.startsWith("http") || yamlInputPath.startsWith("https")) {

        data = new String(Resources.toByteArray(new URL(yamlInputPath)));
    } else {//  w  w w.  ja va 2  s.c o m
        data = new String(Files.readAllBytes(java.nio.file.Paths.get(new File(yamlInputPath).toURI())));
    }

    ObjectMapper yamlMapper = Yaml.mapper();
    JsonNode rootNode = yamlMapper.readTree(data);

    // must have swagger node set
    JsonNode swaggerNode = rootNode.get("swagger");

    return rootNode.toString();
}

From source file:fr.gouv.vitam.mdbes.CouchbaseAccess.java

/**
 * Set the JsonNode as Id in database//from   w  w  w . j  a v a  2s  .c o  m
 * @param id
 * @param node
 * @param ttl time to live in seconds
 * @return True if OK
 */
public final boolean setToId(final String id, final JsonNode node, final int ttl) {
    final String nid = createDigest(id);
    String value = node.toString();
    try {
        return client.set(nid, ttl, value).get();
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.error(e);
        return false;
    }
}

From source file:com.redhat.lightblue.metadata.types.BigIntegerType.java

@Override
public Object fromJson(JsonNode node) {
    if (node.isValueNode()) {
        return node.bigIntegerValue();
    } else {/*from  w ww  .  j a  v  a 2  s . co m*/
        throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString());
    }
}

From source file:com.redhat.lightblue.metadata.types.BigDecimalType.java

@Override
public Object fromJson(JsonNode node) {
    if (node.isValueNode()) {
        return node.decimalValue();
    } else {//from  ww w .  java  2 s.c o m
        throw Error.get(NAME, MetadataConstants.ERR_INCOMPATIBLE_VALUE, node.toString());
    }
}

From source file:com.redhat.lightblue.eval.ArrayRangeProjectorTest.java

@Test
public void array_range_projection_with_match() throws Exception {
    Projection p = EvalTestContext//  w w  w  . java2 s.c  om
            .projectionFromJson("{'field':'field7','range':[1,2],'project':{'field':'elemf3'}}");
    Projector projector = Projector.getInstance(p, md);
    JsonNode expectedNode = JsonUtils.json("{'field7':[{'elemf3':4},{'elemf3':5}]}".replace('\'', '\"'));

    JsonDoc pdoc = projector.project(jsonDoc, JSON_NODE_FACTORY);

    Assert.assertEquals(expectedNode.toString(), pdoc.toString());
}