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

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

Introduction

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

Prototype

public ObjectNode put(String paramString1, String paramString2) 

Source Link

Usage

From source file:org.apache.streams.jackson.CleanAdditionalPropertiesProcessor.java

public static void cleanAdditionalProperties(ObjectNode node) {
    if (node.get("additionalProperties") != null) {
        ObjectNode additionalProperties = (ObjectNode) node.get("additionalProperties");
        cleanAdditionalProperties(additionalProperties);
        Iterator<Map.Entry<String, JsonNode>> jsonNodeIterator = additionalProperties.fields();
        while (jsonNodeIterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = jsonNodeIterator.next();
            node.put(entry.getKey(), entry.getValue());
        }/* w  ww  .java 2s  .com*/
    }
}

From source file:controllers.api.v1.Metric.java

public static Result watchMetric(int id) {
    ObjectNode result = Json.newObject();
    String username = session("user");
    Map<String, String[]> params = request().body().asFormUrlEncoded();
    if (StringUtils.isNotBlank(username)) {
        String message = MetricsDAO.watchMetric(id, params, username);
        if (StringUtils.isBlank(message)) {
            result.put("status", "success");
        } else {/*from   ww w.ja va2  s  . c om*/
            result.put("status", "failed");
            result.put("message", message);
        }
    } else {
        result.put("status", "failed");
        result.put("message", "User is not authenticated");
    }

    return ok(result);
}

From source file:com.msopentech.odatajclient.testservice.utils.JSONUtilities.java

public static InputStream getJsonProperty(final InputStream src, final String[] path, final String edmType)
        throws Exception {

    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode srcNode = mapper.readTree(src);

    final ObjectNode property = new ObjectNode(JsonNodeFactory.instance);

    if (StringUtils.isNotBlank(edmType)) {
        property.put(JSON_ODATAMETADATA_NAME, ODATA_METADATA_PREFIX + edmType);
    }//from w  ww.  java 2  s  . co m

    JsonNode jsonNode = getJsonProperty(srcNode, path, 0);
    if (jsonNode.isObject()) {
        property.putAll((ObjectNode) jsonNode);
    } else {
        property.put("value", jsonNode.asText());
    }

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    mapper.writeValue(bos, property);

    final InputStream res = new ByteArrayInputStream(bos.toByteArray());
    IOUtils.closeQuietly(bos);

    return res;
}

From source file:org.raegdan.troca.Main.java

private static void printRatesAsJSON(HashMap<String, Double> rates, boolean fancy) {
    JsonNodeFactory factory = new JsonNodeFactory(true);
    ObjectNode rootNode = new ObjectNode(factory);

    for (Entry<String, Double> rate : rates.entrySet())
        rootNode.put(rate.getKey(), rate.getValue());

    try {/*ww  w.java 2 s .co  m*/
        out((fancy) ? new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(rootNode)
                : rootNode.toString(), true);
    } catch (JsonProcessingException e) {
        printException(e);
    }
}

From source file:me.tfeng.play.avro.AvroHelper.java

private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory)
        throws IOException {
    if (json.isObject() && schema.getType() == Type.RECORD) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        for (Field field : schema.getFields()) {
            String fieldName = field.name();
            if (node.has(fieldName)) {
                newNode.put(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory));
            } else if (field.defaultValue() != null) {
                newNode.put(fieldName, Json.parse(field.defaultValue().toString()));
            } else {
                newNode.put(fieldName, factory.nullNode());
            }/*from  w  ww .j  av a  2 s  .  co m*/
        }
        return newNode;
    } else if (json.isObject() && schema.getType() == Type.MAP) {
        ObjectNode node = (ObjectNode) json;
        ObjectNode newNode = factory.objectNode();
        Schema valueType = schema.getValueType();
        Iterator<Entry<String, JsonNode>> entries = node.fields();
        while (entries.hasNext()) {
            Entry<String, JsonNode> entry = entries.next();
            newNode.put(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory));
        }
        return newNode;
    } else if (schema.getType() == Type.UNION) {
        Schema type = AvroHelper.getSimpleUnionType(schema);
        if (type == null) {
            if (json.isNull()) {
                return json;
            } else {
                ObjectNode node = (ObjectNode) json;
                Entry<String, JsonNode> entry = node.fields().next();
                for (Schema unionType : schema.getTypes()) {
                    if (unionType.getFullName().equals(entry.getKey())) {
                        ObjectNode newNode = factory.objectNode();
                        newNode.put(entry.getKey(),
                                convertFromSimpleRecord(unionType, entry.getValue(), factory));
                        return newNode;
                    }
                }
                throw new IOException("Unable to get schema for type " + entry.getKey() + " in union");
            }
        } else if (json.isNull()) {
            return json;
        } else {
            ObjectNode newNode = factory.objectNode();
            newNode.put(type.getFullName(), convertFromSimpleRecord(type, json, factory));
            return newNode;
        }
    } else if (json.isArray() && schema.getType() == Type.ARRAY) {
        ArrayNode node = (ArrayNode) json;
        ArrayNode newNode = factory.arrayNode();
        Iterator<JsonNode> iterator = node.elements();
        while (iterator.hasNext()) {
            newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory));
        }
        return newNode;
    } else {
        return json;
    }
}

From source file:com.attribyte.essem.MGraphResponseGenerator.java

/**
 * Sets a field value, converting rates, if requested.
 * @param rateUnit The rate unit./* www .  j ava 2s . c  o m*/
 * @param parentObj The parent to which the field is added.
 * @param field The field name.
 * @param valueNode The input value node.
 */
private static void setFieldValue(RateUnit rateUnit, ObjectNode parentObj, String field, JsonNode valueNode) {

    if (rateUnit == ResponseGenerator.RAW_RATE_UNIT) {
        parentObj.set(field, valueNode);
    } else {
        if (!Fields.rateFields.contains(field)) {
            parentObj.set(field, valueNode);
        } else {
            parentObj.put(field, valueNode.doubleValue() * rateUnit.mult);
        }
    }
}

From source file:com.squarespace.template.plugins.CoreFormattersTest.java

protected static String getDateTestJson(long timestamp, String tzId) {
    DateTimeZone timezone = DateTimeZone.forID(tzId);
    ObjectNode node = JsonUtils.createObjectNode();
    node.put("time", timestamp);
    ObjectNode website = JsonUtils.createObjectNode();
    website.put("timeZoneOffset", timezone.getOffset(timestamp));
    website.put("timeZone", timezone.getID());
    node.put("website", website);
    return node.toString();
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * //ww w.  ja v  a2s .co  m
 * @param node
 * @param field
 * @param value
 */
public final static void setValue(ObjectNode node, String field, String value) {
    if (value == null || value.isEmpty()) {
        return;
    }
    node.put(field, value);
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * /*from w ww.  ja va  2  s . c  o m*/
 * @param node
 * @param field
 * @param value
 */
public final static void setValue(ObjectNode node, Enum<?> field, String value) {
    if (value == null || value.isEmpty()) {
        return;
    }
    node.put(field.name(), value);
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * /*  w ww . jav a2  s .co m*/
 * @param node
 * @param field
 * @param value
 */
public final static void setValue(ObjectNode node, String field, byte[] value) {
    if (value == null || value.length == 0) {
        return;
    }
    node.put(field, value);
}