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

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

Introduction

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

Prototype

@Override
public ObjectNode createObjectNode() 

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:eu.europa.ec.fisheries.uvms.reporting.rest.resources.ReportingResource.java

private ObjectNode mapToGeoJson(ExecutionResultDTO dto) throws IOException {

    ObjectNode rootNode;//from   w w w .ja  va  2  s.c  om

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    rootNode = mapper.createObjectNode();
    StringWriter stringWriter = new StringWriter();

    GeometryMapper.INSTANCE.featureCollectionToGeoJson(dto.getMovements(), stringWriter);

    rootNode.set("movements", mapper.readTree(stringWriter.toString()));

    stringWriter.getBuffer().setLength(0);
    GeometryMapper.INSTANCE.featureCollectionToGeoJson(dto.getSegments(), stringWriter);
    rootNode.set("segments", mapper.readTree(stringWriter.toString()));

    rootNode.putPOJO("tracks", dto.getTracks());
    rootNode.putPOJO("trips", dto.getTrips());

    ObjectNode activityNode = new FeatureToGeoJsonJacksonMapper().convert(dto.getActivities());
    rootNode.putPOJO("activities", activityNode);

    rootNode.putPOJO("criteria", dto.getFaCatchSummaryDTO());

    return rootNode;
}

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);//from   w  w w.  j  av a  2s  .  co 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:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

private boolean verify(Message message, MsgCheck check) {
    String sVal = "";

    if (check.getField().equals(MESSAGECONTENTFIELD)) {
        try {//from   ww  w  . j  ava 2s  .  com
            if (message instanceof TextMessage) {
                sVal = ((TextMessage) message).getText();
            } else if (message instanceof MapMessage) {
                MapMessage mm = (MapMessage) message;
                ObjectMapper mapper = new ObjectMapper();
                ObjectNode root = mapper.createObjectNode();

                @SuppressWarnings("unchecked")
                Enumeration<String> e = mm.getMapNames();
                while (e.hasMoreElements()) {
                    String field = e.nextElement();
                    root.set(field, mapper.convertValue(mm.getObject(field), JsonNode.class));
                }
                sVal = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
            } else if (message instanceof BytesMessage) {
                BytesMessage bm = (BytesMessage) message;
                bm.reset();
                byte[] bytes = new byte[(int) bm.getBodyLength()];
                if (bm.readBytes(bytes) == bm.getBodyLength()) {
                    sVal = new String(bytes);
                }
            }
        } catch (JMSException e) {
            return false;
        } catch (JsonProcessingException e) {
            return false;
        }
    } else {
        Enumeration<String> propNames = null;
        try {
            propNames = message.getPropertyNames();
            while (propNames.hasMoreElements()) {
                String propertyName = propNames.nextElement();
                if (propertyName.equals(check.getField())) {
                    if (message.getObjectProperty(propertyName) != null) {
                        sVal = message.getObjectProperty(propertyName).toString();
                        break;
                    }
                }
            }
        } catch (JMSException e) {
            return false;
        }
    }

    String eVal = "";
    if (check.getExpectedValue() != null) {
        eVal = check.getExpectedValue();
    }
    if (Pattern.compile(eVal).matcher(sVal).find()) {
        return true;
    }
    return false;
}

From source file:org.flowable.editor.dmn.converter.DmnJsonConverterUtil.java

public static JsonNode migrateModel(JsonNode decisionTableNode, ObjectMapper objectMapper) {

    // check if model is version 1
    if (decisionTableNode.get("modelVersion") == null || decisionTableNode.get("modelVersion").isNull()) {
        // split input rule nodes into operator and expression nodes
        ////from ww w .j a  v a2 s.c  o  m
        // determine input node ids
        JsonNode inputExpressionNodes = decisionTableNode.get("inputExpressions");
        Map<String, String> inputExpressionIds = new HashMap<>();

        if (inputExpressionNodes != null && !inputExpressionNodes.isNull()) {
            for (JsonNode inputExpressionNode : inputExpressionNodes) {
                if (inputExpressionNode.get("id") != null && !inputExpressionNode.get("id").isNull()) {
                    String inputId = inputExpressionNode.get("id").asText();

                    String inputType = null;
                    if (inputExpressionNode.get("type") != null && !inputExpressionNode.get("type").isNull()) {
                        inputType = inputExpressionNode.get("type").asText();
                    }

                    inputExpressionIds.put(inputId, inputType);
                }
            }
        }
        // split input rule nodes
        JsonNode ruleNodes = decisionTableNode.get("rules");
        ArrayNode newRuleNodes = objectMapper.createArrayNode();

        if (ruleNodes != null && !ruleNodes.isNull()) {
            for (JsonNode ruleNode : ruleNodes) {
                ObjectNode newRuleNode = objectMapper.createObjectNode();

                for (String inputExpressionId : inputExpressionIds.keySet()) {
                    if (ruleNode.has(inputExpressionId)) {
                        String operatorId = inputExpressionId + "_operator";
                        String expressionId = inputExpressionId + "_expression";
                        String operatorValue = null;
                        String expressionValue = null;

                        if (ruleNode.get(inputExpressionId) != null
                                && !ruleNode.get(inputExpressionId).isNull()) {
                            String oldExpression = ruleNode.get(inputExpressionId).asText();

                            if (StringUtils.isNotEmpty(oldExpression)) {
                                if (oldExpression.indexOf(' ') != -1) {
                                    operatorValue = oldExpression.substring(0, oldExpression.indexOf(' '));
                                    expressionValue = oldExpression.substring(oldExpression.indexOf(' ') + 1);
                                } else { // no prefixed operator
                                    expressionValue = oldExpression;
                                }

                                // remove outer escape quotes
                                if (expressionValue.startsWith("\"") && expressionValue.endsWith("\"")) {
                                    expressionValue = expressionValue.substring(1,
                                            expressionValue.length() - 1);
                                }

                                // if build in date function
                                if (expressionValue.startsWith("fn_date(")) {
                                    expressionValue = expressionValue.substring(9,
                                            expressionValue.lastIndexOf('\''));

                                } else if (expressionValue.startsWith("date:toDate(")) {
                                    expressionValue = expressionValue.substring(13,
                                            expressionValue.lastIndexOf('\''));
                                }

                                // determine type is null
                                if (StringUtils.isEmpty(inputExpressionIds.get(inputExpressionId))) {
                                    String expressionType = determineExpressionType(expressionValue);
                                    inputExpressionIds.put(inputExpressionId, expressionType);
                                }
                            }
                        }

                        // add new operator kv
                        if (StringUtils.isNotEmpty(operatorValue)) {
                            newRuleNode.put(operatorId, operatorValue);
                        } else { // default value
                            newRuleNode.put(operatorId, "==");
                        }

                        // add new expression kv
                        if (StringUtils.isNotEmpty(expressionValue)) {
                            newRuleNode.put(expressionId, expressionValue);
                        } else { // default value
                            newRuleNode.put(expressionId, "-");
                        }
                    }
                }

                Iterator<String> ruleProperty = ruleNode.fieldNames();
                while (ruleProperty.hasNext()) {
                    String outputExpressionId = ruleProperty.next();
                    if (!inputExpressionIds.containsKey(outputExpressionId)) { // is output expression
                        String outputExpressionValue = ruleNode.get(outputExpressionId).asText();

                        // remove outer escape quotes
                        if (StringUtils.isNotEmpty(outputExpressionValue)
                                && outputExpressionValue.startsWith("\"")
                                && outputExpressionValue.endsWith("\"")) {
                            outputExpressionValue = outputExpressionValue.substring(1,
                                    outputExpressionValue.length() - 1);
                        }

                        // if build in date function
                        if (outputExpressionValue.startsWith("fn_date(")) {
                            outputExpressionValue = outputExpressionValue.substring(9,
                                    outputExpressionValue.lastIndexOf('\''));

                        } else if (outputExpressionValue.startsWith("date:toDate(")) {
                            outputExpressionValue = outputExpressionValue.substring(13,
                                    outputExpressionValue.lastIndexOf('\''));
                        }

                        newRuleNode.put(outputExpressionId, outputExpressionValue);
                    }
                }

                newRuleNodes.add(newRuleNode);
            }

            // set input expression nodes types
            if (inputExpressionNodes != null && !inputExpressionNodes.isNull()) {
                for (JsonNode inputExpressionNode : inputExpressionNodes) {
                    if (inputExpressionNode.get("id") != null && !inputExpressionNode.get("id").isNull()) {
                        String inputId = inputExpressionNode.get("id").asText();
                        ((ObjectNode) inputExpressionNode).put("type", inputExpressionIds.get(inputId));
                    }
                }
            }

            // replace rules node
            ObjectNode decisionTableObjectNode = (ObjectNode) decisionTableNode;
            decisionTableObjectNode.replace("rules", newRuleNodes);
        }
    }

    return decisionTableNode;
}

From source file:org.wrml.runtime.format.text.html.WrmldocFormatter.java

protected ObjectNode buildApiNode(final ObjectMapper objectMapper, final Model model) {

    final ObjectNode apiNode = objectMapper.createObjectNode();
    if (!(model instanceof Document)) {
        return apiNode;
    }//from  ww w  . j av a  2  s .  c o m

    final Context context = getContext();

    final Document document = (Document) model;
    final URI uri = document.getUri();
    final ApiLoader apiLoader = context.getApiLoader();
    final ApiNavigator apiNavigator = apiLoader.getParentApiNavigator(uri);
    final Api api = apiNavigator.getApi();
    final Resource endpointResource = apiNavigator.getResource(uri);

    final URI apiUri = api.getUri();

    apiNode.put(PropertyName.uri.name(), apiUri.toString());
    apiNode.put(PropertyName.title.name(), api.getTitle());
    apiNode.put(PropertyName.description.name(), api.getDescription());
    apiNode.put(PropertyName.version.name(), api.getVersion());

    final Map<URI, ObjectNode> schemaNodes = new HashMap<>();
    final Map<URI, LinkRelation> linkRelationCache = new HashMap<>();

    if (document instanceof Api) {

        final Map<UUID, Resource> allResources = apiNavigator.getAllResources();
        final SortedMap<String, Resource> orderedResources = new TreeMap<>();

        for (final Resource resource : allResources.values()) {
            orderedResources.put(resource.getPathText(), resource);
        }

        final ArrayNode allResourcesNode = objectMapper.createArrayNode();
        for (final Resource resource : orderedResources.values()) {
            final ObjectNode resourceNode = buildResourceNode(objectMapper, schemaNodes, linkRelationCache,
                    apiNavigator, resource);
            allResourcesNode.add(resourceNode);
        }

        apiNode.put(PropertyName.allResources.name(), allResourcesNode);
    } else {
        final ObjectNode endpointResourceNode = buildResourceNode(objectMapper, schemaNodes, linkRelationCache,
                apiNavigator, endpointResource);
        apiNode.put(PropertyName.resource.name(), endpointResourceNode);
    }

    return apiNode;
}

From source file:com.marklogic.jena.functionaltests.ConnectedRESTQA.java

public static ObjectNode getCollectionNode(String... collections) {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode mNode = mapper.createObjectNode();
    ArrayNode aNode = mapper.createArrayNode();

    for (String c : collections) {
        aNode.add(c);/*from   ww w  . j a v  a2  s .  c  o m*/
    }
    mNode.withArray("collection").addAll(aNode);
    return mNode;
}

From source file:com.marklogic.jena.functionaltests.ConnectedRESTQA.java

public static void enableWordLexicon(String dbName) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode childNode = mapper.createObjectNode();
    ArrayNode childArray = mapper.createArrayNode();
    childArray.add("http://marklogic.com/collation/");
    childNode.putArray("word-lexicon").addAll(childArray);
    setDatabaseProperties(dbName, "word-lexicons", childNode);

}

From source file:com.marklogic.jena.functionaltests.ConnectedRESTQA.java

public static ObjectNode getPermissionNode(String roleName, DocumentMetadataHandle.Capability... cap) {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode mNode = mapper.createObjectNode();
    ArrayNode aNode = mapper.createArrayNode();

    for (DocumentMetadataHandle.Capability c : cap) {
        ObjectNode roleNode = mapper.createObjectNode();
        roleNode.put("role-name", roleName);
        roleNode.put("capability", c.toString().toLowerCase());
        aNode.add(roleNode);/*  www.java 2s  . com*/
    }
    mNode.withArray("permission").addAll(aNode);
    return mNode;
}