List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory textNode
public TextNode textNode(String paramString)
From source file:org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter.java
/** * Renders a {@link Patch} as a {@link JsonNode}. * // w w w . j av a 2s. c o m * @param patch the patch * @return a {@link JsonNode} containing JSON Patch. */ public JsonNode convert(Patch patch) { List<PatchOperation> operations = patch.getOperations(); JsonNodeFactory nodeFactory = JsonNodeFactory.instance; ArrayNode patchNode = nodeFactory.arrayNode(); for (PatchOperation operation : operations) { ObjectNode opNode = nodeFactory.objectNode(); opNode.set("op", nodeFactory.textNode(operation.getOp())); opNode.set("path", nodeFactory.textNode(operation.getPath())); if (operation instanceof FromOperation) { FromOperation fromOp = (FromOperation) operation; opNode.set("from", nodeFactory.textNode(fromOp.getFrom())); } Object value = operation.getValue(); if (value != null) { opNode.set("value", MAPPER.valueToTree(value)); } patchNode.add(opNode); } return patchNode; }
From source file:com.redhat.lightblue.metadata.UIDFields.java
public static void initializeUIDFields(JsonNodeFactory factory, EntityMetadata md, JsonDoc doc) { FieldCursor cursor = md.getFieldCursor(); while (cursor.next()) { FieldTreeNode node = cursor.getCurrentNode(); // Process all UID fields if (node.getType().equals(UIDType.TYPE)) { Path p = cursor.getCurrentPath(); LOGGER.debug("Processing UID field {}", p); if (node instanceof Field && required((Field) node)) { LOGGER.debug("Field {} is required", p); setRequiredField(factory, doc, p, 1, null); } else { // Here, node could be a field or an array LOGGER.debug("Field {} is not required", p); KeyValueCursor<Path, JsonNode> nodeCursor = doc.getAllNodes(p); while (nodeCursor.hasNext()) { nodeCursor.next();/*from w w w .j a va 2s . c o m*/ JsonNode valueNode = nodeCursor.getCurrentValue(); if (valueNode.isNull() || valueNode.asText().length() == 0) { String value = UIDType.newValue(); LOGGER.debug("Setting {} to {}", nodeCursor.getCurrentKey(), value); doc.modify(nodeCursor.getCurrentKey(), factory.textNode(value), true); } } } } } }
From source file:io.gravitee.management.service.ApiServiceTest.java
private EventEntity mockEvent(EventType eventType) throws Exception { final JsonNodeFactory factory = JsonNodeFactory.instance; ObjectNode node = factory.objectNode(); node.set("id", factory.textNode(API_ID)); Map<String, String> properties = new HashMap<String, String>(); properties.put(Event.EventProperties.API_ID.getValue(), API_ID); properties.put(Event.EventProperties.USERNAME.getValue(), USER_NAME); Api api = new Api(); api.setId(API_ID);/*from w w w .ja v a 2 s . c o m*/ EventEntity event = new EventEntity(); event.setType(eventType); event.setId(UUID.randomUUID().toString()); event.setPayload(objectMapper.writeValueAsString(api)); event.setCreatedAt(new Date()); event.setUpdatedAt(event.getCreatedAt()); event.setProperties(properties); return event; }
From source file:com.redhat.smonkey.RndString.java
@Override public JsonNode generate(JsonNodeFactory nodeFactory, JsonNode data, Monkey mon) { int minlength = 1; int maxlength = 32; String charset = DEFAULT_CHARSET; minlength = Utils.asInt(data.get("minlength"), minlength); maxlength = Utils.asInt(data.get("maxlength"), maxlength); int len = Utils.asInt(data.get("length"), -1); charset = Utils.asString(data.get("charset"), charset); if (len <= 0) { len = Utils.rndi(minlength, maxlength); }//from w w w . jav a2s . co m StringBuilder bld = new StringBuilder(); for (int i = 0; i < len; i++) bld.append(charset.charAt(Utils.rnd.nextInt(charset.length()))); return nodeFactory.textNode(bld.toString()); }
From source file:com.netflix.genie.web.controllers.JobRestController.java
/** * Get the status of the given job if it exists. * * @param id The id of the job to get status for * @return The status of the job as one of: {@link JobStatus} * @throws GenieException on error//w ww. j a va2s . c o m */ @RequestMapping(value = "/{id}/status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public JsonNode getJobStatus(@PathVariable("id") final String id) throws GenieException { log.debug("[getJobStatus] Called for job with id: {}", id); final JsonNodeFactory factory = JsonNodeFactory.instance; return factory.objectNode().set("status", factory.textNode(this.jobSearchService.getJobStatus(id).toString())); }
From source file:com.redhat.lightblue.config.rdbms.RDBMSDataSourceMap.java
@Override public String toString() { JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(true); ObjectNode root = jsonNodeFactory.objectNode(); ObjectNode datasourcesNode = jsonNodeFactory.objectNode(); ObjectNode databasesNode = jsonNodeFactory.objectNode(); ObjectNode dbMapNode = jsonNodeFactory.objectNode(); ObjectNode dsMapNode = jsonNodeFactory.objectNode(); root.set("datasources", datasourcesNode); root.set("databases", databasesNode); root.set("dbMap", dbMapNode); root.set("dsMap", dsMapNode); if (datasources != null) { for (Map.Entry<String, RDBMSDataSourceConfiguration> a : datasources.entrySet()) { datasourcesNode.set(a.getKey(), jsonNodeFactory.textNode(a.getValue().toString())); }/*from www . j a va2 s . c o m*/ } if (databases != null) { for (Map.Entry<String, RDBMSDataSourceConfiguration> a : databases.entrySet()) { databasesNode.set(a.getKey(), jsonNodeFactory.textNode(a.getValue().toString())); } } if (dbMap != null) { for (Map.Entry<String, DataSource> a : dbMap.entrySet()) { dbMapNode.set(a.getKey(), jsonNodeFactory.textNode(a.getValue().toString())); } } if (dsMap != null) { for (Map.Entry<String, DataSource> a : dsMap.entrySet()) { dsMapNode.set(a.getKey(), jsonNodeFactory.textNode(a.getValue().toString())); } } return JsonUtils.prettyPrint(root); }
From source file:com.redhat.lightblue.metadata.UIDFields.java
private static void setRequiredField(JsonNodeFactory factory, JsonDoc doc, Path fieldPath, int startSegment, Path resolvedPath) {// w w w . j av a2s . c o m LOGGER.debug("setRequiredField: fieldPath:{} startSegment:{} resolvedPath:{}", fieldPath, startSegment, resolvedPath); int nSegments = fieldPath.numSegments(); boolean array = false; for (int segment = startSegment; segment < nSegments; segment++) { if (fieldPath.head(segment).equals(Path.ANY)) { array = true; MutablePath arrPath = new MutablePath(fieldPath.prefix(segment)); if (resolvedPath != null) { arrPath.rewriteIndexes(resolvedPath); } LOGGER.debug("Processing segment {}", arrPath); JsonNode node = doc.get(arrPath); if (node != null) { int size = node.size(); LOGGER.debug("{} size={}", arrPath, size); arrPath.push(0); for (int i = 0; i < size; i++) { arrPath.setLast(i); setRequiredField(factory, doc, fieldPath, segment + 1, arrPath.immutableCopy()); } } break; } } if (!array) { Path p; if (resolvedPath == null) { p = fieldPath; } else { p = new MutablePath(fieldPath).rewriteIndexes(resolvedPath); } LOGGER.debug("Setting {}", p); JsonNode valueNode = doc.get(p); if (valueNode == null || valueNode.isNull() || valueNode.asText().length() == 0) { String value = UIDType.newValue(); LOGGER.debug("Setting {} to {}", p, value); doc.modify(p, factory.textNode(value), true); } } }
From source file:com.redhat.smonkey.PropertyFile.java
@Override public JsonNode generate(JsonNodeFactory nodeFactory, JsonNode data, Monkey mon) { String s = Utils.asString(data.get("file"), null); ParsedProperties p;/*from w w w . j av a 2 s . c om*/ if (s == null) p = defaultProperties; else { p = propertyFiles.get(s); if (p == null) { try { Properties pr = new Properties(); pr.load(new FileInputStream(s)); propertyFiles.put(s, p = new ParsedProperties(pr)); } catch (Exception e) { throw new RuntimeException(e); } } } s = Utils.asString(data.get("choose"), null); if (s != null) { String[] values = p.getValues(s); if (values != null) { int x = Utils.rnd.nextInt(values.length); return nodeFactory.textNode(values[x]); } else throw new RuntimeException("No property:" + s); } else { s = Utils.asString(data.get("property"), null); if (s == null) throw new RuntimeException("choose or property is required for $propfile"); String value = p.getValue(s); if (value == null) throw new RuntimeException("No property:" + s); return nodeFactory.textNode(value); } }
From source file:com.redhat.smonkey.RndDate.java
@Override public JsonNode generate(JsonNodeFactory nodeFactory, JsonNode data, Monkey mon) { SimpleDateFormat fmt;/*from www. ja va 2s. c o m*/ JsonNode formatNode = data.get("format"); if (formatNode == null) { fmt = DEFAULT_FORMATTER; } else { fmt = new SimpleDateFormat(formatNode.asText()); } try { String min = Utils.asString(data.get("min"), null); Date minDate = min == null ? null : fmt.parse(min); String max = Utils.asString(data.get("max"), null); Date maxDate = max == null ? null : fmt.parse(max); if (minDate == null && maxDate == null) { String base = Utils.asString(data.get("base"), fmt.format(new Date())); Date baseDate = fmt.parse(base); String fwd = Utils.asString(data.get("fwd"), null); String bck = Utils.asString(data.get("bck"), null); if (fwd == null && bck == null) return nodeFactory.textNode(base); if (fwd != null) maxDate = applyDiff(baseDate, fwd, false); if (bck != null) minDate = applyDiff(baseDate, bck, true); } return nodeFactory.textNode(fmt.format(genDate(minDate, maxDate))); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.redhat.lightblue.metadata.rdbms.converter.RDBMSContext.java
@Override public String toString() { JsonNodeFactory jsonNodeFactory1 = jsonNodeFactory; if (jsonNodeFactory == null) { jsonNodeFactory1 = JsonNodeFactory.withExactBigDecimals(true); }/* w ww . java 2 s . c om*/ ObjectNode objectNode = jsonNodeFactory1.objectNode(); NullNode nullNode = jsonNodeFactory1.nullNode(); String nullNodeString = JsonUtils.prettyPrint(nullNode); String s = null; JsonNode j = null; try { s = fromToQueryRange == null ? nullNodeString : fromToQueryRange.toString(); objectNode.set("fromToQueryRange", JsonUtils.json(s)); j = dataSource == null ? nullNode : jsonNodeFactory1.textNode(dataSource.toString()); objectNode.set("dataSource", j); j = dataSourceName == null ? nullNode : jsonNodeFactory1.textNode(dataSourceName.toString()); objectNode.set("dataSourceName", j); j = connection == null ? nullNode : jsonNodeFactory1.textNode(connection.toString()); objectNode.set("connection", j); j = preparedStatement == null ? nullNode : jsonNodeFactory1.textNode(preparedStatement.toString()); objectNode.set("preparedStatement", j); j = resultBoolean == null ? nullNode : jsonNodeFactory1.booleanNode(resultBoolean); objectNode.set("resultBoolean", j); j = resultInteger == null ? nullNode : jsonNodeFactory1.numberNode(resultInteger); objectNode.set("resultInteger", j); j = rowMapper == null ? nullNode : jsonNodeFactory1.textNode(rowMapper.toString()); objectNode.set("rowMapper", j); if (resultList != null) { ArrayNode jsonNodes = jsonNodeFactory1.arrayNode(); for (T a : resultList) { jsonNodes.add(a.toString()); } objectNode.set("resultList", jsonNodes); } else { objectNode.set("resultList", nullNode); } s = rdbms == null ? nullNodeString : rdbms.toString(); objectNode.set("rdbms", JsonUtils.json(s)); j = sql == null ? nullNode : jsonNodeFactory1.textNode(sql.toString()); objectNode.set("sql", j); j = type == null ? nullNode : jsonNodeFactory1.textNode(type.toString()); objectNode.set("type", j); j = entityMetadata == null ? nullNode : jsonNodeFactory1.textNode(entityMetadata.toString()); objectNode.set("entityMetadata", j); j = queryExpression == null ? nullNode : jsonNodeFactory1.textNode(queryExpression.toString()); objectNode.set("queryExpression", j); j = projection == null ? nullNode : jsonNodeFactory1.textNode(projection.toString()); objectNode.set("projection", j); j = sort == null ? nullNode : jsonNodeFactory1.textNode(sort.toString()); objectNode.set("sort", j); if (temporaryVariable != null) { ObjectNode jsonNodes = jsonNodeFactory1.objectNode(); for (Map.Entry<String, Object> a : temporaryVariable.entrySet()) { jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString())); } objectNode.set("temporaryVariable", jsonNodes); } else { objectNode.set("temporaryVariable", nullNode); } if (in != null) { ArrayNode jsonNodes = jsonNodeFactory1.arrayNode(); for (InOut a : in) { jsonNodes.add(a.toString()); } objectNode.set("in", jsonNodes); } else { objectNode.set("in", nullNode); } s = out == null ? nullNodeString : out.toString(); if (out != null) { ArrayNode jsonNodes = jsonNodeFactory1.arrayNode(); for (InOut a : out) { jsonNodes.add(a.toString()); } objectNode.set("out", jsonNodes); } else { objectNode.set("out", nullNode); } s = inVar == null ? nullNodeString : inVar.toString(); objectNode.set("inVar", JsonUtils.json(s)); s = outVar == null ? nullNodeString : outVar.toString(); objectNode.set("outVar", JsonUtils.json(s)); objectNode.set("initialInput", JsonUtils.json(Boolean.toString(initialInput))); if (inputMappedByField != null) { ObjectNode jsonNodes = jsonNodeFactory1.objectNode(); for (Map.Entry<String, Object> a : inputMappedByField.entrySet()) { jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString())); } objectNode.set("inputMappedByField", jsonNodes); } else { objectNode.set("inputMappedByField", nullNode); } if (inputMappedByColumn != null) { ObjectNode jsonNodes = jsonNodeFactory1.objectNode(); for (Map.Entry<String, Object> a : inputMappedByColumn.entrySet()) { jsonNodes.set(a.getKey(), jsonNodeFactory1.textNode(a.getValue().toString())); } objectNode.set("inputMappedByColumn", jsonNodes); } else { objectNode.set("inputMappedByColumn", nullNode); } s = jsonNodeFactory == null ? nullNodeString : jsonNodeFactory.toString(); objectNode.set("jsonNodeFactory", jsonNodeFactory1.textNode(s)); s = RDBMSDataSourceResolver == null ? nullNodeString : RDBMSDataSourceResolver.toString(); objectNode.set("RDBMSDataSourceResolver", JsonUtils.json(s)); s = fieldAccessRoleEvaluator == null ? nullNodeString : fieldAccessRoleEvaluator.toString(); objectNode.set("fieldAccessRoleEvaluator", jsonNodeFactory1.textNode(s)); s = CRUDOperationName == null ? nullNodeString : CRUDOperationName.toString(); objectNode.set("CRUDOperationName", jsonNodeFactory1.textNode(s)); s = crudOperationContext == null ? nullNodeString : crudOperationContext.toString(); objectNode.set("crudOperationContext", jsonNodeFactory1.textNode(s)); s = currentLoopOperator == null ? nullNodeString : currentLoopOperator.toString(); objectNode.set("currentLoopOperator", jsonNodeFactory1.textNode(s)); s = updateExpression == null ? nullNodeString : updateExpression.toString(); objectNode.set("updateExpression", jsonNodeFactory1.textNode(s)); } catch (IOException e) { throw new IllegalStateException(e); } return JsonUtils.prettyPrint(objectNode); }