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

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

Introduction

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

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:services.TodoService.java

public static Todo add(String accessToken, String username, String description) {
    WSRequestHolder req = WS.url(OAUTH_RESOURCE_SERVER_URL + "/rest/todos/add");
    req.setHeader("Authorization", "Bearer " + accessToken);

    JsonNode json = Json.newObject().put("username", username).put("description", description);

    Promise<Todo> jsonPromise = req.post(json).map(new Function<WSResponse, Todo>() {
        public Todo apply(WSResponse response) {
            JsonNode json = response.asJson();
            Todo todo = new Todo();
            todo.setId(json.get("id").asLong());
            todo.setDescription(json.get("description").asText());
            todo.setUsername(json.get("username").asText());
            return todo;
        }//w w w.ja  va 2  s . c  o  m
    });

    return jsonPromise.get(5000);
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static ArrayNode getItemVariantOptions(JsonNode item) {
    JsonNode structuredContent = item.path("structuredContent");
    JsonNode variants = structuredContent.path("variants");
    if (variants.size() <= 1) {
        return EMPTY_ARRAY;
    }/*from  www  .ja  v a2 s  .c  om*/

    ArrayNode userDefinedOptions = JsonUtils.createArrayNode();
    JsonNode ordering = structuredContent.path("variantOptionOrdering");
    for (int i = 0; i < ordering.size(); i++) {
        String optionName = ordering.path(i).asText();
        ObjectNode option = JsonUtils.createObjectNode();
        option.put("name", optionName);
        option.put("values", JsonUtils.createArrayNode());
        userDefinedOptions.add(option);
    }

    for (int i = 0; i < variants.size(); i++) {
        JsonNode variant = variants.path(i);
        JsonNode attributes = variant.get("attributes");
        if (attributes == null) {
            continue;
        }
        Iterator<String> fields = attributes.fieldNames();

        while (fields.hasNext()) {
            String field = fields.next();

            String variantOptionValue = attributes.get(field).asText();
            ObjectNode userDefinedOption = null;

            for (int j = 0; j < userDefinedOptions.size(); j++) {
                ObjectNode current = (ObjectNode) userDefinedOptions.get(j);
                if (current.get("name").asText().equals(field)) {
                    userDefinedOption = current;
                }
            }

            if (userDefinedOption != null) {
                boolean hasOptionValue = false;
                ArrayNode optionValues = (ArrayNode) userDefinedOption.get("values");
                for (int k = 0; k < optionValues.size(); k++) {
                    String optionValue = optionValues.get(k).asText();
                    if (optionValue.equals(variantOptionValue)) {
                        hasOptionValue = true;
                        break;
                    }
                }

                if (!hasOptionValue) {
                    optionValues.add(variantOptionValue);
                }
            }
        }
    }
    return userDefinedOptions;
}

From source file:de.unikonstanz.winter.crossref.node.doi.CrossrefDoiNodeModel.java

private static DataCell nodeToDateCell(final JsonNode node) {
    if (CrossrefUtil.isNull(node)) {
        return new MissingCell(null);
    }/*w ww .  ja v  a 2  s. co m*/
    JsonNode timestamp = node.get("timestamp");
    if (CrossrefUtil.isNull(timestamp)) {
        return new MissingCell(null);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp.asLong());
    return new DateAndTimeCell(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND));
}

From source file:net.sourceforge.jwbf.mediawiki.MediaWiki.java

public static void checkResponseForError(String response) {
    JsonNode node = mapper.toJsonNode(response);
    node = node.path("error");
    if (!node.isMissingNode()) {
        throw new ApiException(node.get("code").asText(), node.get("info").asText());
    }/*from   w w  w .j  av a  2 s.co m*/
}

From source file:org.bndtools.rt.repository.marshall.CapReqJson.java

private static void toAttribute(CapReqBuilder builder, JsonNode attrNode) throws IllegalArgumentException {
    String name = getRequiredValueField(attrNode, "name").asText();
    Object value;/*from ww w .  ja  v a  2s.  c o  m*/
    JsonNode valueNode = getRequiredValueField(attrNode, "value");

    JsonNode typeNode = attrNode.get("type");
    AttributeType type = AttributeType.DEFAULT;
    if (typeNode != null) {
        String typeName = typeNode.asText();
        type = AttributeType.parseTypeName(typeName);
    }

    if (valueNode.isFloatingPointNumber()) {
        if (typeNode == null || type.equals(AttributeType.DOUBLE))
            value = valueNode.asDouble();
        else
            throw new IllegalArgumentException(String
                    .format("JSON type for value is floating point, does not match declared type '%s'", type));
    } else if (valueNode.isIntegralNumber()) {
        if (typeNode == null || type.equals(AttributeType.LONG))
            value = valueNode.asLong();
        else if (type.equals(AttributeType.DOUBLE))
            value = valueNode.asDouble();
        else
            throw new IllegalArgumentException(
                    String.format("JSON type for value is integral, does not match declared type '%s'", type));
    } else if (valueNode.isTextual()) {
        String valueStr = valueNode.asText();
        value = type.parseString(valueStr);
    } else {
        throw new IllegalArgumentException("JSON value node is not a recognised type");
    }

    builder.addAttribute(name, value);
}

From source file:com.baasbox.service.scripting.ScriptingService.java

private static ODocument createScript(ScriptsDao dao, JsonNode node) throws ScriptException {
    updateCacheVersion();//ww  w  . ja  va2s . c om
    String lang = node.get(ScriptsDao.LANG).asText();
    ScriptLanguage language = ScriptLanguage.forName(lang);
    String name = node.get(ScriptsDao.NAME).asText();
    String code = node.get(ScriptsDao.CODE).asText();
    JsonNode initialStorage = node.get(ScriptsDao.LOCAL_STORAGE);
    JsonNode library = node.get(ScriptsDao.LIB);
    boolean isLibrary = library == null ? false : library.asBoolean();
    JsonNode activeNode = node.get(ScriptsDao.ACTIVE);
    boolean active = activeNode == null ? false : activeNode.asBoolean();
    JsonNode encoded = node.get(ScriptsDao.ENCODED);
    ODocument doc;
    if (encoded != null && encoded.isTextual()) {
        String encodedValue = encoded.asText();
        doc = dao.create(name, language.name, code, isLibrary, active, initialStorage, encodedValue);
    } else {
        doc = dao.create(name, language.name, code, isLibrary, active, initialStorage);
    }
    return doc;
}

From source file:dk.kk.ibikecphlib.util.HttpUtils.java

public static Message JSONtoMessage(JsonNode result) {
    Message ret = new Message();
    Bundle data = new Bundle();
    if (result != null) {
        if (result.has("success"))
            data.putBoolean("success", result.get("success").asBoolean());
        if (result.has("info"))
            data.putString("info", result.get("info").asText());
        if (result.has("has_password"))
            data.putBoolean("has_password", result.get("has_password").asBoolean());
        if (result.has("invalid_token")) {
            if (result.get("invalid_token").asBoolean()) {
                data.putBoolean("invalid_token", result.get("invalid_token").asBoolean());
                IBikeApplication.logoutWrongToken();
            }/*from  w  ww.j  a v a2 s. com*/
        }
        /*if (result.has("errors"))
        data.putString("errors", result.get("errors").get(0).asText());*/
    }
    if (result != null && result.has("data")) {
        JsonNode dataNode = result.get("data");
        if (dataNode != null) {
            if (dataNode.has("id"))
                data.putInt("id", dataNode.get("id").asInt());
            if (dataNode.has("auth_token"))
                data.putString("auth_token", dataNode.get("auth_token").asText());
            if (dataNode.has("signature"))
                data.putString("signature", dataNode.get("signature").asText());
            if (dataNode.has("provider"))
                data.putString("provider", dataNode.get("provider").asText());
            /*if (dataNode.has("errors"))
                data.putString("errors", dataNode.get("errors").get(0).asText());*/
        }
    }

    ret.setData(data);
    return ret;
}

From source file:com.ikanow.aleph2.data_import_manager.utils.BeanDiffUtils.java

public static Map<String, Boolean> createDataBucketBeanDiffs(DataBucketBean updated_bucket,
        DataBucketBean original_bucket) {
    final JsonNode updated_bucket_jsonnode = BeanTemplateUtils.toJson(updated_bucket);
    final JsonNode original_bucket_jsonnode = BeanTemplateUtils.toJson(original_bucket);
    final Map<String, Boolean> diff = new HashMap<String, Boolean>();

    //this only reports back fields that are not null, so we have to run both directions
    original_bucket_jsonnode.fields().forEachRemaining(field -> {
        diff.put(field.getKey(), !(field.getValue().equals(updated_bucket_jsonnode.get(field.getKey()))));
    });//from  w  w w.  j  a  va 2  s . c o  m
    updated_bucket_jsonnode.fields().forEachRemaining(field -> {
        //if the field wasn't already checked, it means it only exists here, don't bother don't expensive compares
        if (!diff.containsKey(field.getKey()))
            diff.put(field.getKey(), true);
    });

    return diff;
}

From source file:com.smartsheet.tin.filters.common.JsonFilterTools.java

/**
 * Fetch a child node from a JSON object node.
 * //w  w  w. j  a v  a  2s . c  o m
 * @param parent The parent JSON object node.
 * @param node_name The name of the child node.
 * @param node_type array, object, string, number, container, value, or bool
 * @return The child node.
 * @throws JsonFilterException
 */
public static JsonNode fetchChildByName(JsonNode parent, String node_name, String node_type)
        throws JsonFilterException {
    if (JsonNodeType.OBJECT != parent.getNodeType()) {
        String err = String.format("parent must be an Object/{}/hash type");
        throw new JsonFilterException(err);
    }

    if (parent.hasNonNull(node_name)) {
        JsonNode child = parent.get(node_name);
        boolean type_error = false;

        if (node_type == null || node_type.isEmpty()) {
            // Do no validation on the child type.
            type_error = false;
        } else {
            if (node_type.equals("array")) {
                if (!child.isArray()) {
                    type_error = true;
                }
            } else if (node_type.equals("object")) {
                if (!child.isObject()) {
                    type_error = true;
                }
            } else if (node_type.equals("string")) {
                if (!child.isTextual()) {
                    type_error = true;
                }
            } else if (node_type.equals("number")) {
                if (!child.isNumber()) {
                    type_error = true;
                }
            } else if (node_type.equals("container")) {
                if (!child.isContainerNode()) {
                    type_error = true;
                }
            } else if (node_type.equals("value")) {
                if (!child.isValueNode()) {
                    type_error = true;
                }
            } else if (node_type.startsWith("bool")) {
                if (!child.isBoolean()) {
                    type_error = true;
                }
            }
        }
        if (type_error == true) {
            String err = String.format("Type error: child node '%s' " + "is not a %s", node_name, node_type);
            throw new JsonFilterException(err);
        }
        return child;
    } else {
        String err = String.format("Child node '%s', type: '%s' not found", node_name, node_type);
        throw new JsonFilterChildNotFound(err);
    }
}

From source file:models.metadata.ClimateService.java

/**
 * Generate the list of all sensor categories
 *
 * @return a list of all the sensor categories
 *//*from w  w w. j  a  v a 2 s .com*/
public static List<ClimateService> all() {

    List<ClimateService> climateServices = new ArrayList<ClimateService>();

    JsonNode climateServicesNode = APICall.callAPI(GET_CLIMATE_SERVICES_CALL);

    if (climateServicesNode == null || climateServicesNode.has("error") || !climateServicesNode.isArray()) {
        return climateServices;
    }

    for (int i = 0; i < climateServicesNode.size(); i++) {
        JsonNode json = climateServicesNode.path(i);
        ClimateService newService = new ClimateService();
        newService.setId(json.path("id").asText());
        newService.setClimateServiceName(json.get("name").asText());
        newService.setPurpose(json.path("purpose").asText());
        newService.setUrl(json.path("url").asText());
        //newService.setCreateTime(json.path("createTime").asText());
        newService.setScenario(json.path("scenario").asText());
        newService.setVersion(json.path("versionNo").asText());
        newService.setRootservice(json.path("rootServiceId").asText());
        climateServices.add(newService);
    }
    return climateServices;
}