Example usage for com.fasterxml.jackson.databind.node ObjectNode toString

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode toString

Introduction

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

Prototype

public String toString() 

Source Link

Usage

From source file:com.ibm.watson.catalyst.corpus.tfidf.util.JsonUtil.java

public static void main(String[] args) {

    Term t = new Term("aterm", 100, 5.236);
    ObjectNode on = t.toObjectNode();
    System.out.println(on.toString());

}

From source file:com.ibm.watson.catalyst.corpus.tfidf.CorpusTfidf.java

public static void main(String[] args) {
    PROPERTIES = BaseProperties.setInstance(args, "sample/test.properties");

    String input = PROPERTIES.getProperty("input", "sample/test-check.json");

    TermCorpusBuilder cb = new TermCorpusBuilder();
    cb.setJson(input);//from w ww  .j  a  v  a2 s  .  c om

    System.out.println("Building corpus.");
    TermCorpus c = cb.build();
    System.out.println(c.size());

    System.out.println("Generating terms.");
    c.genTerms();
    System.out.println("Generating idfs.");
    c.genIdfs();
    System.out.println(c.numTerms());
    System.out.println("Terms generated.");

    ObjectNode tfidfs = getCorpusTfidfs(c);

    String output = PROPERTIES.getProperty("output", "sample/test-tfidf-output.json");
    try (BufferedWriter bw = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(output), "UTF-8"))) {
        bw.write(tfidfs.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:api.Events.java

public static Result finish(String id) {
    try {//from  w ww  .j a  v a  2 s .c  o  m
        ObjectNode o = EventsDBMapper.getInstance().markAsFinished(id);
        return ok(o.toString());
    } catch (Exception e) {
        Logger.error(e.getMessage(), e);
    }
    return notFound("Didn't find event with _id " + id);
}

From source file:com.redhat.lightblue.query.SortKey.java

public static SortKey fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_SORT, node.toString());
    }/* ww  w .ja  v  a2  s  .com*/
    String fieldString = node.fieldNames().next();
    String dir = node.get(fieldString).asText();
    Path field = new Path(fieldString);
    boolean desc = false;
    switch (dir) {
    case "$asc":
        desc = false;
        break;
    case "$desc":
        desc = true;
        break;
    default:
        throw Error.get(QueryConstants.ERR_INVALID_SORT, node.toString());
    }
    return new SortKey(field, desc);
}

From source file:com.redhat.lightblue.query.UnaryLogicalExpression.java

/**
 * Parses a unary logical expression using the given object node
 */// www. java2 s  .c  o m
public static UnaryLogicalExpression fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    String fieldName = node.fieldNames().next();
    UnaryLogicalOperator op = UnaryLogicalOperator.fromString(fieldName);
    if (op == null) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    QueryExpression q = QueryExpression.fromJson(node.get(fieldName));
    return new UnaryLogicalExpression(op, q);
}

From source file:eu.trentorise.opendata.commons.test.jackson.OdtJacksonTester.java

/**
* Converts {@code obj} to an {@link ObjectNode}, sets field
* {@code fieldName} to {@code newNode} and returns the json string
* representation of such new object. Also logs the json with the provided logger at FINE
* level./*w  ww . j  av  a2 s  .  com*/
*/
public static String changeField(ObjectMapper objectMapper, Logger logger, Object obj, String fieldName,
        JsonNode newNode) {
    checkNotNull(obj);
    checkNotEmpty(fieldName, "Invalid field name!");

    String string;
    try {
        string = objectMapper.writeValueAsString(obj);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException("Error while jacksonizing object to json node!", ex);
    }
    TreeNode treeNode;
    try {
        treeNode = (ObjectNode) objectMapper.readTree(string);
    } catch (IOException ex) {
        throw new RuntimeException("Error while creating json tree from serialized object:" + string, ex);
    }
    if (!treeNode.isObject()) {
        throw new OdtException(
                "The provided object was jacksonized to a string which does not represent a JSON object! String is "
                        + string);
    }
    ObjectNode jo = (ObjectNode) treeNode;
    jo.put(fieldName, newNode);

    String json = jo.toString();

    logger.log(Level.FINE, "converted json = {0}", json);

    return json;

}

From source file:com.redhat.lightblue.query.NaryLogicalExpression.java

/**
 * Parses an n-ary logical expression from the given json object
 *//*from   w  ww. ja  va 2 s  .co m*/
public static NaryLogicalExpression fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    String fieldName = node.fieldNames().next();
    NaryLogicalOperator op = NaryLogicalOperator.fromString(fieldName);
    if (op == null) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    JsonNode x = node.get(fieldName);
    if (x instanceof ArrayNode) {
        ArrayList<QueryExpression> list = new ArrayList<>(((ArrayNode) x).size());
        for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) {
            list.add(QueryExpression.fromJson(itr.next()));
        }
        return new NaryLogicalExpression(op, list);
    } else {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
}

From source file:org.opendaylight.nic.bgp.service.parser.BgpDataflowParser.java

public static String fromBgpDataFlow(final BgpDataflow bgpDataflow) {
    final ObjectMapper objectMapper = createObjectMapper();
    final ObjectNode ipv4NextHopNode = objectMapper.createObjectNode();
    final ObjectNode originNode = objectMapper.createObjectNode();
    final ObjectNode localPrefNode = objectMapper.createObjectNode();
    final ObjectNode asPathNode = objectMapper.createObjectNode();

    final ObjectNode attributesNode = objectMapper.createObjectNode();

    ipv4NextHopNode.put(GLOBAL, bgpDataflow.getGlobalIp().getValue());
    attributesNode.put(IPV4_NEXT_HOP, ipv4NextHopNode);
    attributesNode.put(AS_PATH, asPathNode);
    originNode.put(VALUE, ORIGIN_IGP);/*from   ww  w.j  a v a  2  s .co m*/
    attributesNode.put(ORIGIN, originNode);
    localPrefNode.put(PREF, FIXED_PREF);
    attributesNode.put(LOCAL_PREF, localPrefNode);

    final ObjectNode ipv4RouteAttributesNode = objectMapper.createObjectNode();
    ipv4RouteAttributesNode.put(PREFIX, bgpDataflow.getPrefix().getValue());
    ipv4RouteAttributesNode.put(PATH_ID, bgpDataflow.getPathId());
    ipv4RouteAttributesNode.put(ATTRIBUTES, attributesNode);

    final ArrayNode arrayNode = objectMapper.createArrayNode();
    arrayNode.add(ipv4RouteAttributesNode);

    final ObjectNode ipv4RouteNode = objectMapper.createObjectNode();
    ipv4RouteNode.put(IPV4_ROUTE, arrayNode);

    final ObjectNode bgpInetIpv4Routes = objectMapper.createObjectNode();
    bgpInetIpv4Routes.put(BGP_INET_IPV4_ROUTES, ipv4RouteNode);

    return bgpInetIpv4Routes.toString();
}

From source file:eu.trentorise.opendata.commons.test.jackson.TodJacksonTester.java

/**
 * Converts {@code obj} to an {@link ObjectNode}, sets field
 * {@code fieldName} to {@code newNode} and returns the json string
 * representation of such new object. Also logs the json with the provided
 * logger at FINE level.//from ww w  .j  a v  a 2  s  .  com
 */
public static String changeField(ObjectMapper objectMapper, Logger logger, Object obj, String fieldName,
        JsonNode newNode) {
    checkNotNull(obj);
    checkNotEmpty(fieldName, "Invalid field name!");

    String string;
    try {
        string = objectMapper.writeValueAsString(obj);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException("Error while jacksonizing object to json node!", ex);
    }
    TreeNode treeNode;
    try {
        treeNode = (ObjectNode) objectMapper.readTree(string);
    } catch (IOException ex) {
        throw new RuntimeException("Error while creating json tree from serialized object:" + string, ex);
    }
    if (!treeNode.isObject()) {
        throw new TodException(
                "The provided object was jacksonized to a string which does not represent a JSON object! String is "
                        + string);
    }
    ObjectNode jo = (ObjectNode) treeNode;
    jo.put(fieldName, newNode);

    String json = jo.toString();

    logger.log(Level.FINE, "converted json = {0}", json);

    return json;

}

From source file:dk.dbc.rawrepo.oai.ResumptionToken.java

/**
 * Encode a json object wit a timeout/*from  w  w w  . ja va2 s .  com*/
 *
 * @param obj        json
 * @param validHours timeout
 * @return base64 encoded string
 */
public static String encode(ObjectNode obj, int validHours) {
    if (obj == null) {
        return null;
    }
    long epoch = Instant.now().plus(validHours, ChronoUnit.HOURS).toEpochMilli() / 1000L;
    String format = String.format("%x%s", epoch, obj.toString());
    return PackText.encode(format);
}