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

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

Introduction

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

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

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

/**
 * Parses an n-ary logical expression from the given json object
 *//*from  ww w.  j  ava 2  s. c o  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.jetbrains.webdemo.examples.ExamplesLoader.java

private static Example loadExample(String path, String parentUrl, boolean loadTestVersion,
        List<ObjectNode> commonFilesManifests) throws IOException {
    File manifestFile = new File(path + File.separator + "manifest.json");
    try (BufferedInputStream reader = new BufferedInputStream(new FileInputStream(manifestFile))) {
        ObjectNode manifest = (ObjectNode) JsonUtils.getObjectMapper().readTree(reader);

        String name = new File(path).getName();
        String id = (parentUrl + name).replaceAll(" ", "%20");
        String args = manifest.has("args") ? manifest.get("args").asText() : "";
        String runConfiguration = manifest.get("confType").asText();
        boolean searchForMain = manifest.has("searchForMain") ? manifest.get("searchForMain").asBoolean()
                : true;//from w  w  w  .  j  ava 2  s.  c o  m
        String expectedOutput;
        List<String> readOnlyFileNames = new ArrayList<>();
        List<ProjectFile> files = new ArrayList<>();
        List<ProjectFile> hiddenFiles = new ArrayList<>();
        if (manifest.has("expectedOutput")) {
            expectedOutput = manifest.get("expectedOutput").asText();
        } else if (manifest.has("expectedOutputFile")) {
            Path expectedOutputFilePath = Paths
                    .get(path + File.separator + manifest.get("expectedOutputFile").asText());
            expectedOutput = new String(Files.readAllBytes(expectedOutputFilePath));
        } else {
            expectedOutput = null;
        }
        String help = null;
        File helpFile = new File(path + File.separator + "task.md");
        if (helpFile.exists()) {
            PegDownProcessor processor = new PegDownProcessor(org.pegdown.Extensions.FENCED_CODE_BLOCKS);
            String helpInMarkdown = new String(Files.readAllBytes(helpFile.toPath()));
            help = new GFMNodeSerializer().toHtml(processor.parseMarkdown(helpInMarkdown.toCharArray()));
        }

        List<JsonNode> fileManifests = new ArrayList<JsonNode>();
        if (manifest.has("files")) {
            for (JsonNode fileManifest : manifest.get("files")) {
                fileManifests.add(fileManifest);
            }
        }
        fileManifests.addAll(commonFilesManifests);
        for (JsonNode fileDescriptor : fileManifests) {

            if (loadTestVersion && fileDescriptor.has("skipInTestVersion")
                    && fileDescriptor.get("skipInTestVersion").asBoolean()) {
                continue;
            }

            String filePath = fileDescriptor.has("path") ? fileDescriptor.get("path").asText()
                    : path + File.separator + fileDescriptor.get("filename").textValue();
            ExampleFile file = loadExampleFile(filePath, id, fileDescriptor);
            if (!loadTestVersion && file.getType().equals(ProjectFile.Type.SOLUTION_FILE)) {
                continue;
            }
            if (!file.isModifiable()) {
                readOnlyFileNames.add(file.getName());
            }

            if (file.isHidden()) {
                hiddenFiles.add(file);
            } else {
                files.add(file);
            }
        }
        loadDefaultFiles(path, id, files, loadTestVersion);

        return new Example(id, name, args, runConfiguration, id, expectedOutput, searchForMain, files,
                hiddenFiles, readOnlyFileNames, help);
    } catch (IOException e) {
        System.err.println("Can't load project: " + e.toString());
        return null;
    }
}

From source file:org.apache.streams.data.util.PropertyUtil.java

public static ObjectNode unflattenObjectNode(ObjectNode flatObject, char seperator) {
    ObjectNode root = mapper.createObjectNode();
    Iterator<Map.Entry<String, JsonNode>> iter = flatObject.fields();
    while (iter.hasNext()) {
        Map.Entry<String, JsonNode> item = iter.next();
        String fullKey = item.getKey();
        if (!fullKey.contains(Character.valueOf(seperator).toString())) {
            root.put(item.getKey(), item.getValue());
        } else {/*from   w  w  w  .j a va 2  s.co  m*/
            ObjectNode currentNode = root;
            List<String> keyParts = Splitter.on(seperator).splitToList(item.getKey());
            Iterator<String> keyPartIterator = Iterables
                    .limit(Splitter.on(seperator).split(item.getKey()), keyParts.size() - 1).iterator();
            while (keyPartIterator.hasNext()) {
                String part = keyPartIterator.next();
                if (currentNode.has(part) && currentNode.get(part).isObject()) {
                    currentNode = (ObjectNode) currentNode.get(part);
                } else {
                    ObjectNode newNode = mapper.createObjectNode();
                    currentNode.put(part, newNode);
                    currentNode = newNode;
                }
            }
            ;
            currentNode.put(keyParts.get(keyParts.size() - 1), item.getValue());

        }
    }
    return root;
}

From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java

public static MongoCredential credentialFromJson(ObjectNode node) {
    String userName = null;//from  ww  w .j a v  a 2s .  c  o  m
    String password = null;
    String source = null;

    JsonNode xnode = node.get("mechanism");
    if (xnode == null) {
        throw new IllegalArgumentException("mechanism is required in credentials");
    }
    String mech = xnode.asText();
    xnode = node.get("userName");
    if (xnode != null) {
        userName = xnode.asText();
    }
    xnode = node.get("password");
    if (xnode != null) {
        password = xnode.asText();
    }
    xnode = node.get("source");
    if (xnode != null) {
        source = xnode.asText();
    }

    MongoCredential cr = null;
    if (null != mech) {
        switch (mech) {
        case "GSSAPI_MECHANISM":
            cr = MongoCredential.createGSSAPICredential(userName);
            break;
        case "MONGODB_CR_MECHANISM":
            cr = MongoCredential.createMongoCRCredential(userName, source,
                    password == null ? null : password.toCharArray());
            break;
        case "MONGODB_X509_MECHANISM":
            cr = MongoCredential.createMongoX509Credential(userName);
            break;
        case "PLAIN_MECHANISM":
            cr = MongoCredential.createPlainCredential(userName, source,
                    password == null ? null : password.toCharArray());
            break;
        default:
            throw new IllegalArgumentException("invalid mechanism:" + mech + ", must be one of "
                    + "GSSAPI_MECHANISM, MONGODB_CR_MECHANISM, "
                    + "MONGODB_X5090_MECHANISM, or PLAIN_MECHANISM");
        }
    }
    return cr;
}

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

public static Map.Entry<String, List<String>> extractLinkURIs(final InputStream is) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode srcNode = (ObjectNode) mapper.readTree(is);
    IOUtils.closeQuietly(is);/*w w  w . j a  va2s . c o m*/

    final List<String> links = new ArrayList<String>();

    JsonNode uris = srcNode.get("value");
    if (uris == null) {
        final JsonNode url = srcNode.get("url");
        if (url != null) {
            links.add(url.textValue());
        }
    } else {
        final Iterator<JsonNode> iter = ((ArrayNode) uris).iterator();
        while (iter.hasNext()) {
            links.add(iter.next().get("url").textValue());
        }
    }

    final JsonNode next = srcNode.get(JSON_NEXTLINK_NAME);

    return new SimpleEntry<String, List<String>>(next == null ? null : next.asText(), links);
}

From source file:org.jetbrains.webdemo.examples.ExamplesLoader.java

private static ExamplesFolder loadFolder(String path, String url, List<ObjectNode> parentCommonFiles,
        ExamplesFolder parentFolder) {/*from   www. ja  va  2s  .  c  o  m*/
    File manifestFile = new File(path + File.separator + "manifest.json");
    try (BufferedInputStream reader = new BufferedInputStream(new FileInputStream(manifestFile))) {
        ObjectNode manifest = (ObjectNode) JsonUtils.getObjectMapper().readTree(reader);
        String name = new File(path).getName();
        List<ObjectNode> commonFiles = new ArrayList<>();
        commonFiles.addAll(parentCommonFiles);
        boolean taskFolder = manifest.has("taskFolder") ? manifest.get("taskFolder").asBoolean() : false;

        List<LevelInfo> levels = null;
        if (manifest.has("levels")) {
            levels = new ArrayList<>();
            for (JsonNode level : manifest.get("levels")) {
                levels.add(new LevelInfo(level.get("projectsNeeded").asInt(), level.get("color").asText()));
            }
        }

        if (parentFolder != null && parentFolder.isTaskFolder())
            taskFolder = true;
        ExamplesFolder folder = new ExamplesFolder(name, url, taskFolder, levels);
        if (manifest.has("files")) {
            for (JsonNode node : manifest.get("files")) {
                ObjectNode fileManifest = (ObjectNode) node;
                fileManifest.put("path", path + File.separator + fileManifest.get("filename").asText());
                commonFiles.add(fileManifest);
            }
        }

        if (manifest.has("folders")) {
            for (JsonNode node : manifest.get("folders")) {
                String folderName = node.textValue();
                folder.addChildFolder(loadFolder(path + File.separator + folderName, url + folderName + "/",
                        commonFiles, folder));
            }
        }

        if (manifest.has("examples")) {
            for (JsonNode node : manifest.get("examples")) {
                String projectName = node.textValue();
                String projectPath = path + File.separator + projectName;
                folder.addExample(loadExample(projectPath, url,
                        ApplicationSettings.LOAD_TEST_VERSION_OF_EXAMPLES, commonFiles));
            }
        }

        return folder;
    } catch (IOException e) {
        System.err.println("Can't load folder: " + e.toString());
        return null;
    }
}

From source file:com.github.fge.jsonpatch.diff.JsonDiff.java

private static void generateObjectDiffs(final DiffProcessor processor, final JsonPointer pointer,
        final ObjectNode source, final ObjectNode target) {
    final Set<String> firstFields = Sets.newTreeSet(Sets.newHashSet(source.fieldNames()));
    final Set<String> secondFields = Sets.newTreeSet(Sets.newHashSet(target.fieldNames()));

    for (final String field : Sets.difference(firstFields, secondFields))
        processor.valueRemoved(pointer.append(field), source.get(field));

    for (final String field : Sets.difference(secondFields, firstFields))
        processor.valueAdded(pointer.append(field), target.get(field));

    for (final String field : Sets.intersection(firstFields, secondFields))
        generateDiffs(processor, pointer.append(field), source.get(field), target.get(field));
}

From source file:com.palominolabs.crm.sf.rest.RestConnectionImpl.java

@Nonnull
private static RestSObject getSObject(JsonNode rawNode) throws IOException {
    ObjectNode jsonNode = asObjectNode(rawNode);

    ObjectNode attributes = getObjectNode(jsonNode, ATTRIBUTES_KEY);

    String type = getString(attributes, "type");
    JsonNode idNode = jsonNode.get(ID_KEY);
    RestSObjectImpl sObject;//from   w  ww.j a  v  a2  s  .  co m
    if (isNull(idNode)) {
        sObject = RestSObjectImpl.getNew(type);
    } else {
        if (!idNode.isTextual()) {
            throw new ResponseParseException("Id node <" + idNode + "> wasn't textual");
        }
        sObject = RestSObjectImpl.getNewWithId(type, new Id(idNode.textValue()));
    }

    jsonNode.remove(ID_KEY);
    jsonNode.remove(ATTRIBUTES_KEY);

    Iterator<String> fieldNames = jsonNode.fieldNames();
    while (fieldNames.hasNext()) {
        String fieldName = fieldNames.next();
        JsonNode fieldValueNode = jsonNode.get(fieldName);

        if (fieldValueNode.isNull()) {
            // null node is a value node so handle it first
            sObject.setField(fieldName, null);
            continue;
        } else if (fieldValueNode.isValueNode()) {
            sObject.setField(fieldName, fieldValueNode.asText());
            continue;
        } else if (fieldValueNode.isObject()) {
            // it could either be a subquery or a sub object at this point.
            if (fieldValueNode.path("attributes").isObject()) {
                sObject.setRelationshipSubObject(fieldName, getSObject(fieldValueNode));
            } else if (fieldValueNode.path("records").isArray()) {
                sObject.setRelationshipQueryResult(fieldName, getQueryResult(fieldValueNode));
            } else {
                throw new ResponseParseException("Could not understand field value node: " + fieldValueNode);
            }

            continue;
        }

        throw new ResponseParseException("Unknown node type <" + fieldValueNode + ">");
    }
    return sObject;
}

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

private static JsonNode convertToSimpleRecord(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)) {
                JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory);
                if (!value.isNull()) {
                    newNode.put(fieldName, value);
                }//from  w w w  . ja v  a2 s  .  c  o 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();
            JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory);
            if (value.isNull()) {
                newNode.put(entry.getKey(), value);
            }
        }
        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(),
                                convertToSimpleRecord(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 {
            return convertToSimpleRecord(type, json.get(type.getFullName()), factory);
        }
    } 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(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory));
        }
        return newNode;
    } else {
        return json;
    }
}

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

private static JsonNode convertToSimpleRecord(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)) {
                JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory);
                if (!value.isNull()) {
                    newNode.set(fieldName, value);
                }/*from w  ww .j a  v  a 2  s  .  c  o 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();
            JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory);
            if (value.isNull()) {
                newNode.set(entry.getKey(), value);
            }
        }
        return newNode;
    } else if (schema.getType() == Type.UNION) {
        Schema type = 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.set(entry.getKey(),
                                convertToSimpleRecord(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 {
            return convertToSimpleRecord(type, json.get(type.getFullName()), factory);
        }
    } 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(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory));
        }
        return newNode;
    } else {
        return json;
    }
}