Java Code Examples for com.fasterxml.jackson.databind.JsonNode#traverse()

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#traverse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogicalFilter4.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Override
public LogicalFilter4 deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException {

    ObjectCodec objectCodec = jp.getCodec();
    ObjectNode root = jp.readValueAsTree();

    // We assume it is a LogicalFilter
    Iterator<String> iter = root.fieldNames();
    String key = iter.next();

    JsonNode arrayNode = root.iterator().next();
    if ( arrayNode == null || arrayNode.isMissingNode() || ! arrayNode.isArray() ) {
        throw new RuntimeException( "Invalid format of LogicalFilter encountered." );
    }

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = arrayNode.traverse( objectCodec );
    List<QueryFilter4> childrenQueryFilters = subJsonParser.readValueAs( new TypeReference<List<QueryFilter4>>() {} );

    return new LogicalFilter4( QueryParam.valueOf( key ), childrenQueryFilters );
}
 
Example 2
Source File: LogicalFilter3.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Override
public LogicalFilter3 deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException {

    ObjectCodec objectCodec = jp.getCodec();
    ObjectNode root = jp.readValueAsTree();

    // We assume it is a LogicalFilter
    Iterator<String> iter = root.fieldNames();
    String key = iter.next();

    JsonNode arrayNode = root.iterator().next();
    if ( arrayNode == null || arrayNode.isMissingNode() || ! arrayNode.isArray() ) {
        throw new RuntimeException( "Invalid format of LogicalFilter encountered." );
    }

    // pass in our objectCodec so that the subJsonParser knows about our configured Modules and Annotations
    JsonParser subJsonParser = arrayNode.traverse( objectCodec );
    List<QueryFilter> childrenQueryFilters = subJsonParser.readValueAs( new TypeReference<List<QueryFilter>>() {} );

    return new LogicalFilter3( QueryParam.valueOf( key ), childrenQueryFilters );
}
 
Example 3
Source File: YamlConfiguration.java    From styx with Apache License 2.0 5 votes vote down vote up
private static <T> T parseNodeToClass(JsonNode node, Class<T> tClass) {
    JsonParser parser = node.traverse();

    try {
        return YAML_MAPPER.readValue(parser, tClass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: JsonNodeConfig.java    From styx with Apache License 2.0 5 votes vote down vote up
private <T> T parseNodeToClass(JsonNode node, Class<T> tClass) {
    JsonParser parser = node.traverse();

    try {
        return mapper.readValue(parser, tClass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: FilterDescriptor.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
private <T> T readValueAs(Class<T> valueType, JsonNode jsonNode, JsonParser p) throws IOException {
    JsonParser traverse = jsonNode.traverse(p.getCodec());
    return traverse.readValueAs(valueType);
}
 
Example 6
Source File: Directory.java    From pegasus with Apache License 2.0 4 votes vote down vote up
/**
 * Deserializes a Directory YAML description of the type
 *
 * <pre>
 * yamlType: sharedScratch
 * path: /tmp/workflows/scratch
 * fileServers:
 * - operation: all
 * url: file:///tmp/workflows/scratch
 * </pre>
 *
 * @param parser
 * @param dc
 * @return
 * @throws IOException
 * @throws JsonProcessingException
 */
@Override
public Directory deserialize(JsonParser parser, DeserializationContext dc)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = parser.getCodec();
    JsonNode node = oc.readTree(parser);
    Directory directory = new Directory();

    for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
        Map.Entry<String, JsonNode> e = it.next();
        String key = e.getKey();
        SiteCatalogKeywords reservedKey = SiteCatalogKeywords.getReservedKey(key);
        if (reservedKey == null) {
            this.complainForIllegalKey(
                    SiteCatalogKeywords.DIRECTORIES.getReservedName(), key, node);
        }

        switch (reservedKey) {
            case TYPE:
                directory.setType(Directory.yamlTypeToType(node.get(key).asText()));
                break;

            case PATH:
                directory.getInternalMountPoint().setMountPoint(node.get(key).asText());
                break;

            case FILESERVERS:
                JsonNode fileServersNodes = node.get(key);
                if (fileServersNodes != null) {
                    if (fileServersNodes.isArray()) {
                        for (JsonNode fileServerNode : fileServersNodes) {
                            parser = fileServerNode.traverse(oc);
                            FileServer fs = parser.readValueAs(FileServer.class);
                            directory.addFileServer(fs);
                        }
                    }
                }
                break;

                // defined in schema but we don't do anything about it
            case FREE_SIZE:
                directory.getInternalMountPoint().setFreeSize(node.get(key).asText());
                break;

            case TOTAL_SIZE:
                directory.getInternalMountPoint().setTotalSize(node.get(key).asText());
                break;

            default:
                this.complainForUnsupportedKey(
                        SiteCatalogKeywords.DIRECTORIES.getReservedName(), key, node);
        }
    }

    return directory;
}
 
Example 7
Source File: ReplicaStore.java    From pegasus with Apache License 2.0 4 votes vote down vote up
/**
 * Deserializes a Transformation YAML description of the type
 *
 * <pre>
 *  pegasus: 5.0
 *  replicas:
 *    # matches "f.a"
 *    - lfn: "f.a"
 *      pfn: "file:///Volumes/data/input/f.a"
 *      site: "local"
 *
 *    # matches faa, f.a, f0a, etc.
 *    - lfn: "f.a"
 *    pfn: "file:///Volumes/data/input/f.a"
 *    site: "local"
 *    regex: true
 * </pre>
 *
 * @param parser
 * @param dc
 * @return
 * @throws IOException
 * @throws JsonProcessingException
 */
@Override
public ReplicaStore deserialize(JsonParser parser, DeserializationContext dc)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = parser.getCodec();
    JsonNode node = oc.readTree(parser);
    ReplicaStore store = new ReplicaStore();
    for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
        Map.Entry<String, JsonNode> e = it.next();
        String key = e.getKey();
        ReplicaCatalogKeywords reservedKey = ReplicaCatalogKeywords.getReservedKey(key);
        if (reservedKey == null) {
            this.complainForIllegalKey(
                    ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
        }

        String keyValue = node.get(key).asText();
        switch (reservedKey) {
            case PEGASUS:
                store.setVersion(keyValue);
                break;

            case REPLICAS:
                JsonNode replicaNodes = node.get(key);
                if (replicaNodes != null) {
                    if (replicaNodes.isArray()) {
                        for (JsonNode replicaNode : replicaNodes) {
                            parser = replicaNode.traverse(oc);
                            ReplicaLocation rl = parser.readValueAs(ReplicaLocation.class);
                            int count = rl.getPFNCount();
                            if (count == 0) {
                                throw new ReplicaCatalogException(
                                        "ReplicaLocation "
                                                + rl
                                                + " can  have one pfn or more pfns. Found "
                                                + count);
                            }
                            if (rl.isRegex()) {
                                StringBuffer error = new StringBuffer();
                                error.append(
                                                "Unable to deserialize into Replica Store an entry")
                                        .append(" ")
                                        .append("for lfn")
                                        .append(" ")
                                        .append(rl)
                                        .append(" ")
                                        .append(
                                                "as it has regex attribute set to true. Please specify such entries in a replica catalog file.");
                                throw new ReplicaCatalogException(error.toString());
                            }
                            store.add(rl);
                        }
                    }
                }
                break;

            default:
                this.complainForUnsupportedKey(
                        ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
        }
    }

    return store;
}
 
Example 8
Source File: YAML.java    From pegasus with Apache License 2.0 4 votes vote down vote up
/**
 * Deserializes a Replica Catalog representation YAML description of the type
 *
 * <pre>
 *  pegasus: "5.0"
 *  replicas:
 *    - lfn: f1
 *      pfns:
 *        - site: local
 *          pfn: /path/to/file
 *        - site: condorpool
 *          pfn: /path/to/file
 *      checksum:
 *        sha256: abc123
 *      metadata:
 *        owner: vahi
 *        size: 1024
 *    - lfn: f2
 *      pfns:
 *        - site: local
 *          pfn: /path/to/file
 *        - site: condorpool
 *          pfn: /path/to/file
 *      checksum:
 *        sha256: 991232132abc
 *      metadata:
 *        owner: pegasus
 *        size: 1024
 *    - lfn: .*\.gz
 *      pfns:
 *        - site: local
 *          pfn: input/mono/[0]
 *          # cant have checksum
 *      metadata:
 *        owner: pegasus
 *        regex: true
 * </pre>
 *
 * @param parser
 * @param dc
 * @return
 * @throws IOException
 * @throws JsonProcessingException
 */
@Override
public ReplicaCatalog deserialize(JsonParser parser, DeserializationContext dc)
        throws IOException, JsonProcessingException {
    ObjectCodec oc = parser.getCodec();
    JsonNode node = oc.readTree(parser);
    YAML yamlRC = (YAML) dc.findInjectableValue("callback", null, null);
    if (yamlRC == null) {
        throw new RuntimeException("Callback not initialized when parsing inititated");
    }
    for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
        Map.Entry<String, JsonNode> e = it.next();
        String key = e.getKey();
        ReplicaCatalogKeywords reservedKey = ReplicaCatalogKeywords.getReservedKey(key);
        if (reservedKey == null) {
            this.complainForIllegalKey(
                    ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
        }

        String keyValue = node.get(key).asText();
        switch (reservedKey) {
            case PEGASUS:
                yamlRC.setVersion(keyValue);
                break;

            case REPLICAS:
                JsonNode replicaNodes = node.get(key);
                if (replicaNodes != null) {
                    if (replicaNodes.isArray()) {
                        for (JsonNode replicaNode : replicaNodes) {
                            parser = replicaNode.traverse(oc);
                            ReplicaLocation rl = parser.readValueAs(ReplicaLocation.class);
                            yamlRC.insert(rl);
                        }
                    }
                }
                break;

            default:
                this.complainForUnsupportedKey(
                        ReplicaCatalogKeywords.REPLICAS.getReservedName(), key, node);
        }
    }

    return yamlRC;
}