Example usage for com.fasterxml.jackson.databind.node ArrayNode ArrayNode

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode ArrayNode

Introduction

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

Prototype

public ArrayNode(JsonNodeFactory paramJsonNodeFactory) 

Source Link

Usage

From source file:org.apache.olingo.fit.utils.Commons.java

public static InputStream getLinksAsJSON(final String entitySetName,
        final Map.Entry<String, Collection<String>> link) throws IOException {

    final ObjectNode links = new ObjectNode(JsonNodeFactory.instance);
    links.put(Constants.get(ConstantKey.JSON_ODATAMETADATA_NAME),
            Constants.get(ConstantKey.ODATA_METADATA_PREFIX) + entitySetName + "/$links/" + link.getKey());

    final ArrayNode uris = new ArrayNode(JsonNodeFactory.instance);

    for (String uri : link.getValue()) {
        final String absoluteURI;
        if (URI.create(uri).isAbsolute()) {
            absoluteURI = uri;/*from w w w  . jav a2 s.  com*/
        } else {
            absoluteURI = Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + uri;
        }
        uris.add(new ObjectNode(JsonNodeFactory.instance).put("url", absoluteURI));
    }

    if (uris.size() == 1) {
        links.setAll((ObjectNode) uris.get(0));
    } else {
        links.set("value", uris);
    }

    return IOUtils.toInputStream(links.toString(), Constants.ENCODING);
}

From source file:com.redhat.lightblue.crud.rdbms.RDBMSProcessor.java

private static void convertRecursion(RDBMSContext rdbmsContext, JsonDoc jd, String key, Object values,
        ArrayNode doc) {// w w  w  . j a  v  a  2s  .  c o m
    Collection c = values instanceof Collection ? ((Collection) values) : null;
    if (values == null || (c != null && c.isEmpty())) {
        if (doc == null) {
            jd.modify(new Path(key), NullNode.getInstance(), true);
        } else {
            doc.add(NullNode.getInstance());
        }
    } else if (c != null && c.size() > 1) {
        if (doc == null) {
            doc = new ArrayNode(rdbmsContext.getJsonNodeFactory());
        }
        for (Object value : c) {
            if (value instanceof Collection) {
                Collection cValue = (Collection) value;
                ArrayNode newDoc = new ArrayNode(rdbmsContext.getJsonNodeFactory());
                for (Object o : cValue) {
                    convertRecursion(rdbmsContext, jd, key, o, newDoc);
                }
                doc.add(newDoc);
            } else {
                doc.add(value.toString());
            }
        }
        jd.modify(new Path(key), doc, true);
    } else {
        if (doc == null) {
            Object o = c.iterator().next();
            jd.modify(new Path(key), new TextNode(o.toString()), true);
        } else {
            doc.add(new TextNode(values.toString()));
        }
    }
}

From source file:controllers.ConfigurationApplication.java

public static Result uploadPuppetFile() {
    String callBackUrl = GoogleComputeEngineAuthImpl.getCallBackURL(request());
    ObjectNode returnMessage = Json.newObject();
    try {/*w w  w  . ja  va 2s  .c  om*/
        String result = GoogleAuthenticationService.authenticate(callBackUrl, null);
        if (result != null) {
            return redirect(result);
        }

        ArrayNode fileList = new ArrayNode(JsonNodeFactory.instance);
        for (Http.MultipartFormData.FilePart filePart : request().body().asMultipartFormData().getFiles()) {
            ConfigurationService.uploadPuppetFile(PuppetConfiguration.PUPPET_FILE, filePart.getFilename(),
                    filePart.getFile());
            fileList.add(filePart.getFilename());
        }
        returnMessage.set("result", Json.newObject().textNode("ok"));
        returnMessage.set("files", fileList);
        return ok(returnMessage);
    } catch (GoogleComputeEngineException e) {
        returnMessage.set("result", Json.newObject().textNode("error"));
        returnMessage.set("message", Json.newObject().textNode(e.getMessage()));
        return ok(returnMessage);
    } catch (PuppetConfigurationException e) {
        returnMessage.set("result", Json.newObject().textNode("error"));
        returnMessage.set("message", Json.newObject().textNode(e.getMessage()));
        return ok(returnMessage);
    }
}

From source file:org.apache.james.mailbox.tika.TikaTextExtractorTest.java

@Test
public void asListOfStringShouldReturnAListWhenMultipleElements() {
    ArrayNode jsonArray = new ArrayNode(JsonNodeFactory.instance).add("first").add("second").add("third");

    ContentAndMetadataDeserializer deserializer = new TikaTextExtractor.ContentAndMetadataDeserializer();
    List<String> listOfString = deserializer.asListOfString(jsonArray);

    assertThat(listOfString).containsOnly("first", "second", "third");
}

From source file:nextflow.fs.dx.api.DxApi.java

/**
 * Creater the {@code JsonNode} input object required to delete one, or more, files
 *
 * <p>//from w  w  w  . j av a2s . co  m
 *     Example JSON object format:
 *     <pre>
 *     '{"objects":["file-B86Q5B80j58B67zVXG3Q01K8"]}'
 *     </pre>
 * </p>
 *
 *
 *
 * <p>
 *      http://wiki.dnanexus.com/API-Specification-v1.0.0/Folders-and-Deletion#API-method:-/class-xxxx/removeObjects
 * </p>
 *
 * @param fileIds an array of strings representing IDs of the objects to be removed from the data
 * @return @return The entity ID of the manipulated data container
 */
public String fileDelete(String contextId, String... fileIds) throws IOException {

    ArrayNode container = new ArrayNode(JsonNodeFactory.instance);
    for (String item : fileIds) {
        container.add(item);
    }

    JsonNode input = DxJson.getObjectBuilder().put("objects", container).build();

    JsonNode result = api(String.format("/%s/removeObjects", contextId), input).call();

    return result.get("id").textValue();
}

From source file:nextflow.fs.dx.api.DxApi.java

/**
 * https://wiki.dnanexus.com/API-Specification-v1.0.0/Tags#API-method%3A-%2Fclass-xxxx%2FaddTags
 *
 * @param contextId//from  w ww.jav a 2s  .c  o  m
 * @param fileId
 * @param tags
 * @return
 */
public String addFileTags(String contextId, String fileId, String[] tags) throws IOException {

    ArrayNode container = new ArrayNode(JsonNodeFactory.instance);
    for (String item : tags) {
        container.add(item);
    }

    JsonNode input = DxJson.getObjectBuilder().put("project", contextId).put("tags", container).build();

    JsonNode result = api(String.format("/%s/addTags", fileId), input).call();

    return result.get("id").textValue();
}

From source file:nextflow.fs.dx.api.DxApi.java

/**
 * https://wiki.dnanexus.com/API-Specification-v1.0.0/Types#API-method%3A-%2Fclass-xxxx%2FaddTypes
 *
 * @param contextId//from   w w  w. j  a  v  a  2 s . c om
 * @param fileId
 * @param types
 * @return
 */
public String addFileTypes(String contextId, String fileId, String[] types) throws IOException {

    ArrayNode container = new ArrayNode(JsonNodeFactory.instance);
    for (String item : types) {
        container.add(item);
    }

    JsonNode input = DxJson.getObjectBuilder().put("project", contextId).put("types", container).build();

    JsonNode result = api(String.format("/%s/addTypes", fileId), input).call();

    return result.get("id").textValue();
}

From source file:org.walkmod.conf.providers.YAMLConfigurationProvider.java

@Override
public void addPluginConfig(final PluginConfig pluginConfig, boolean recursive) throws TransformerException {

    File cfg = new File(fileName);

    ArrayNode pluginList = null;//from  w ww .j  a  v  a 2s .  co m
    JsonNode node = null;
    try {
        node = mapper.readTree(cfg);
    } catch (Exception e) {

    }
    if (node == null) {
        node = new ObjectNode(mapper.getNodeFactory());
    }
    if (recursive && node.has("modules")) {
        JsonNode aux = node.get("modules");
        if (aux.isArray()) {
            ArrayNode modules = (ArrayNode) aux;
            int max = modules.size();
            for (int i = 0; i < max; i++) {
                JsonNode module = modules.get(i);
                if (module.isTextual()) {
                    String moduleDir = module.asText();

                    try {
                        File auxFile = new File(fileName).getCanonicalFile().getParentFile();
                        YAMLConfigurationProvider child = new YAMLConfigurationProvider(
                                auxFile.getAbsolutePath() + File.separator + moduleDir + File.separator
                                        + "walkmod.yml");
                        child.createConfig();
                        child.addPluginConfig(pluginConfig, recursive);
                    } catch (IOException e) {
                        throw new TransformerException(e);
                    }

                }
            }
        }
    } else {
        if (!node.has("plugins")) {
            pluginList = new ArrayNode(mapper.getNodeFactory());
            if (node.isObject()) {
                ObjectNode aux = (ObjectNode) node;
                aux.set("plugins", pluginList);
            } else {
                throw new TransformerException("The root element is not a JSON node");
            }
        } else {
            JsonNode aux = node.get("plugins");
            if (aux.isArray()) {
                pluginList = (ArrayNode) node.get("plugins");
            } else {
                throw new TransformerException("The plugins element is not a valid array");
            }
        }
        pluginList.add(new TextNode(pluginConfig.getGroupId() + ":" + pluginConfig.getArtifactId() + ":"
                + pluginConfig.getVersion()));
        write(node);
    }
}

From source file:org.jetbrains.webdemo.sessions.MyHttpSession.java

private void sendExamplesList() {
    try {//from w ww .  j  a v  a 2s .  co  m
        ArrayNode responseBody = new ArrayNode(JsonNodeFactory.instance);
        Map<String, Boolean> taskStatuses = MySqlConnector.getInstance()
                .getUserTaskStatuses(sessionInfo.getUserInfo());
        for (ExamplesFolder folder : ExamplesFolder.ROOT_FOLDER.getChildFolders()) {
            addFolderContent(responseBody, folder, taskStatuses);
        }

        ObjectNode adventOfCodeContent = responseBody.addObject();
        adventOfCodeContent.put("name", "Advent of Code");
        adventOfCodeContent.put("id", "advent%20of%20code");
        adventOfCodeContent.putArray("childFolders");
        if (sessionInfo.getUserInfo().isLogin()) {
            adventOfCodeContent.put("projects", MySqlConnector.getInstance()
                    .getProjectHeaders(sessionInfo.getUserInfo(), "ADVENT_OF_CODE_PROJECT"));
        } else {
            adventOfCodeContent.putArray("projects");
        }

        ObjectNode myProgramsContent = responseBody.addObject();
        myProgramsContent.put("name", "My programs");
        myProgramsContent.put("id", "My%20programs");
        myProgramsContent.putArray("childFolders");
        if (sessionInfo.getUserInfo().isLogin()) {
            myProgramsContent.put("projects",
                    MySqlConnector.getInstance().getProjectHeaders(sessionInfo.getUserInfo(), "USER_PROJECT"));
        } else {
            myProgramsContent.putArray("projects");
        }

        writeResponse(responseBody.toString(), HttpServletResponse.SC_OK);
    } catch (DatabaseOperationException e) {
        writeResponse(e.getMessage(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:com.googlecode.jsonrpc4j.JsonRpcClient.java

/**
 * Writes a request./*from   w  w w  .  j a  v  a  2s.c o  m*/
 * @param methodName the method name
 * @param arguments the arguments
 * @param ops the stream
 * @param id the optional id
 * @throws IOException on error
 */
private void internalWriteRequest(String methodName, Object arguments, OutputStream ops, String id)
        throws IOException {

    // create the request
    ObjectNode request = mapper.createObjectNode();

    // add id
    if (id != null) {
        request.put("id", id);
    }

    // add protocol and method
    request.put("jsonrpc", JSON_RPC_VERSION);
    request.put("method", methodName);

    // object array args
    if (arguments != null && arguments.getClass().isArray()) {
        Object[] args = Object[].class.cast(arguments);
        if (args.length > 0) {
            // serialize every param for itself so jackson can determine
            // right serializer
            ArrayNode paramsNode = new ArrayNode(mapper.getNodeFactory());
            for (Object arg : args) {
                JsonNode argNode = mapper.valueToTree(arg);
                paramsNode.add(argNode);
            }
            request.put("params", paramsNode);
        }

        // collection args
    } else if (arguments != null && Collection.class.isInstance(arguments)) {
        Collection<?> args = Collection.class.cast(arguments);
        if (!args.isEmpty()) {
            // serialize every param for itself so jackson can determine
            // right serializer
            ArrayNode paramsNode = new ArrayNode(mapper.getNodeFactory());
            for (Object arg : args) {
                JsonNode argNode = mapper.valueToTree(arg);
                paramsNode.add(argNode);
            }
            request.put("params", paramsNode);
        }

        // map args
    } else if (arguments != null && Map.class.isInstance(arguments)) {
        if (!Map.class.cast(arguments).isEmpty()) {
            request.put("params", mapper.valueToTree(arguments));
        }

        // other args
    } else if (arguments != null) {
        request.put("params", mapper.valueToTree(arguments));
    }

    // show to listener
    if (this.requestListener != null) {
        this.requestListener.onBeforeRequestSent(this, request);
    }
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "JSON-PRC Request: " + request.toString());
    }

    // post the json data;
    writeAndFlushValue(ops, request);
}