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:org.xbmc.kore.utils.JsonUtils.java

public static List<Integer> integerListFromJsonNode(JsonNode node, String key) {
    if (node == null)
        return new ArrayList<Integer>(0);
    JsonNode value = node.get(key);
    if (value == null)
        return new ArrayList<Integer>(0);

    ArrayList<Integer> result;
    if (value.isArray()) {
        ArrayNode arrayNode = (ArrayNode) value;
        result = new ArrayList<Integer>(arrayNode.size());
        for (JsonNode innerNode : arrayNode) {
            result.add(innerNode.asInt());
        }/*from   w  w  w .  j  a  v a  2  s.  c  om*/
    } else {
        result = new ArrayList<Integer>(1);
        result.add(value.asInt());
    }
    return result;
}

From source file:com.syncedsynapse.kore2.utils.JsonUtils.java

public static int intFromJsonNode(JsonNode node, String key) {
    // Duplicate code for performance resons
    if (node == null)
        return 0;
    JsonNode value = node.get(key);
    if (value == null)
        return 0;
    return value.asInt();
}

From source file:services.TodoService.java

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

    Promise<List<Todo>> jsonPromise = req.get().map(new Function<WSResponse, List<Todo>>() {
        public List<Todo> apply(WSResponse response) {
            JsonNode json = response.asJson();
            ArrayNode results = (ArrayNode) json;
            List<Todo> todos = new ArrayList<Todo>();
            Iterator<JsonNode> it = results.iterator();

            while (it.hasNext()) {
                JsonNode node = it.next();
                Todo todo = new Todo();
                todo.setId(node.get("id").asLong());
                todo.setDescription(node.get("description").asText());
                todo.setUsername(node.get("username").asText());
                todos.add(todo);//from  w  w w .ja v a2 s. c o  m
            }

            return todos;
        }
    });
    return jsonPromise.get(5000);
}

From source file:com.maxpowa.chime.util.UpdateChecker.java

public static void checkUpdate() {
    try {//from w w w  .  j a v  a 2  s .c  om
        URLConnection versionData = new URL("http://widget.mcf.li/223265.json").openConnection();
        JsonNode object = mapper
                .readTree(new BufferedReader(new InputStreamReader(versionData.getInputStream())));
        // Utils.log.info(object.toString());
        if (object.get("versions").has(Chime.MC_VERSION)) {
            latest = mapper.treeToValue(object.get("versions").get(Chime.MC_VERSION).get(0),
                    ChimeVersion.class);
            // Utils.log.info(object.get("versions").get(Chime.MC_VERSION).get(0).toString());
        } else {
            Utils.log.info(
                    "Update checker was unable to find a release for this Minecraft version, showing latest release.");
            // Utils.log.info(object.get("download").toString());
            latest = mapper.treeToValue(object.get("download"), ChimeVersion.class);
        }
    } catch (IOException e) {
        Utils.log.error("Update checker failed to check for new version.", e);
    }
}

From source file:com.syncedsynapse.kore2.utils.JsonUtils.java

public static String stringFromJsonNode(JsonNode node, String key) {
    // Duplicate code for performance resons
    if (node == null)
        return null;
    JsonNode value = node.get(key);
    if (value == null)
        return null;
    return value.textValue();
}

From source file:org.fcrepo.importexport.common.BagProfile.java

/**
 * Loads required tags and allowed values
 *
 * @param json json to parse//from  w w w  .  jav a  2 s  .  com
 * @param key key in json to load tags from
 * @return map of tags => set of allowed values
 */
private static Map<String, Set<String>> metadataFields(final JsonNode json, final String key) {
    final JsonNode fields = json.get(key);

    if (fields == null) {
        return null;
    }

    final Map<String, Set<String>> results = new HashMap<>();
    for (final java.util.Iterator<String> it = fields.fieldNames(); it.hasNext();) {
        final String name = it.next();
        final JsonNode field = fields.get(name);

        if (field.get("required") != null && field.get("required").asBoolean()) {
            results.put(name, arrayValues(field, "values"));
        }
    }

    return results;
}

From source file:org.jboss.aerogear.simplepush.server.netty.standalone.ConfigReader.java

private static SimplePushServerConfig parseSimplePushProperties(final JsonNode json) {
    final JsonNode host = json.get("host");
    final JsonNode port = json.get("port");
    final Builder builder = DefaultSimplePushConfig.create(host.asText(), port.asInt());
    final JsonNode password = json.get("password");
    if (password != null) {
        builder.password(password.asText());
    }/*from  w  ww.j  a va2 s. c om*/
    final JsonNode useragentReaperTimeout = json.get("useragent-reaper-timeout");
    if (useragentReaperTimeout != null) {
        builder.userAgentReaperTimeout(useragentReaperTimeout.asLong());
    }
    final JsonNode endpointHost = json.get("endpoint-host");
    if (endpointHost != null) {
        builder.endpointHost(endpointHost.asText());
    }
    final JsonNode endpointPort = json.get("endpoint-port");
    if (endpointPort != null) {
        builder.endpointPort(endpointPort.asInt());
    }
    final JsonNode endpointTls = json.get("endpoint-tls");
    if (endpointTls != null) {
        builder.endpointTls(endpointTls.asBoolean());
    }
    final JsonNode endpointPrefix = json.get("endpoint-prefix");
    if (endpointPrefix != null) {
        builder.endpointPrefix(endpointPrefix.asText());
    }
    final JsonNode ackInterval = json.get("ack-interval");
    if (ackInterval != null) {
        builder.ackInterval(ackInterval.asLong());
    }
    final JsonNode notifierMaxThreads = json.get("notifier-max-threads");
    if (notifierMaxThreads != null) {
        builder.notifierMaxThreads(notifierMaxThreads.asInt());
    }
    return builder.build();
}

From source file:services.TodoService.java

public static List<Todo> findAll(String accessToken) {
    WSRequestHolder req = WS.url(OAUTH_RESOURCE_SERVER_URL + "/rest/todos");
    req.setHeader("Authorization", "Bearer " + accessToken);

    Promise<List<Todo>> jsonPromise = req.get().map(new Function<WSResponse, List<Todo>>() {
        public List<Todo> apply(WSResponse response) {
            JsonNode json = response.asJson();
            ArrayNode results = (ArrayNode) json;
            List<Todo> todos = new ArrayList<Todo>();
            Iterator<JsonNode> it = results.iterator();

            while (it.hasNext()) {
                JsonNode node = it.next();
                Todo todo = new Todo();
                todo.setDescription(node.get("description").asText());
                todos.add(todo);//from w ww.  j  ava 2  s .  c  o  m
            }

            return todos;
        }
    });
    return jsonPromise.get(5000);
}

From source file:org.hawkular.services.rest.test.InventoryHelper.java

private static Optional<ExtendedInventoryStructure> rebuildFromChunks(JsonNode dataNode) {
    if (!dataNode.has(0)) {
        return Optional.empty();
    }//from w w w. j  a v a2s .c  om
    try {
        JsonNode masterNode = dataNode.get(0);
        final byte[] all;
        if (masterNode.has("tags") && masterNode.get("tags").has("chunks")) {
            int nbChunks = masterNode.get("tags").get("chunks").asInt();
            int totalSize = masterNode.get("tags").get("size").asInt();
            byte[] master = masterNode.get("value").binaryValue();
            if (master.length == 0) {
                return Optional.empty();
            }
            all = new byte[totalSize];
            int pos = 0;
            System.arraycopy(master, 0, all, pos, master.length);
            pos += master.length;
            for (int i = 1; i < nbChunks; i++) {
                JsonNode slaveNode = dataNode.get(i);
                byte[] slave = slaveNode.get("value").binaryValue();
                System.arraycopy(slave, 0, all, pos, slave.length);
                pos += slave.length;
            }
        } else {
            // Not chunked
            all = masterNode.get("value").binaryValue();
        }
        String decompressed = decompress(all);
        ExtendedInventoryStructure structure = MAPPER.readValue(decompressed, ExtendedInventoryStructure.class);
        return Optional.of(structure);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.meetingninja.csse.database.ContactDatabaseAdapter.java

public static List<Contact> getContacts(String userID) throws IOException {
    // Server URL setup
    String _url = getBaseUri().appendPath(userID).build().toString();
    // Establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    // add request header

    conn.setRequestMethod(IRequest.GET);
    addRequestHeader(conn, false);//from   w ww .  ja va  2 s .  co m
    // Get server response
    int responseCode = conn.getResponseCode();
    String response = getServerResponse(conn);
    List<Contact> contacts = new ArrayList<Contact>();
    List<String> contactIds = new ArrayList<String>();
    List<String> relationIds = new ArrayList<String>();
    final JsonNode contactsArray = MAPPER.readTree(response).get(Keys.User.CONTACTS);
    if (contactsArray.isArray()) {
        for (final JsonNode userNode : contactsArray) {
            relationIds.add(userNode.get(Keys.User.RELATIONID).asText());
            contactIds.add(userNode.get(Keys.User.CONTACTID).asText());
        }
    }

    conn.disconnect();
    for (int i = 0; i < contactIds.size(); i++) {

        User contact = UserDatabaseAdapter.getUserInfo(contactIds.get(i));
        if (contact != null) {
            Contact oneContact = new Contact(contact, relationIds.get(i));
            contacts.add(oneContact);
        }
    }
    return contacts;
}