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:com.gsma.mobileconnect.impl.ParseIDTokenTest.java

private JsonNode buildJsonNode() {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.createObjectNode();
}

From source file:org.activiti.designer.eclipse.navigator.cloudrepo.ProcessModelContentProvider.java

public Object[] getChildren(Object parentElement) {
    if (parentElement instanceof ActivitiCloudEditorRoot) {
        if (modelsNode == null) {
            try {
                initializeRootElements();
            } catch (final ActivitiCloudEditorException e) {
                String detailMessage = null;
                if (e.getExceptionNode() != null) {
                    detailMessage = e.getExceptionNode().get("message").asText();
                } else {
                    detailMessage = e.getMessage();
                }/*from   w  ww . java2  s. c om*/
                // creating fake entry
                ObjectMapper objectMapper = new ObjectMapper();
                modelsNode = objectMapper.createObjectNode();
                ArrayNode modelArrayNode = objectMapper.createArrayNode();
                ((ObjectNode) modelsNode).put("data", modelArrayNode);
                ObjectNode errorNode = objectMapper.createObjectNode();
                modelArrayNode.add(errorNode);
                errorNode.put("name", "Process models could not be retrieved: " + detailMessage);
            }
        }

        if (modelsNode != null) {
            ArrayNode modelArrayNode = (ArrayNode) modelsNode.get("data");
            Object[] objectArray = new Object[modelArrayNode.size()];
            for (int i = 0; i < modelArrayNode.size(); i++) {
                JsonNode modelNode = modelArrayNode.get(i);
                objectArray[i] = modelNode;
            }
            return objectArray;
        } else {
            return EMPTY_ARRAY;
        }

    } else {
        return EMPTY_ARRAY;
    }
}

From source file:org.apache.asterix.api.http.server.QueryWebInterfaceServlet.java

private void doPost(IServletResponse response) throws IOException {
    ServletUtils.setContentType(response, IServlet.ContentType.APPLICATION_JSON, IServlet.Encoding.UTF8);
    ExternalProperties externalProperties = AppContextInfo.INSTANCE.getExternalProperties();
    response.setStatus(HttpResponseStatus.OK);
    ObjectMapper om = new ObjectMapper();
    ObjectNode obj = om.createObjectNode();
    try {//from w  w  w .ja v a  2 s  .co m
        PrintWriter out = response.writer();
        obj.put("api_port", String.valueOf(externalProperties.getAPIServerPort()));
        out.println(obj.toString());
        return;
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failure writing response", e);
    }
    try {
        response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failure setting response status", e);
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.FormatIT.java

@Test
public void valueCanBeSerializedAndDeserialized() throws NoSuchMethodException, IOException,
        IntrospectionException, IllegalAccessException, InvocationTargetException {

    ObjectMapper objectMapper = new ObjectMapper();

    ObjectNode node = objectMapper.createObjectNode();
    node.put(propertyName, jsonValue.toString());

    Object pojo = objectMapper.treeToValue(node, classWithFormattedProperties);

    Method getter = new PropertyDescriptor(propertyName, classWithFormattedProperties).getReadMethod();

    assertThat(getter.invoke(pojo).toString(), is(equalTo(javaValue.toString())));

    JsonNode jsonVersion = objectMapper.valueToTree(pojo);

    assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue.toString())));

}

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

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
    ObjectMapper mapper = Mapper.getInstance();
    ObjectNode node = mapper.createObjectNode();

    if (this.score != null) {
        node.put("score", this.getScore().toJSONNode(version));
    }/*from   www. java2s. c o m*/
    if (this.success != null) {
        node.put("success", this.getSuccess());
    }
    if (this.completion != null) {
        node.put("completion", this.getCompletion());
    }
    if (this.duration != null) {
        //
        // ISOPeriodFormat includes milliseconds but the spec only allows
        // hundredths of a second here, so get the normal string, then truncate
        // the last digit to provide the proper precision
        //
        String shortenedDuration = ISOPeriodFormat.standard().print(this.getDuration())
                .replaceAll("(\\.\\d\\d)\\dS", "$1S");

        node.put("duration", shortenedDuration);
    }
    if (this.response != null) {
        node.put("response", this.getResponse());
    }
    if (this.extensions != null) {
        node.put("extensions", this.getExtensions().toJSONNode(version));
    }

    return node;
}

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;
    }//w  w w  .j  a  v a 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;
}

From source file:ru.histone.spring.mvc.HistoneView.java

@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ObjectMapper jackson = new ObjectMapper();
    ObjectNode context = jackson.createObjectNode();
    for (Map.Entry<String, Object> entry : model.entrySet()) {
        if (entry.getKey().startsWith("context.")) {
            String key = entry.getKey().substring(8);
            Object val = entry.getValue();

            if (val instanceof JsonNode) {
                context.put(key, (JsonNode) val);
            } else {
                JsonNode jsonVal = jackson.valueToTree(val);
                context.put(key, jsonVal);
            }/*from   w  w  w.  j  a  v a2 s . c  o m*/
        }
    }

    response.setCharacterEncoding("UTF-8");
    Reader content = new InputStreamReader(getServletContext().getResourceAsStream(getUrl()), "UTF-8");
    getHistone().setGlobalProperty(GlobalProperty.BASE_URI, "webapp:" + getUrl());
    String result = getHistone().evaluate(getUrl(), content, context);
    response.getOutputStream().write(result.getBytes("UTF-8"));
}

From source file:com.googlecode.jsonschema2pojo.rules.ArrayRuleTest.java

@Test
public void arrayWithUniqueItemsProducesSet() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());

    ObjectMapper mapper = new ObjectMapper();

    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "integer");

    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.put("uniqueItems", true);
    propertyNode.put("items", itemsNode);

    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, mock(Schema.class));

    assertThat(propertyType, notNullValue());
    assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
    assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Integer.class.getName()));
}

From source file:org.jolokia.client.request.J4pConnectionPoolingIntegrationTest.java

private String getJsonResponse(String message) {
    final ObjectMapper objectMapper = new ObjectMapper();
    final ObjectNode node = objectMapper.createObjectNode();

    final ArrayNode arrayNode = objectMapper.createArrayNode();
    arrayNode.add("java.lang:type=Memory");
    node.putArray("value").addAll(arrayNode);

    node.put("status", 200);
    node.put("timestamp", 1244839118);

    return node.toString();
}

From source file:com.googlecode.jsonschema2pojo.rules.ArrayRuleTest.java

@Test
public void arrayDefaultsToNonUnique() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());

    ObjectMapper mapper = new ObjectMapper();

    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "boolean");

    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.put("uniqueItems", false);
    propertyNode.put("items", itemsNode);

    Schema schema = mock(Schema.class);
    when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));

    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);

    assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}