public static void convertMessagesToJson(Collection<Message> messages, ObjectNode propertiesNode) { String propertyName = "messages"; ArrayNode messagesNode = objectMapper.createArrayNode(); for (Message message : messages) { ObjectNode propertyItemNode = objectMapper.createObjectNode(); propertyItemNode.put(PROPERTY_MESSAGE_ID, message.getId()); propertyItemNode.put(PROPERTY_MESSAGE_NAME, message.getName()); propertyItemNode.put(PROPERTY_MESSAGE_ITEM_REF, message.getItemRef()); messagesNode.add(propertyItemNode); } propertiesNode.set(propertyName, messagesNode); }
private static Map<String, JsonNode> groupArrayElementsByKey(ArrayNode fields, String key) { Map<String, JsonNode> fieldsMap = new HashMap<String, JsonNode>(); for (int i = 0; i < fields.size(); i++) { JsonNode field = fields.get(i); fieldsMap.put(field.get(key).asText(), field); } return fieldsMap; }
private static void getNestedFieldName(JsonNode jsonNode, String fieldName, Collection<String> results) { if (jsonNode instanceof ArrayNode) { Iterator<JsonNode> elements = ((ArrayNode) jsonNode).elements(); if (!elements.hasNext()) { results.add(fieldName); return; } while (elements.hasNext()) { JsonNode element = elements.next(); getNestedFieldName(element, fieldName, results); } } else { Iterator<Entry<String, JsonNode>> fields = jsonNode.fields(); if (!fields.hasNext()) { results.add(fieldName); return; } while (fields.hasNext()) { Entry<String, JsonNode> fieldKV = fields.next(); String key = fieldKV.getKey(); getNestedFieldName(fieldKV.getValue(), fieldName.length() == 0 ? key : fieldName + "." + key, results); } } }
/** * Method that will set specified field, replacing old value, * if any. * * @param value to set field to; if null, will be converted * to a {@link NullNode} first (to remove field entry, call * {@link #remove} instead) * * @return Old value of the field, if any; null if there was no * old value. */ public JsonNode set(int index, JsonNode value) { if (value == null) { // let's not store 'raw' nulls but nodes value = nullNode(); } if (index < 0 || index >= _children.size()) { throw new IndexOutOfBoundsException("Illegal index "+ index +", array size "+size()); } return _children.set(index, value); }
public void changeUserTaskCandidateGroup(String id, String candidateGroup, boolean overwriteOtherChangedEntries, ObjectNode infoNode) { ArrayNode valuesNode = null; if (overwriteOtherChangedEntries) { valuesNode = processEngineConfiguration.getObjectMapper().createArrayNode(); } else { if (doesElementPropertyExist(id, USER_TASK_CANDIDATE_GROUPS, infoNode)) { valuesNode = (ArrayNode) infoNode.get(BPMN_NODE).get(id).get(USER_TASK_CANDIDATE_GROUPS); } if (valuesNode == null || valuesNode.isNull()) { valuesNode = processEngineConfiguration.getObjectMapper().createArrayNode(); } } valuesNode.add(candidateGroup); setElementProperty(id, USER_TASK_CANDIDATE_GROUPS, valuesNode, infoNode); }
@Test public void serializeShapesPoints_resultJsonHasShapes() throws IOException { //when points.setShape(Arrays.asList(ShapeType.CIRCLE, ShapeType.CROSS)); pointsSerializer.serialize(points, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("shaps")).isTrue(); ArrayNode arrayNode = (ArrayNode) actualObj.get("shaps"); Assertions.assertThat(arrayNode.get(1).asText()).isEqualTo("CROSS"); }
ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(json); ArrayNode slaidsNode = (ArrayNode) rootNode.get("SLA"); Iterator<JsonNode> slaidsIterator = slaidsNode.elements(); while (slaidsIterator.hasNext()) { JsonNode slaidNode = slaidsIterator.next(); System.out.println(slaidNode.get("slaid")); System.out.println(slaidNode.get("ragindicator")); System.out.println(slaidNode.get("name")); }
@Override public MatrixResponse route(GHMRequest ghRequest) { ObjectNode requestJson = factory.objectNode(); ArrayNode outArrayListJson = factory.arrayNode(); for (String str : outArraysList) { outArrayListJson.add(str); requestJson.putArray("points").addAll(createPointList(ghRequest.getFromPoints())); requestJson.putArray("point_hints").addAll(createStringList(ghRequest.getFromPointHints())); } else { ArrayNode fromPointList = createPointList(ghRequest.getFromPoints()); ArrayNode toPointList = createPointList(ghRequest.getToPoints()); requestJson.putArray("from_points").addAll(fromPointList); requestJson.putArray("from_point_hints").addAll(createStringList(ghRequest.getFromPointHints())); requestJson.putArray("to_points").addAll(toPointList); requestJson.putArray("to_point_hints").addAll(createStringList(ghRequest.getToPointHints())); requestJson.putArray("out_arrays").addAll(outArrayListJson); requestJson.put("vehicle", ghRequest.getVehicle()); requestJson.put("elevation", hasElevation); if (responseJson.has("message")) { matrixResponse.addErrors(readErrors(responseJson)); return matrixResponse; if (!responseJson.has("job_id")) { throw new IllegalStateException("Response should contain job_id but was " + postResponseStr + ", json:" + requestJson + ",url:" + postUrl);
@Override public JsonNode digest(final JsonNode schema) { // TODO: return an array directly (same for "required" in v4) final ObjectNode ret = FACTORY.objectNode(); final ArrayNode required = FACTORY.arrayNode(); ret.put("required", required); final JsonNode node = schema.get(keyword); final List<String> list = Lists.newArrayList(node.fieldNames()); Collections.sort(list); for (final String field: list) if (node.get(field).path("required").asBoolean(false)) required.add(field); return ret; } }
@Override public JsonNode digest(final JsonNode schema) final ObjectNode ret = FACTORY.objectNode(); final ArrayNode simpleTypes = FACTORY.arrayNode(); ret.put(keyword, simpleTypes); final ArrayNode schemas = FACTORY.arrayNode(); ret.put("schemas", schemas); final JsonNode node = schema.get(keyword); if (node.isTextual()) // Single type putType(set, node.textValue()); else { // More than one type, and possibly schemas final int size = node.size(); putType(set, element.textValue()); else schemas.add(index); schemas.removeAll(); simpleTypes.add(type.toString());
public String toJSON() { ArrayNode array = JACKSON_MAPPER.createArrayNode(); for (RequestAttempt attempt : this) { array.add(attempt.toJsonNode()); } try { return JACKSON_MAPPER.writeValueAsString(array); } catch (JsonProcessingException e) { throw new RuntimeException("Error serializing RequestAttempts!", e); } }
@Override public JsonNode digest(final JsonNode schema) final ObjectNode ret = FACTORY.objectNode(); final ObjectNode propertyDeps = FACTORY.objectNode(); ret.put("propertyDeps", propertyDeps); final ArrayNode schemaDeps = FACTORY.arrayNode(); ret.put("schemaDeps", schemaDeps); = JacksonUtils.asMap(schema.get(keyword)); case ARRAY: final JsonNode node = sortedSet(value); if (node.size() != 0) propertyDeps.put(key, node); break; case STRING: propertyDeps.put(key, FACTORY.arrayNode() .add(value.textValue())); schemaDeps.add(s);
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ArrayNode allowedTypes = FACTORY.arrayNode(); ret.put(keyword, allowedTypes); final JsonNode node = schema.get(keyword); final EnumSet<NodeType> typeSet = EnumSet.noneOf(NodeType.class); if (node.isTextual()) // Single type typeSet.add(NodeType.fromName(node.textValue())); else // More than one type for (final JsonNode element: node) typeSet.add(NodeType.fromName(element.textValue())); if (typeSet.contains(NodeType.NUMBER)) typeSet.add(NodeType.INTEGER); /* * Note that as this is an enumset, order is guaranteed */ for (final NodeType type: typeSet) allowedTypes.add(type.toString()); return ret; } }
@Override protected JsonNode generateSchema() { final ArrayNode schemas = FACTORY.arrayNode(); schemas.add(sub1); schemas.add(sub2); final ObjectNode ret = FACTORY.objectNode(); ret.put(keyword, schemas); return ret; }
BpmnModel bpmnModel) { if (objectNode.get(EDITOR_CHILD_SHAPES) != null) { for (JsonNode jsonChildNode : objectNode.get(EDITOR_CHILD_SHAPES)) { JsonNode boundsNode = jsonChildNode.get(EDITOR_BOUNDS); ObjectNode upperLeftNode = (ObjectNode) boundsNode.get(EDITOR_BOUNDS_UPPER_LEFT); graphicInfo.setX(upperLeftNode.get(EDITOR_BOUNDS_X).asDouble() + parentX); graphicInfo.setY(upperLeftNode.get(EDITOR_BOUNDS_Y).asDouble() + parentY); ObjectNode lowerRightNode = (ObjectNode) boundsNode.get(EDITOR_BOUNDS_LOWER_RIGHT); graphicInfo.setWidth(lowerRightNode.get(EDITOR_BOUNDS_X).asDouble() - graphicInfo.getX() + parentX); graphicInfo.setHeight(lowerRightNode.get(EDITOR_BOUNDS_Y).asDouble() - graphicInfo.getY() + parentY); ArrayNode outgoingNode = (ArrayNode) jsonChildNode.get("outgoing"); if (outgoingNode != null && outgoingNode.size() > 0) { for (JsonNode outgoingChildNode : outgoingNode) { JsonNode resourceNode = outgoingChildNode.get(EDITOR_SHAPE_ID);
private void collectVarargsFromNode(JsonNode node) { if (node.isArray()) { ArrayNode arrayNode = ArrayNode.class.cast(node); for (int i = 0; i < node.size(); i++) { addArgument(arrayNode.get(i)); } } if (node.isObject()) { ObjectNode objectNode = ObjectNode.class.cast(node); Iterator<Map.Entry<String,JsonNode>> items = objectNode.fields(); while (items.hasNext()) { Map.Entry<String,JsonNode> item = items.next(); JsonNode name = JsonNodeFactory.instance.objectNode().put(item.getKey(),item.getKey()); addArgument(name.get(item.getKey())); addArgument(item.getValue()); } } }
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ObjectNode propertyDeps = FACTORY.objectNode(); ret.put("propertyDeps", propertyDeps); final ArrayNode schemaDeps = FACTORY.arrayNode(); ret.put("schemaDeps", schemaDeps); final List<String> list = Lists.newArrayList(); final Map<String, JsonNode> map = JacksonUtils.asMap(schema.get(keyword)); String key; JsonNode value; for (final Map.Entry<String, JsonNode> entry: map.entrySet()) { key = entry.getKey(); value = entry.getValue(); if (value.isObject()) // schema dep list.add(key); else // property dep propertyDeps.put(key, sortedSet(value)); } for (final String s: Ordering.natural().sortedCopy(list)) schemaDeps.add(s); return ret; }
if (valueNode == null || !valueNode.isContainerNode()) { return valueNode; JsonNode substituteValue = valueNode; if (shapeSub.getEmitFromMember() != null) { substituteValue = valueNode.get(shapeSub.getEmitFromMember()); if (substituteValue == null) { System.err.println(String.format("Warning: Substituting shape '%s' for its" + " member '%s' as shape '%s' produced null value. Original" + " value: %s", shapeName, shapeSub.getEmitFromMember(), substituteShapeName, valueNode.toString())); ObjectNode obj = MAPPER.createObjectNode(); obj.set(memberName, applyCustomizationsToShapeJson(memberShapeName, memberShape, memberValue)); ArrayNode list = MAPPER.createArrayNode(); list.add(applyCustomizationsToShapeJson(memberShapeName, memberShape, e));
@Test public void serializeGraphicsListCategoryPlot_resultJsonHasGraphicsList() throws IOException { //when CategoryPlot categoryPlot = new CategoryPlot(); categoryPlot.add(Arrays.asList(new CategoryBars(), new CategoryPoints())); categoryPlotSerializer.serialize(categoryPlot, jgen, new DefaultSerializerProvider.Impl()); jgen.flush(); //then JsonNode actualObj = mapper.readTree(sw.toString()); Assertions.assertThat(actualObj.has("graphics_list")).isTrue(); ArrayNode arrayNode = (ArrayNode) actualObj.get("graphics_list"); Assertions.assertThat(arrayNode.size()).isEqualTo(2); }
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); ret.put("hasAdditional", schema.path("additionalProperties").isObject()); Set<String> set; ArrayNode node; node = FACTORY.arrayNode(); ret.put("properties", node); set = Sets.newHashSet(schema.path("properties").fieldNames()); for (final String field: Ordering.natural().sortedCopy(set)) node.add(field); node = FACTORY.arrayNode(); ret.put("patternProperties", node); set = Sets.newHashSet(schema.path("patternProperties").fieldNames()); for (final String field: Ordering.natural().sortedCopy(set)) node.add(field); return ret; } }