Example usage for com.fasterxml.jackson.databind.node ObjectNode putNull

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode putNull

Introduction

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

Prototype

public ObjectNode putNull(String paramString) 

Source Link

Usage

From source file:com.basistech.yca.JsonNodeFlattener.java

private static void setValue(ObjectNode node, String fieldName, Object value) {
    if (value == NULL) {
        node.putNull(fieldName);
        return;//from w  w  w .j  a  v  a 2  s.c  o  m
    }
    /* the following is slow but compact to write. I'm not in a hurry. */
    try {
        Method suitableSetter = ObjectNode.class.getMethod("put", String.class, value.getClass());
        suitableSetter.invoke(node, fieldName, value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

private void setValue(ObjectNode jsonEntity, String name, ValueNode node) {
    Object value = node.getValue();
    if (value == null) {
        jsonEntity.putNull(node.getName());
    } else if (value instanceof String) {
        jsonEntity.put(node.getName(), (String) value);
    } else if (value instanceof Long) {
        jsonEntity.put(node.getName(), (Long) value);
    } else if (value instanceof Date) {
        jsonEntity.put(node.getName(), df.format((Date) value));
    } else if (value instanceof Integer) {
        jsonEntity.put(node.getName(), (Integer) value);
    } else if (value instanceof BigDecimal) {
        jsonEntity.put(node.getName(), (BigDecimal) value);
    } else if (value instanceof Boolean) {
        jsonEntity.put(node.getName(), (Boolean) value);
    } else if (value == NotLoaded.VALUE) {
        //we skip these properties
    } else {//from   w w w .ja  v  a 2  s  . co  m
        throw new IllegalStateException("Cannot serialize value of type " + value.getClass().getSimpleName());
    }
}

From source file:org.springframework.tuple.TupleToJsonStringConverter.java

private ObjectNode toObjectNode(Tuple source) {
    ObjectNode root = mapper.createObjectNode();
    for (int i = 0; i < source.size(); i++) {
        Object value = source.getValues().get(i);
        String name = source.getFieldNames().get(i);
        if (value == null) {
            root.putNull(name);
        } else {//from  w ww .j  av  a  2  s .co  m
            root.putPOJO(name, toNode(value));
        }
    }
    return root;
}

From source file:org.activiti.rest.service.api.identity.UserResourceTest.java

/**
 * Test updating a single user passing in no fields in the json, user should remain unchanged.
 *//*from   ww  w. j av a2s. c  o m*/
public void testUpdateUserNullFields() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;

        ObjectNode taskUpdateRequest = objectMapper.createObjectNode();
        taskUpdateRequest.putNull("firstName");
        taskUpdateRequest.putNull("lastName");
        taskUpdateRequest.putNull("email");
        taskUpdateRequest.putNull("password");

        HttpPut httpPut = new HttpPut(
                SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()));
        httpPut.setEntity(new StringEntity(taskUpdateRequest.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testuser", responseNode.get("id").textValue());
        assertTrue(responseNode.get("firstName").isNull());
        assertTrue(responseNode.get("lastName").isNull());
        assertTrue(responseNode.get("email").isNull());
        assertTrue(responseNode.get("url").textValue()
                .endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())));

        // Check user is updated in activiti
        newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult();
        assertNull(newUser.getLastName());
        assertNull(newUser.getFirstName());
        assertNull(newUser.getEmail());

    } finally {

        // Delete user after test fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}

From source file:org.activiti.rest.service.api.runtime.process.ProcessDefinitionPropertiesResource.java

@Get
public ObjectNode getStartFormProperties() {
    if (authenticate() == false)
        return null;

    String processDefinitionId = (String) getRequest().getAttributes().get("processDefinitionId");
    StartFormData startFormData = ActivitiUtil.getFormService().getStartFormData(processDefinitionId);

    ObjectNode responseJSON = new ObjectMapper().createObjectNode();

    ArrayNode propertiesJSON = new ObjectMapper().createArrayNode();

    if (startFormData != null) {

        List<FormProperty> properties = startFormData.getFormProperties();

        for (FormProperty property : properties) {
            ObjectNode propertyJSON = new ObjectMapper().createObjectNode();
            propertyJSON.put("id", property.getId());
            propertyJSON.put("name", property.getName());

            if (property.getValue() != null) {
                propertyJSON.put("value", property.getValue());
            } else {
                propertyJSON.putNull("value");
            }/*w w  w .  j  a va 2 s.co  m*/

            if (property.getType() != null) {
                propertyJSON.put("type", property.getType().getName());

                if (property.getType() instanceof EnumFormType) {
                    @SuppressWarnings("unchecked")
                    Map<String, String> valuesMap = (Map<String, String>) property.getType()
                            .getInformation("values");
                    if (valuesMap != null) {
                        ArrayNode valuesArray = new ObjectMapper().createArrayNode();
                        propertyJSON.put("enumValues", valuesArray);

                        for (String key : valuesMap.keySet()) {
                            ObjectNode valueJSON = new ObjectMapper().createObjectNode();
                            valueJSON.put("id", key);
                            valueJSON.put("name", valuesMap.get(key));
                            valuesArray.add(valueJSON);
                        }
                    }
                }

            } else {
                propertyJSON.put("type", "String");
            }

            propertyJSON.put("required", property.isRequired());
            propertyJSON.put("readable", property.isReadable());
            propertyJSON.put("writable", property.isWritable());

            propertiesJSON.add(propertyJSON);
        }
    }

    responseJSON.put("data", propertiesJSON);
    return responseJSON;
}

From source file:org.activiti.rest.service.api.legacy.TaskPropertiesResource.java

@Get
public ObjectNode getTaskProperties() {
    if (authenticate() == false)
        return null;
    String taskId = (String) getRequest().getAttributes().get("taskId");
    TaskFormData taskFormData = ActivitiUtil.getFormService().getTaskFormData(taskId);

    ObjectNode responseJSON = new ObjectMapper().createObjectNode();

    ArrayNode propertiesJSON = new ObjectMapper().createArrayNode();

    if (taskFormData != null) {

        List<FormProperty> properties = taskFormData.getFormProperties();

        for (FormProperty property : properties) {
            ObjectNode propertyJSON = new ObjectMapper().createObjectNode();
            propertyJSON.put("id", property.getId());
            propertyJSON.put("name", property.getName());

            if (property.getValue() != null) {
                propertyJSON.put("value", property.getValue());
            } else {
                propertyJSON.putNull("value");
            }/*w  w w . j  av a2s.c  o  m*/

            if (property.getType() != null) {
                propertyJSON.put("type", property.getType().getName());

                if (property.getType() instanceof EnumFormType) {
                    @SuppressWarnings("unchecked")
                    Map<String, String> valuesMap = (Map<String, String>) property.getType()
                            .getInformation("values");
                    if (valuesMap != null) {
                        ArrayNode valuesArray = new ObjectMapper().createArrayNode();
                        propertyJSON.put("enumValues", valuesArray);

                        for (String key : valuesMap.keySet()) {
                            ObjectNode valueJSON = new ObjectMapper().createObjectNode();
                            valueJSON.put("id", key);
                            valueJSON.put("name", valuesMap.get(key));
                            valuesArray.add(valueJSON);
                        }
                    }
                }

            } else {
                propertyJSON.put("type", "String");
            }

            propertyJSON.put("required", property.isRequired());
            propertyJSON.put("readable", property.isReadable());
            propertyJSON.put("writable", property.isWritable());

            propertiesJSON.add(propertyJSON);
        }
    }

    responseJSON.put("data", propertiesJSON);

    return responseJSON;
}

From source file:org.activiti.rest.service.api.legacy.process.LegacyProcessInstanceResource.java

@Get
public ObjectNode getProcessInstance() {
    if (authenticate() == false)
        return null;

    String processInstanceId = (String) getRequest().getAttributes().get("processInstanceId");
    HistoricProcessInstance instance = ActivitiUtil.getHistoryService().createHistoricProcessInstanceQuery()
            .processInstanceId(processInstanceId).singleResult();

    if (instance == null) {
        throw new ActivitiObjectNotFoundException("Process instance not found for id " + processInstanceId,
                ProcessInstance.class);
    }/*from  w w  w . j av a  2  s  .c o m*/

    ObjectNode responseJSON = new ObjectMapper().createObjectNode();
    responseJSON.put("processInstanceId", instance.getId());
    if (instance.getBusinessKey() != null) {
        responseJSON.put("businessKey", instance.getBusinessKey());
    } else {
        responseJSON.putNull("businessKey");
    }
    responseJSON.put("processDefinitionId", instance.getProcessDefinitionId());
    responseJSON.put("startTime", RequestUtil.dateToString(instance.getStartTime()));
    responseJSON.put("startActivityId", instance.getStartActivityId());
    if (instance.getStartUserId() != null) {
        responseJSON.put("startUserId", instance.getStartUserId());
    } else {
        responseJSON.putNull("startUserId");
    }

    if (instance.getEndTime() == null) {
        responseJSON.put("completed", false);
    } else {
        responseJSON.put("completed", true);
        responseJSON.put("endTime", RequestUtil.dateToString(instance.getEndTime()));
        responseJSON.put("endActivityId", instance.getEndActivityId());
        responseJSON.put("duration", instance.getDurationInMillis());
    }

    addTaskList(processInstanceId, responseJSON);
    addActivityList(processInstanceId, responseJSON);
    addVariableList(processInstanceId, responseJSON);

    return responseJSON;
}

From source file:com.zero_x_baadf00d.partialize.Partialize.java

/**
 * Add requested item on the partial JSON document.
 *
 * @param depth         Current depth level
 * @param aliasField    The alias field name
 * @param field         The field name//  w  w  w .jav a 2  s  . co  m
 * @param args          The field Arguments
 * @param partialObject The current partial JSON document part
 * @param clazz         The class of the object to add
 * @param object        The object to add
 * @since 16.01.18
 */
private void internalBuild(final int depth, final String aliasField, final String field, final String args,
        final ObjectNode partialObject, final Class<?> clazz, final Object object) {
    if (object == null) {
        partialObject.putNull(aliasField);
    } else if (object instanceof String) {
        partialObject.put(aliasField, (String) object);
    } else if (object instanceof Integer) {
        partialObject.put(aliasField, (Integer) object);
    } else if (object instanceof Long) {
        partialObject.put(aliasField, (Long) object);
    } else if (object instanceof Double) {
        partialObject.put(aliasField, (Double) object);
    } else if (object instanceof UUID) {
        partialObject.put(aliasField, object.toString());
    } else if (object instanceof Boolean) {
        partialObject.put(aliasField, (Boolean) object);
    } else if (object instanceof JsonNode) {
        partialObject.putPOJO(aliasField, object);
    } else if (object instanceof Collection<?>) {
        final ArrayNode partialArray = partialObject.putArray(aliasField);
        if (((Collection<?>) object).size() > 0) {
            for (final Object o : (Collection<?>) object) {
                this.internalBuild(depth, aliasField, field, args, partialArray, o.getClass(), o);
            }
        }
    } else if (object instanceof Map<?, ?>) {
        this.buildPartialObject(depth + 1, args, object.getClass(), object,
                partialObject.putObject(aliasField));
    } else if (object instanceof Enum) {
        final String tmp = object.toString();
        try {
            partialObject.put(aliasField, Integer.valueOf(tmp));
        } catch (NumberFormatException ignore) {
            partialObject.put(aliasField, tmp);
        }
    } else {
        final Converter converter = PartializeConverterManager.getInstance().getConverter(object.getClass());
        if (converter != null) {
            converter.convert(aliasField, object, partialObject);
        } else {
            this.buildPartialObject(depth + 1, args, object.getClass(), object,
                    partialObject.putObject(aliasField));
        }
    }
}

From source file:org.activiti.rest.service.api.legacy.process.LegacyProcessInstanceResource.java

private void addActivityList(String processInstanceId, ObjectNode responseJSON) {
    List<HistoricActivityInstance> activityList = ActivitiUtil.getHistoryService()
            .createHistoricActivityInstanceQuery().processInstanceId(processInstanceId)
            .orderByHistoricActivityInstanceStartTime().asc().list();

    if (activityList != null && activityList.size() > 0) {
        ArrayNode activitiesJSON = new ObjectMapper().createArrayNode();
        responseJSON.put("activities", activitiesJSON);
        for (HistoricActivityInstance historicActivityInstance : activityList) {
            ObjectNode activityJSON = new ObjectMapper().createObjectNode();
            activityJSON.put("activityId", historicActivityInstance.getActivityId());
            if (historicActivityInstance.getActivityName() != null) {
                activityJSON.put("activityName", historicActivityInstance.getActivityName());
            } else {
                activityJSON.putNull("activityName");
            }//from   w ww .ja  v  a  2s .co  m
            activityJSON.put("activityType", historicActivityInstance.getActivityType());
            activityJSON.put("startTime", RequestUtil.dateToString(historicActivityInstance.getStartTime()));
            if (historicActivityInstance.getEndTime() == null) {
                activityJSON.put("completed", false);
            } else {
                activityJSON.put("completed", true);
                activityJSON.put("endTime", RequestUtil.dateToString(historicActivityInstance.getEndTime()));
                activityJSON.put("duration", historicActivityInstance.getDurationInMillis());
            }
            activitiesJSON.add(activityJSON);
        }
    }
}

From source file:org.activiti.rest.service.api.legacy.process.LegacyProcessInstanceResource.java

private void addTaskList(String processInstanceId, ObjectNode responseJSON) {
    List<HistoricTaskInstance> taskList = ActivitiUtil.getHistoryService().createHistoricTaskInstanceQuery()
            .processInstanceId(processInstanceId).orderByHistoricTaskInstanceStartTime().asc().list();

    if (taskList != null && taskList.size() > 0) {
        ArrayNode tasksJSON = new ObjectMapper().createArrayNode();
        responseJSON.put("tasks", tasksJSON);
        for (HistoricTaskInstance historicTaskInstance : taskList) {
            ObjectNode taskJSON = new ObjectMapper().createObjectNode();
            taskJSON.put("taskId", historicTaskInstance.getId());
            taskJSON.put("taskDefinitionKey", historicTaskInstance.getTaskDefinitionKey());
            if (historicTaskInstance.getName() != null) {
                taskJSON.put("taskName", historicTaskInstance.getName());
            } else {
                taskJSON.putNull("taskName");
            }/* w  w  w  . ja  va 2s .  c om*/
            if (historicTaskInstance.getDescription() != null) {
                taskJSON.put("description", historicTaskInstance.getDescription());
            } else {
                taskJSON.putNull("description");
            }
            if (historicTaskInstance.getOwner() != null) {
                taskJSON.put("owner", historicTaskInstance.getOwner());
            } else {
                taskJSON.putNull("owner");
            }
            if (historicTaskInstance.getAssignee() != null) {
                taskJSON.put("assignee", historicTaskInstance.getAssignee());
            } else {
                taskJSON.putNull("assignee");
            }
            taskJSON.put("startTime", RequestUtil.dateToString(historicTaskInstance.getStartTime()));
            if (historicTaskInstance.getDueDate() != null) {
                taskJSON.put("dueDate", RequestUtil.dateToString(historicTaskInstance.getDueDate()));
            } else {
                taskJSON.putNull("dueDate");
            }
            if (historicTaskInstance.getEndTime() == null) {
                taskJSON.put("completed", false);
            } else {
                taskJSON.put("completed", true);
                taskJSON.put("endTime", RequestUtil.dateToString(historicTaskInstance.getEndTime()));
                taskJSON.put("duration", historicTaskInstance.getDurationInMillis());
            }
            tasksJSON.add(taskJSON);
        }
    }
}