Example usage for com.fasterxml.jackson.databind ObjectMapper createArrayNode

List of usage examples for com.fasterxml.jackson.databind ObjectMapper createArrayNode

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper createArrayNode.

Prototype

@Override
public ArrayNode createArrayNode() 

Source Link

Document

Note: return type is co-variant, as basic ObjectCodec abstraction can not refer to concrete node types (as it's part of core package, whereas impls are part of mapper package)

Usage

From source file:org.onosproject.EvacuateCommand.java

/**
 * Produces a JSON array of flows grouped by the each device.
 *
 * @param devices     collection of devices to group flow by
 * @param flows       collection of flows per each device
 * @return JSON array/*from w  w  w. j a  v a2s .  co m*/
 */
private JsonNode json(Iterable<Device> devices, Map<Device, List<FlowEntry>> flows) {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode result = mapper.createArrayNode();
    for (Device device : devices) {
        result.add(json(mapper, device, flows.get(device)));
    }
    return result;
}

From source file:org.openlmis.fulfillment.util.CustomSortDeserializerTest.java

@Test
public void shouldDeserializeArraySort() throws IOException {
    ObjectMapper mapper = new ObjectMapper();

    ObjectNode order = mapper.createObjectNode();
    order.put("direction", "DESC");
    order.put("property", "startDate");
    order.put("ignoreCase", false);
    order.put("nullHandling", "NATIVE");
    order.put("ascending", false);
    order.put("descending", true);

    ArrayNode arrayNode = mapper.createArrayNode();
    arrayNode.add(order);//from w w  w .j  a  v  a  2s  .c  o m

    ObjectNode testObject = mapper.createObjectNode();
    testObject.set("sort", arrayNode);

    Sort sort = deserialize(testObject.toString());

    assertEquals(Sort.Direction.DESC, sort.getOrderFor("startDate").getDirection());
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void extractUrl_withMissingUrl_shouldReturnNull() {
    // GIVEN/*from w w w. j  ava2  s.  c  o m*/
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = mapper.createObjectNode();
    ArrayNode links = mapper.createArrayNode();
    root.set("links", links);

    // WHEN
    String url = JsonUtils.extractUrl(root, "MISSING");

    // THEN
    assertNull(url);
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void extractUrl_withNoRelField_shouldReturnUrl() {
    // GIVEN/*from   ww  w.  j  a v  a 2 s  .co m*/
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = mapper.createObjectNode();
    ArrayNode links = mapper.createArrayNode();
    root.set("links", links);

    ObjectNode link = mapper.createObjectNode();
    link.put("href", "href");

    links.add(link);

    // WHEN
    String url = JsonUtils.extractUrl(root, "relToFind");

    // THEN
    assertNull(url);
}

From source file:com.gsma.mobileconnect.utils.JsonUtilsTest.java

@Test
public void extractUrl_withUrl_shouldReturnUrl() {
    // GIVEN/*from w  w w.  jav  a2  s  .c  o  m*/
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = mapper.createObjectNode();
    ArrayNode links = mapper.createArrayNode();
    root.set("links", links);

    ObjectNode link = mapper.createObjectNode();
    String relToFind = "relToFind";
    link.put("rel", relToFind);
    String expectedHref = "hrefToFind";
    link.put("href", expectedHref);

    links.add(link);

    // WHEN
    String url = JsonUtils.extractUrl(root, relToFind);

    // THEN
    assertEquals(expectedHref, url);
}

From source file:org.onosproject.EvacuateCommand.java

private ObjectNode json(ObjectMapper mapper, Device device, List<FlowEntry> flows) {
    ObjectNode result = mapper.createObjectNode();
    ArrayNode array = mapper.createArrayNode();

    flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));

    result.put("device", device.id().toString()).put("flowCount", flows.size()).set("flows", array);
    return result;
}

From source file:scott.barleyrs.rest.AdminService.java

private JsonNode toJsonSchema(ObjectMapper mapper, String namespace, EntityType entityType) {
    ObjectNode schemaRoot = mapper.createObjectNode();
    schemaRoot.put("title",
            "JSON Schema for namepsace " + namespace + " and entity " + entityType.getInterfaceName());
    schemaRoot.put("type", "object");
    /*/*  www  .  ja  v a2  s .c  om*/
     * required section.
     */
    ArrayNode required = mapper.createArrayNode();
    for (NodeType nodeType : entityType.getNodeTypes()) {
        if (nodeType.isMandatory()) {
            required.add(nodeType.getName());
        }
    }
    schemaRoot.set("required", required);

    /*
     * properties section
     */
    ObjectNode properties = mapper.createObjectNode();
    for (NodeType nodeType : entityType.getNodeTypes()) {
        if (nodeType.getEnumSpec() != null) {
            ObjectNode prop = mapper.createObjectNode();
            prop.put("type", toJSONSchemaType(nodeType.getJavaType()));
            prop.set("enum", enumValuesAsJsonArray(mapper, nodeType.getEnumSpec()));
            properties.set(nodeType.getName(), prop);
        } else if (nodeType.getJavaType() != null) {
            ObjectNode prop = mapper.createObjectNode();
            prop.put("type", toJSONSchemaType(nodeType.getJavaType()));
            properties.set(nodeType.getName(), prop);
        } else if (nodeType.getColumnName() != null && nodeType.getRelationInterfaceName() != null) {
            ObjectNode prop = mapper.createObjectNode();
            prop.put("type", "string");
            properties.set(nodeType.getName(), prop);
        }
    }
    schemaRoot.set("properties", properties);
    return schemaRoot;
}

From source file:net.sf.jasperreports.engine.export.JsonExporter.java

protected void exportWebFonts() throws IOException {
    HtmlResourceHandler fontHandler = getExporterOutput().getFontHandler();
    ReportContext reportContext = getReportContext();

    if (fontHandler != null && reportContext != null
            && reportContext.containsParameter(REPORT_CONTEXT_PARAMETER_WEB_FONTS)) {
        Map<String, HtmlFontFamily> fontsToProcess = (Map<String, HtmlFontFamily>) reportContext
                .getParameterValue(REPORT_CONTEXT_PARAMETER_WEB_FONTS);

        ObjectMapper mapper = new ObjectMapper();
        ArrayNode webFonts = mapper.createArrayNode();

        for (HtmlFontFamily htmlFontFamily : fontsToProcess.values()) {
            ObjectNode objNode = mapper.createObjectNode();
            objNode.put("id", htmlFontFamily.getId());
            objNode.put("path", fontHandler.getResourcePath(htmlFontFamily.getId()));
            webFonts.add(objNode);/* w  ww .  j a va2 s  . c o m*/
        }

        if (gotFirstJsonFragment) {
            writer.write(",\n");
        } else {
            gotFirstJsonFragment = true;
        }
        writer.write("\"webfonts_" + (webFonts.hashCode() & 0x7FFFFFFF) + "\": {");

        writer.write("\"id\": \"webfonts_" + (webFonts.hashCode() & 0x7FFFFFFF) + "\",");
        writer.write("\"type\": \"webfonts\",");
        writer.write("\"webfonts\": " + jacksonUtil.getJsonString(webFonts));

        writer.write("}");
    }
}

From source file:net.sf.jasperreports.engine.export.JsonExporter.java

protected void exportHyperlinks() throws IOException {
    ReportContext reportContext = getReportContext();
    String hyperlinksParameter = "net.sf.jasperreports.html.hyperlinks";
    if (reportContext != null && reportContext.containsParameter(hyperlinksParameter)) {
        List<HyperlinkData> contextHyperlinksData = (List<HyperlinkData>) reportContext
                .getParameterValue(hyperlinksParameter);
        hyperlinksData.addAll(contextHyperlinksData);
    }/*w ww.  j  av  a  2s. c  om*/
    if (hyperlinksData.size() > 0) {
        String id = "hyperlinks_" + (hyperlinksData.hashCode() & 0x7FFFFFFF);
        if (gotFirstJsonFragment) {
            writer.write(",\n");
        } else {
            gotFirstJsonFragment = true;
        }
        writer.write("\"" + id + "\": {");

        writer.write("\"id\": \"" + id + "\",");
        writer.write("\"type\": \"hyperlinks\",");
        writer.write("\"hyperlinks\": ");

        ObjectMapper mapper = new ObjectMapper();
        ArrayNode hyperlinkArray = mapper.createArrayNode();

        for (HyperlinkData hd : hyperlinksData) {
            JRPrintHyperlink hyperlink = hd.getHyperlink();
            ObjectNode hyperlinkNode = jacksonUtil.hyperlinkToJsonObject(hyperlink);

            jacksonUtil.addProperty(hyperlinkNode, "id", hd.getId());
            jacksonUtil.addProperty(hyperlinkNode, "href", hd.getHref());
            jacksonUtil.addProperty(hyperlinkNode, "selector", hd.getSelector());

            hyperlinkArray.add(hyperlinkNode);
        }

        writer.write(jacksonUtil.getJsonString(hyperlinkArray));
        writer.write("}");
    }
}

From source file:org.wrml.runtime.format.application.vnd.wrml.design.schema.SchemaDesignFormatter.java

private static ObjectNode buildChoicesNode(final ObjectMapper objectMapper, final Type heapValueType,
        final SchemaLoader schemaLoader) {

    if (heapValueType == null || !(heapValueType instanceof Class<?>)) {
        return null;
    }/*from  w w w.  j a va  2s.  com*/

    final Class<?> choicesEnumClass = (Class<?>) heapValueType;

    if (!choicesEnumClass.isEnum()) {
        return null;
    }

    final URI choicesUri = schemaLoader.getTypeUri(choicesEnumClass);
    final String choicesName = choicesEnumClass.getSimpleName();
    final ObjectNode choicesNode = objectMapper.createObjectNode();

    choicesNode.put(PropertyName.title.name(), choicesName);
    choicesNode.put(PropertyName.uri.name(), choicesUri.toString());

    // TODO: Only embed the choices once per schema to lighten the download?
    final Object[] enumConstants = choicesEnumClass.getEnumConstants();
    if (enumConstants != null && enumConstants.length > 0) {
        final ArrayNode valuesNode = objectMapper.createArrayNode();

        choicesNode.put(PropertyName.values.name(), valuesNode);

        for (final Object enumConstant : enumConstants) {
            final String choice = String.valueOf(enumConstant);
            valuesNode.add(choice);
        }
    }

    return choicesNode;
}