List of usage examples for com.fasterxml.jackson.databind ObjectMapper createObjectNode
@Override
public ObjectNode createObjectNode()
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)
From source file:com.basistech.yca.JsonNodeFlattener.java
public static JsonNode unflatten(Dictionary<String, ?> config) { Enumeration<String> keyEnum = config.keys(); List<String> keys = new ArrayList<>(); while (keyEnum.hasMoreElements()) { keys.add(keyEnum.nextElement()); }// w ww . j a va 2 s . co m Collections.sort(keys); ObjectMapper mapper = new ObjectMapper(); ObjectNode root = mapper.createObjectNode(); for (String key : keys) { Object value = config.get(key); addNode(root, key, value); } return root; }
From source file:org.roda.core.common.ClassificationPlanUtils.java
public static ObjectNode aipToJSON(IndexedAIP indexedAIP) throws IOException, RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { JsonFactory factory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(factory); ModelService model = RodaCoreFactory.getModelService(); ObjectNode node = mapper.createObjectNode(); if (indexedAIP.getTitle() != null) { node = node.put("title", indexedAIP.getTitle()); }/*from ww w . j av a2 s. c o m*/ if (indexedAIP.getId() != null) { node = node.put("id", indexedAIP.getId()); } if (indexedAIP.getParentID() != null) { node = node.put("parentId", indexedAIP.getParentID()); } if (indexedAIP.getLevel() != null) { node = node.put("descriptionlevel", indexedAIP.getLevel()); } AIP modelAIP = model.retrieveAIP(indexedAIP.getId()); if (modelAIP.getType() != null) { node = node.put("type", modelAIP.getType()); } if (modelAIP != null) { List<DescriptiveMetadata> descriptiveMetadata = modelAIP.getDescriptiveMetadata(); if (descriptiveMetadata != null && !descriptiveMetadata.isEmpty()) { ArrayNode metadata = mapper.createArrayNode(); for (DescriptiveMetadata dm : descriptiveMetadata) { ObjectNode dmNode = mapper.createObjectNode(); if (dm.getId() != null) { dmNode = dmNode.put("id", dm.getId()); } if (dm.getType() != null) { dmNode = dmNode.put("metadataType", dm.getType()); } if (dm.getVersion() != null) { dmNode = dmNode.put("metadataVersion", dm.getVersion()); } Binary b = model.retrieveDescriptiveMetadataBinary(modelAIP.getId(), dm.getId()); InputStream is = b.getContent().createInputStream(); dmNode = dmNode.put("content", new String(Base64.encodeBase64(IOUtils.toByteArray(is)))); IOUtils.closeQuietly(is); dmNode = dmNode.put("contentEncoding", "Base64"); metadata = metadata.add(dmNode); } node.set("metadata", metadata); } } return node; }
From source file:com.yahoo.bard.webservice.util.Utils.java
/** * Given a field name and a tree of json nodes, empty the contents of all the json nodes matching the field name. * This method is recursive.//from w w w. j av a 2s .c om * * @param node The root of the tree of json nodes. * @param fieldName The name of the node to be emitted. * @param mapper The object mapper that creates and empty node. */ public static void emitField(JsonNode node, String fieldName, ObjectMapper mapper) { if (node.has("context")) { ((ObjectNode) node).replace(fieldName, mapper.createObjectNode()); } for (JsonNode child : node) { emitField(child, fieldName, mapper); } }
From source file:org.opendaylight.sfc.sbrest.json.ExporterUtil.java
protected static ObjectNode getDataPlaneLocatorObjectNode(DataPlaneLocator dataPlaneLocator) { if (dataPlaneLocator == null) { return null; }/*from www . ja v a 2s .c o m*/ ObjectMapper mapper = new ObjectMapper(); ObjectNode locatorNode = null; if (dataPlaneLocator.getLocatorType() != null) { locatorNode = mapper.createObjectNode(); String type = dataPlaneLocator.getLocatorType().getImplementedInterface().getSimpleName().toLowerCase(); switch (type) { case FUNCTION: Function functionLocator = (Function) dataPlaneLocator.getLocatorType(); locatorNode.put(_FUNCTION_NAME, functionLocator.getFunctionName()); break; case IP: Ip ipLocator = (Ip) dataPlaneLocator.getLocatorType(); if (ipLocator.getIp() != null) { locatorNode.put(_IP, convertIpAddress(ipLocator.getIp())); if (ipLocator.getPort() != null) { locatorNode.put(_PORT, ipLocator.getPort().getValue()); } } break; case LISP: Lisp lispLocator = (Lisp) dataPlaneLocator.getLocatorType(); if (lispLocator.getEid() != null) locatorNode.put(_EID, convertIpAddress(lispLocator.getEid())); break; case MAC: Mac macLocator = (Mac) dataPlaneLocator.getLocatorType(); if (macLocator.getMac() != null) locatorNode.put(_MAC, macLocator.getMac().getValue()); locatorNode.put(_VLAN_ID, macLocator.getVlanId()); } } if (dataPlaneLocator.getTransport() != null) { if (locatorNode == null) { locatorNode = mapper.createObjectNode(); } locatorNode.put(_TRANSPORT, getDataPlaneLocatorTransport(dataPlaneLocator)); } return locatorNode; }
From source file:com.msopentech.odatajclient.engine.data.Serializer.java
private static void jsonLink(final ODataLink link, final Writer writer) { final ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); final ObjectNode uri = mapper.createObjectNode(); uri.put(ODataConstants.JSON_URL, link.getLink().toASCIIString()); try {// w w w.java 2 s . c o m mapper.writeValue(writer, uri); } catch (Exception e) { throw new IllegalArgumentException("While serializing JSON link", e); } }
From source file:org.apache.asterix.api.http.server.ResultUtil.java
public static ObjectNode getErrorResponse(int errorCode, String errorMessage, String errorSummary, String errorStackTrace) { ObjectMapper om = new ObjectMapper(); ObjectNode errorResp = om.createObjectNode(); ArrayNode errorArray = om.createArrayNode(); errorArray.add(errorCode);//from w w w . java 2s . co m errorArray.add(errorMessage); errorResp.set("error-code", errorArray); if (!"".equals(errorSummary)) { errorResp.put("summary", errorSummary); } else { //parse exception errorResp.put("summary", errorMessage); } errorResp.put("stacktrace", errorStackTrace); return errorResp; }
From source file:org.flowable.cmmn.editor.json.converter.util.ListenerConverterUtil.java
public static ObjectNode convertListenersToJson(ObjectMapper objectMapper, String jsonPropertyName, List<FlowableListener> listeners) { if (listeners != null) { ObjectNode listenersNode = objectMapper.createObjectNode(); ArrayNode itemsNode = objectMapper.createArrayNode(); for (FlowableListener listener : listeners) { ObjectNode propertyItemNode = objectMapper.createObjectNode(); if (StringUtils.isNotEmpty(listener.getEvent())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_EVENT, listener.getEvent()); }/*from w w w . jav a2 s . c om*/ if (StringUtils.isNotEmpty(listener.getSourceState())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_SOURCE_STATE, listener.getSourceState()); } if (StringUtils.isNotEmpty(listener.getTargetState())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_TARGET_STATE, listener.getTargetState()); } if (ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(listener.getImplementationType())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_CLASS_NAME, listener.getImplementation()); } else if (ImplementationType.IMPLEMENTATION_TYPE_EXPRESSION .equals(listener.getImplementationType())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_EXPRESSION, listener.getImplementation()); } else if (ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION .equals(listener.getImplementationType())) { propertyItemNode.put(CmmnStencilConstants.PROPERTY_LISTENER_DELEGATE_EXPRESSION, listener.getImplementation()); } if (CollectionUtils.isNotEmpty(listener.getFieldExtensions())) { ArrayNode fieldsArray = objectMapper.createArrayNode(); for (FieldExtension fieldExtension : listener.getFieldExtensions()) { ObjectNode fieldNode = objectMapper.createObjectNode(); fieldNode.put(CmmnStencilConstants.PROPERTY_FIELD_NAME, fieldExtension.getFieldName()); if (StringUtils.isNotEmpty(fieldExtension.getStringValue())) { fieldNode.put(CmmnStencilConstants.PROPERTY_FIELD_STRING_VALUE, fieldExtension.getStringValue()); } if (StringUtils.isNotEmpty(fieldExtension.getExpression())) { fieldNode.put(CmmnStencilConstants.PROPERTY_FIELD_EXPRESSION, fieldExtension.getExpression()); } fieldsArray.add(fieldNode); } propertyItemNode.set(CmmnStencilConstants.PROPERTY_LISTENER_FIELDS, fieldsArray); } itemsNode.add(propertyItemNode); } listenersNode.set(jsonPropertyName, itemsNode); return listenersNode; } return null; }
From source file:models.DataSet.java
public static List<DataSet> queryDataSet(String dataSetName, String agency, String instrument, String physicalVariable, String gridDimension, Date dataSetStartTime, Date dataSetEndTime) { List<DataSet> dataset = new ArrayList<DataSet>(); ObjectMapper mapper = new ObjectMapper(); ObjectNode queryJson = mapper.createObjectNode(); queryJson.put("name", dataSetName); queryJson.put("agencyId", agency); queryJson.put("instrument", instrument); queryJson.put("physicalVariable", physicalVariable); queryJson.put("gridDimension", gridDimension); if (dataSetEndTime != null) { queryJson.put("dataSetEndTime", dataSetEndTime.getTime()); }/*from w ww . j av a 2 s .com*/ if (dataSetStartTime != null) { queryJson.put("dataSetStartTime", dataSetStartTime.getTime()); } JsonNode dataSetNode = APICall.postAPI(DATASET_QUERY, queryJson); if (dataSetNode == null || dataSetNode.has("error") || !dataSetNode.isArray()) { return dataset; } for (int i = 0; i < dataSetNode.size(); i++) { JsonNode json = dataSetNode.path(i); DataSet newDataSet = deserializeJsonToDataSet(json); dataset.add(newDataSet); } return dataset; }
From source file:org.jsonschema2pojo.integration.MediaIT.java
public static void roundTripAssertions(ObjectMapper objectMapper, String propertyName, String jsonValue, Object javaValue) throws Exception { ObjectNode node = objectMapper.createObjectNode(); node.put(propertyName, jsonValue);//from w w w .j av a 2 s .c om Object pojo = objectMapper.treeToValue(node, classWithMediaProperties); Method getter = new PropertyDescriptor(propertyName, classWithMediaProperties).getReadMethod(); assertThat(getter.invoke(pojo), is(equalTo(javaValue))); JsonNode jsonVersion = objectMapper.valueToTree(pojo); assertThat(jsonVersion.get(propertyName).asText(), is(equalTo(jsonValue))); }
From source file:com.ikanow.aleph2.data_model.utils.JsonUtils.java
/** Takes a tuple expressed as LinkedHashMap<String, Object> (by convention the Objects are primitives, JsonNode, or POJO), and where one of the objects * is a JSON representation of the original object and creates an object by folding them all together * Note the other fields of the tuple take precedence over the JSON * @param in - the tuple//from w w w .j a v a 2s . c o m * @param mapper - the Jackson object mapper * @param json_field - optional fieldname of the string representation of the JSON - if not present then the last field is used (set to eg "" if there is no base object) * @return */ public static JsonNode foldTuple(final LinkedHashMap<String, Object> in, final ObjectMapper mapper, final Optional<String> json_field) { try { // (do this imperatively to handle the "last element can be the base object case" final Iterator<Map.Entry<String, Object>> it = in.entrySet().iterator(); ObjectNode acc = mapper.createObjectNode(); while (it.hasNext()) { final Map.Entry<String, Object> kv = it.next(); if ((json_field.isPresent() && kv.getKey().equals(json_field.get())) || !json_field.isPresent() && !it.hasNext()) { acc = (ObjectNode) ((ObjectNode) mapper.readTree(kv.getValue().toString())).setAll(acc); } else { final ObjectNode acc_tmp = acc; Patterns.match(kv.getValue()).andAct().when(String.class, s -> acc_tmp.put(kv.getKey(), s)) .when(Long.class, l -> acc_tmp.put(kv.getKey(), l)) .when(Integer.class, l -> acc_tmp.put(kv.getKey(), l)) .when(Boolean.class, b -> acc_tmp.put(kv.getKey(), b)) .when(Double.class, d -> acc_tmp.put(kv.getKey(), d)) .when(JsonNode.class, j -> acc_tmp.set(kv.getKey(), j)) .when(Float.class, f -> acc_tmp.put(kv.getKey(), f)) .when(BigDecimal.class, f -> acc_tmp.put(kv.getKey(), f)) .otherwise(x -> acc_tmp.set(kv.getKey(), BeanTemplateUtils.toJson(x))); } } return acc; } catch (Exception e) { throw new RuntimeException(e); } // (convert to unchecked exception) }