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:org.wrml.runtime.format.application.vnd.wrml.design.schema.SchemaDesignFormatter.java

private static ObjectNode createSlot(final ObjectMapper objectMapper, final Prototype prototype,
        final String slotName) {

    final ProtoSlot protoSlot = prototype.getProtoSlot(slotName);
    final SchemaLoader schemaLoader = prototype.getSchemaLoader();
    final Context context = schemaLoader.getContext();
    final SyntaxLoader syntaxLoader = context.getSyntaxLoader();

    final ValueType valueType = protoSlot.getValueType();
    if (valueType == ValueType.Link) {
        return null;
    }//from ww w . j  ava  2  s  . c  o  m

    final Type heapValueType = protoSlot.getHeapValueType();

    final URI declaringSchemaUri = protoSlot.getDeclaringSchemaUri();

    final ObjectNode slotNode = objectMapper.createObjectNode();
    slotNode.put(PropertyName.name.name(), slotName);
    slotNode.put(PropertyName.title.name(), protoSlot.getTitle());
    slotNode.put(PropertyName.type.name(), valueType.name());
    slotNode.put(PropertyName.description.name(), protoSlot.getDescription());
    slotNode.put(PropertyName.declaringSchemaUri.name(), syntaxLoader.formatSyntaxValue(declaringSchemaUri));

    if (protoSlot instanceof PropertyProtoSlot) {
        final PropertyProtoSlot propertyProtoSlot = (PropertyProtoSlot) protoSlot;
        final Object defaultValue = propertyProtoSlot.getDefaultValue();
        if (defaultValue != null) {
            slotNode.put(PropertyName.defaultValue.name(), syntaxLoader.formatSyntaxValue(defaultValue));
        }
    }

    switch (valueType) {
    case Text: {

        final ObjectNode syntaxNode = buildSyntaxNode(objectMapper, heapValueType, syntaxLoader);
        slotNode.put(PropertyName.syntax.name(), syntaxNode);

        final PropertyProtoSlot textPropertyProtoSlot = (PropertyProtoSlot) protoSlot;
        final boolean isMultiline = textPropertyProtoSlot.isMultiline();
        if (isMultiline) {
            slotNode.put(PropertyName.multiline.name(), isMultiline);
        }

        break;
    }
    case List: {
        final PropertyProtoSlot listPropertyProtoSlot = (PropertyProtoSlot) protoSlot;

        final ObjectNode elementNode = objectMapper.createObjectNode();
        slotNode.put(PropertyName.element.name(), elementNode);

        final Type elementType = listPropertyProtoSlot.getListElementType();
        final ValueType elementValueType = schemaLoader.getValueType(elementType);
        elementNode.put(PropertyName.type.name(), elementValueType.name());

        if (elementValueType == ValueType.Model) {
            final URI elementSchemaUri = listPropertyProtoSlot.getListElementSchemaUri();
            if (elementSchemaUri != null) {
                final ObjectNode schemaNode = buildSchemaNode(objectMapper, elementSchemaUri, schemaLoader,
                        null);
                elementNode.put(PropertyName.schema.name(), schemaNode);
            }
        } else if (elementValueType == ValueType.Text) {
            final ObjectNode syntaxNode = buildSyntaxNode(objectMapper, elementType, syntaxLoader);
            elementNode.put(PropertyName.syntax.name(), syntaxNode);

        }

        break;
    }
    case Model: {
        final ObjectNode schemaNode = buildSchemaNode(objectMapper,
                ((PropertyProtoSlot) protoSlot).getModelSchemaUri(), schemaLoader, null);
        slotNode.put(PropertyName.schema.name(), schemaNode);
        break;
    }
    case SingleSelect: {
        final ObjectNode choicesNode = buildChoicesNode(objectMapper, heapValueType, schemaLoader);
        slotNode.put(PropertyName.choices.name(), choicesNode);
        break;
    }
    }

    return slotNode;
}

From source file:org.bimserver.servlets.ServiceRunnerServlet.java

private void processServiceList(HttpServletRequest request, HttpServletResponse response) {
    BimDatabase database = getBimServer().getDatabase();
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode result = mapper.createObjectNode();
    ArrayNode array = mapper.createArrayNode();
    ArrayNode capabilities = mapper.createArrayNode();
    capabilities.add("WEBSOCKET");
    result.set("capabilities", capabilities);
    result.set("services", array);
    try (DatabaseSession session = database.createSession()) {
        for (PluginDescriptor pluginDescriptor : session.getAllOfType(
                StorePackage.eINSTANCE.getPluginDescriptor(), PluginDescriptor.class, OldQuery.getDefault())) {
            if (pluginDescriptor.getPluginInterfaceClassName().equals(ServicePlugin.class.getName())) {
                ServicePlugin servicePlugin = getBimServer().getPluginManager()
                        .getServicePlugin(pluginDescriptor.getPluginClassName(), true);
                if (servicePlugin instanceof BimBotsServiceInterface) {
                    try {
                        BimBotsServiceInterface bimBotsServiceInterface = (BimBotsServiceInterface) servicePlugin;

                        ObjectNode descriptorJson = mapper.createObjectNode();
                        descriptorJson.put("id", pluginDescriptor.getOid());
                        descriptorJson.put("name", pluginDescriptor.getName());
                        descriptorJson.put("description", pluginDescriptor.getDescription());
                        descriptorJson.put("provider",
                                getBimServer().getServerSettingsCache().getServerSettings().getName());
                        descriptorJson.put("providerIcon",
                                getBimServer().getServerSettingsCache().getServerSettings().getIcon());

                        ArrayNode inputs = mapper.createArrayNode();
                        ArrayNode outputs = mapper.createArrayNode();

                        for (String schemaName : bimBotsServiceInterface.getAvailableInputs()) {
                            inputs.add(schemaName);
                        }//from  w w w.  ja  va 2s  . c  o  m
                        for (String schemaName : bimBotsServiceInterface.getAvailableOutputs()) {
                            outputs.add(schemaName);
                        }

                        descriptorJson.set("inputs", inputs);
                        descriptorJson.set("outputs", outputs);

                        ObjectNode oauth = mapper.createObjectNode();
                        String siteAddress = getBimServer().getServerSettingsCache().getServerSettings()
                                .getSiteAddress();
                        oauth.put("authorizationUrl", siteAddress + "/oauth/authorize");
                        oauth.put("registerUrl", siteAddress + "/oauth/register");
                        oauth.put("tokenUrl", siteAddress + "/oauth/access");

                        descriptorJson.set("oauth", oauth);
                        descriptorJson.put("resourceUrl", siteAddress + "/services");
                        array.add(descriptorJson);
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }
            }
        }
        response.setContentType("application/json");
        response.getOutputStream().write(mapper.writeValueAsBytes(result));
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
    } catch (JsonProcessingException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
}

From source file:com.gsma.mobileconnect.impl.CompleteSelectedOperatorDiscoveryTest.java

private JsonNode buildJsonObject() {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = mapper.createObjectNode();
    root.put("field", "field-value");
    return root;//from w ww  .  j ava 2  s . co m
}

From source file:com.gsma.mobileconnect.cache.DiscoveryCacheHashMapImplTest.java

@Test
public void add_withNullKey_shouldThrowException() {
    // GIVEN/*  w  w w  .j a v  a 2  s  .  c o m*/
    ObjectMapper objectMapper = new ObjectMapper();
    DiscoveryCacheValue value = new DiscoveryCacheValue(new Date(Long.MAX_VALUE),
            objectMapper.createObjectNode());

    IDiscoveryCache cache = Factory.getDefaultDiscoveryCache();

    // THEN
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(containsString("Key"));

    // WHEN
    cache.add(null, value);
}

From source file:com.rusticisoftware.tincan.ActivityDefinition.java

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
    ObjectMapper mapper = Mapper.getInstance();
    ObjectNode node = mapper.createObjectNode();
    if (this.name != null) {
        node.put("name", this.getName().toJSONNode(version));
    }//from   w  w w. ja  va2 s .c o  m
    if (this.description != null) {
        node.put("description", this.getDescription().toJSONNode(version));
    }
    if (this.type != null) {
        node.put("type", this.getType().toString());
    }
    if (this.moreInfo != null) {
        node.put("moreInfo", this.getMoreInfo().toString());
    }
    if (this.extensions != null) {
        node.put("extensions", this.getExtensions().toJSONNode(version));
    }
    if (this.interactionType != null) {
        node.put("interactionType", this.getInteractionType().toString());

        switch (this.interactionType) {
        case CHOICE:
        case SEQUENCING:
            if (this.choices != null && this.choices.size() > 0) {
                ArrayNode choices = mapper.createArrayNode();
                node.put("choices", choices);

                for (InteractionComponent ic : this.getChoices()) {
                    choices.add(ic.toJSONNode(version));
                }
            }
            break;

        case LIKERT:
            if (this.scale != null && this.scale.size() > 0) {
                ArrayNode scale = mapper.createArrayNode();
                node.put("scale", scale);

                for (InteractionComponent ic : this.getScale()) {
                    scale.add(ic.toJSONNode(version));
                }
            }
            break;

        case MATCHING:
            if (this.source != null && this.source.size() > 0) {
                ArrayNode source = mapper.createArrayNode();
                node.put("source", source);

                for (InteractionComponent ic : this.getSource()) {
                    source.add(ic.toJSONNode(version));
                }
            }
            if (this.target != null && this.target.size() > 0) {
                ArrayNode target = mapper.createArrayNode();
                node.put("target", target);

                for (InteractionComponent ic : this.getTarget()) {
                    target.add(ic.toJSONNode(version));
                }
            }
            break;

        case PERFORMANCE:
            if (this.steps != null && this.steps.size() > 0) {
                ArrayNode steps = mapper.createArrayNode();
                node.put("steps", steps);

                for (InteractionComponent ic : this.getSteps()) {
                    steps.add(ic.toJSONNode(version));
                }
            }
            break;

        case TRUE_FALSE:
        case FILL_IN:
        case NUMERIC:
        case OTHER:
            break;
        }
    }
    if (this.correctResponsesPattern != null && this.correctResponsesPattern.size() > 0) {
        ArrayNode responses = mapper.createArrayNode();
        node.put("correctResponsesPattern", responses);

        for (String resp : this.getCorrectResponsesPattern()) {
            responses.add(resp);
        }
    }
    return node;
}

From source file:la.alsocan.jsonshapeshifter.Transformation.java

/**
 * Transforms the given Json instance of the source schema to a Json instance of the 
 * target schema. The transformation applies all bindings defined in the 
 * transformation. Missing bindings will produce {@link Default} values (therefore
 * the transformation always produces a result, even if there are missing bindings).
 * /*  w  ww .j  a v a2  s. c  o m*/
 * @param payload the payload to transform
 * @return A transformed Json payload following the transformation definition.
 */
public JsonNode apply(JsonNode payload) {
    ObjectMapper om = new ObjectMapper();
    ObjectNode root = om.createObjectNode();
    List<Integer> pointerContext = new ArrayList<>();
    target.getChildren().stream().forEach((tNode) -> {
        resolveNode(om, tNode, root, payload, pointerContext);
    });
    return root;
}

From source file:cn.org.once.cstack.controller.ScriptingController.java

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody ArrayNode scriptingLoadAll() throws ServiceException, JsonProcessingException {
    logger.info("Load All");
    User user = authentificationUtils.getAuthentificatedUser();
    try {/* ww w .j av a  2 s .  c  o m*/
        List<Script> scripts = scriptingService.loadAllScripts();

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

        for (Script script : scripts) {
            JsonNode rootNode = mapper.createObjectNode();
            User user1 = userService.findById(script.getCreationUserId());

            ((ObjectNode) rootNode).put("id", script.getId());
            ((ObjectNode) rootNode).put("title", script.getTitle());
            ((ObjectNode) rootNode).put("content", script.getContent());
            ((ObjectNode) rootNode).put("creation_date", script.getCreationDate().toString());
            ((ObjectNode) rootNode).put("creation_user", user1.getFirstName() + " " + user1.getLastName());
            array.add(rootNode);
        }

        return array;
    } finally {
        authentificationUtils.allowUser(user);
    }
}

From source file:com.clicktravel.infrastructure.messaging.aws.sqs.SqsTypedMessageQueueTest.java

private com.amazonaws.services.sqs.model.Message mockSqsMessage(final String type, final String payload,
        final String messageId, final String receiptHandle) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode rootNode = mapper.createObjectNode();
    rootNode.put("Subject", type);
    rootNode.put("Message", payload);
    final String body = mapper.writeValueAsString(rootNode);
    final com.amazonaws.services.sqs.model.Message mockSqsMessage = mock(
            com.amazonaws.services.sqs.model.Message.class);
    when(mockSqsMessage.getMessageId()).thenReturn(messageId);
    when(mockSqsMessage.getReceiptHandle()).thenReturn(receiptHandle);
    when(mockSqsMessage.getBody()).thenReturn(body);
    return mockSqsMessage;
}

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

@Test
public void extractUrl_withMissingLinksNode_shouldReturnNull() {
    // GIVEN//from   w  w  w.  j  a  va2s.  c  o  m
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.createObjectNode();

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

    // THEN
    assertNull(url);
}

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

@Test
public void extractUrl_withMissingUrl_shouldReturnNull() {
    // GIVEN//from ww w.j  av a  2  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);
}