Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#createArrayNode()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#createArrayNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONDocumentTest.java    From java-client-api with Apache License 2.0 7 votes vote down vote up
private ObjectNode makeContent(ObjectMapper mapper) throws IOException {
  ObjectNode sourceNode = mapper.createObjectNode();
  sourceNode.put("stringKey", "string value");
  sourceNode.put("numberKey", 7);
  ObjectNode childNode = mapper.createObjectNode();
  childNode.put("childObjectKey", "child object value");
  sourceNode.set("objectKey", childNode);
  ArrayNode childArray = mapper.createArrayNode();
  childArray.add("item value");
  childArray.add(3);
  childNode = mapper.createObjectNode();
  childNode.put("itemObjectKey", "item object value");
  childArray.add(childNode);
  sourceNode.set("arrayKey", childArray);
  sourceNode.put("booleanKey", false);
  sourceNode.put("numberKey2", 1);
  sourceNode.put("nullKey", 0);

  return sourceNode;
}
 
Example 2
Source File: RawMetricResource.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private ArrayNode getHistoricDocuments(DBCollection dbCollection, DBObject query) {
	ObjectMapper mapper = new ObjectMapper();
	ArrayNode nodeArray = mapper.createArrayNode();		
	DBCursor cursor = dbCollection.find(query);
	while(cursor.hasNext()) {
		DBObject obj = cursor.next();
		JsonNode json;
		try {
			json = mapper.readTree(obj.toString());
			nodeArray.add(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return nodeArray;
}
 
Example 3
Source File: WampMessages.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(requestId);
    if (options != null)
        messageNode.add(options);
    else
        messageNode.add(mapper.createObjectNode());
    messageNode.add(topic);
    if (arguments != null)
        messageNode.add(arguments);
    else if (argumentsKw != null)
        messageNode.add(mapper.createArrayNode());
    if (argumentsKw != null)
        messageNode.add(argumentsKw);
    return messageNode;
}
 
Example 4
Source File: GraphManagerImpl.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
private JacksonHandle generatePermissions(GraphPermissions permissions) {
  ObjectMapper mapper = new ObjectMapper();
  ObjectNode payload = mapper.createObjectNode();
  ArrayNode permissionsNode = mapper.createArrayNode();
  payload.set("permissions", permissionsNode);
  for ( Map.Entry<String,Set<Capability>> entry : permissions.entrySet() ) {
    ObjectNode permissionNode = mapper.createObjectNode();
    permissionNode.put("role-name", entry.getKey());
    ArrayNode capabilitiesNode = mapper.createArrayNode();
    for ( Capability capability : entry.getValue() ) {
      capabilitiesNode.add(capability.toString().toLowerCase());
    }
    permissionNode.set("capabilities", capabilitiesNode);
    permissionsNode.add(permissionNode);
  }

  return new JacksonHandle(payload);
}
 
Example 5
Source File: ConnectedRESTQA.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
public static void includeElementFieldWithWeight(String dbName, String field_name, String namespace,
		String elementName, double weight, String attrNS_URI, String attr_localname, String attr_value)
		throws Exception {

	ObjectMapper mapper = new ObjectMapper();
	ObjectNode childNode = mapper.createObjectNode();
	ArrayNode arrNode = mapper.createArrayNode();
	ObjectNode childNodeObject = mapper.createObjectNode();
	childNodeObject.put("namespace-uri", namespace);
	childNodeObject.put("localname", elementName);
	childNodeObject.put("weight", weight);
	// These 3 are new fields that have been added as of 8.0.2 from
	// 03/20/2015
	// in the Management API.
	childNodeObject.put("attribute-namespace-uri", attrNS_URI);
	childNodeObject.put("attribute-localname", attr_localname);
	childNodeObject.put("attribute-value", attr_value);
	arrNode.add(childNodeObject);
	childNode.putArray("included-element").addAll(arrNode);
	setDatabaseFieldProperties(dbName, field_name, "included-element", childNode);
}
 
Example 6
Source File: TransactionsCommand.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Converts collection of transactions into a JSON object.
 *
 * @param transactionIds transaction identifiers
 */
private JsonNode json(Collection<TransactionId> transactionIds) {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode txns = mapper.createArrayNode();
    transactionIds.forEach(id -> txns.add(id.toString()));
    return txns;
}
 
Example 7
Source File: WampMessages.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(requestId);
    if (options != null)
        messageNode.add(options);
    else
        messageNode.add(mapper.createObjectNode());
    messageNode.add(topic);
    return messageNode;
}
 
Example 8
Source File: WampMessages.java    From jawampa with Apache License 2.0 5 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(requestId);
    messageNode.add(registrationId);
    return messageNode;
}
 
Example 9
Source File: AphelionServiceTest.java    From aphelion with Apache License 2.0 5 votes vote down vote up
@Test
public void getDatapointsTimeSeriesExceeding() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode arrayNode = mapper.createArrayNode();
    ObjectNode objectNode1 = mapper.createObjectNode();
    ObjectNode objectNode2 = mapper.createObjectNode();
    objectNode1.put("parameters", objectNode2);
    objectNode2.put("key", "gt=60");

    List<TimeSeriesPoint> result= aphelionService.getDatapointsTimeSeriesSampledata(objectNode1);

    Assert.assertEquals(1L, result.get(0).getDatapoints().get(0).get(0));
    Assert.assertEquals("ALL", result.get(0).getTarget());
}
 
Example 10
Source File: ConnectedRESTQA.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
public static void addRangeElementIndex(String dbName, String[][] rangeElements) throws Exception {
	ObjectMapper mapper = new ObjectMapper();
	ObjectNode mainNode = mapper.createObjectNode();

	ArrayNode childArray = mapper.createArrayNode();
	int nRowsLen = rangeElements.length;
	int j = 0;
	for (int i = 0; i < nRowsLen; i++) {
		ObjectNode childNodeObject = mapper.createObjectNode();
		childNodeObject.put("scalar-type", rangeElements[i][j++]);
		childNodeObject.put("namespace-uri", rangeElements[i][j++]);
		childNodeObject.put("localname", rangeElements[i][j++]);
		childNodeObject.put("collation", rangeElements[i][j++]);
		if (rangeElements[i][j].equalsIgnoreCase("false"))
			childNodeObject.put("range-value-positions", false);
		else
			childNodeObject.put("range-value-positions", true);
		j++;
		childNodeObject.put("invalid-values", rangeElements[i][j++]);
		/*
		 * if new field elements are to be added, then: 1) Increment value
		 * of j 2) add them below here using
		 * childNodeObject.put("FIELD-NAME", rangeElements[i][j++]);
		 */
		childArray.add(childNodeObject);
		j = 0;
	}
	mainNode.putArray("range-element-index").addAll(childArray);
	setDatabaseProperties(dbName, "range-element-index", mainNode);
}
 
Example 11
Source File: VirtualFlowsListCommand.java    From onos with Apache License 2.0 5 votes vote down vote up
private ObjectNode json(ObjectMapper mapper,
                        Device device, List<FlowEntry> flows) {
    ObjectNode result = mapper.createObjectNode();
    ArrayNode array = mapper.createArrayNode();

    flows.forEach(flow -> array.add(jsonForEntity(flow, FlowEntry.class)));

    result.put("device", device.id().toString())
            .put("flowCount", flows.size())
            .set("flows", array);
    return result;
}
 
Example 12
Source File: SClass.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public ObjectNode toJson(ObjectMapper OBJECT_MAPPER) {
	ObjectNode result = OBJECT_MAPPER.createObjectNode();
	result.put("name", getName());
	result.put("simpleName", getSimpleName());
	result.put("simpleType", getSimpleType().name());
	ArrayNode fieldsJson = OBJECT_MAPPER.createArrayNode();
	for (SField field : ownFields.values()) {
		fieldsJson.add(field.toJson(OBJECT_MAPPER));
	}
	result.set("fields", fieldsJson);
	return result;
}
 
Example 13
Source File: OpenstackRouterListCommand.java    From onos with Apache License 2.0 5 votes vote down vote up
private String json(List<Router> routers) {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode result = mapper.createArrayNode();
    for (Router router: routers) {
        result.add(modelEntityToJson(router, NeutronRouter.class));
    }
    return prettyJson(mapper, result.toString());
}
 
Example 14
Source File: TraceTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
private JsonNode generateTracesFromTransactionTraceAndBlock(
    final ProtocolSchedule protocolSchedule,
    final TransactionTrace transactionTrace,
    final Block block) {
  final ObjectMapper mapper = new ObjectMapper();

  final ArrayNode resultArrayNode = mapper.createArrayNode();

  FlatTraceGenerator.generateFromTransactionTraceAndBlock(
          protocolSchedule, transactionTrace, block)
      .forEachOrdered(resultArrayNode::addPOJO);

  return resultArrayNode;
}
 
Example 15
Source File: WampMessages.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(requestId);
    messageNode.add(subscriptionId);
    return messageNode;
}
 
Example 16
Source File: TraceReplayBlockTransactions.java    From besu with Apache License 2.0 5 votes vote down vote up
private JsonNode generateTracesFromTransactionTrace(
    final List<TransactionTrace> transactionTraces,
    final Block block,
    final TraceTypeParameter traceTypeParameter) {
  final Set<TraceTypeParameter.TraceType> traceTypes = traceTypeParameter.getTraceTypes();
  final ObjectMapper mapper = new ObjectMapper();
  final ArrayNode resultArrayNode = mapper.createArrayNode();
  final AtomicInteger traceCounter = new AtomicInteger(0);
  transactionTraces.forEach(
      transactionTrace ->
          handleTransactionTrace(
              transactionTrace, block, traceTypes, mapper, resultArrayNode, traceCounter));
  return resultArrayNode;
}
 
Example 17
Source File: WampMessages.java    From jawampa with Apache License 2.0 5 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(requestId);
    messageNode.add(registrationId);
    return messageNode;
}
 
Example 18
Source File: JacksonHandleTest.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadWrite() {
  // create an identifier for the database document
  String docId = "/example/jackson-test.json";

  // create a manager for JSON documents
  JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();

  // construct a Jackson JSON structure
  ObjectMapper mapper = new ObjectMapper();
  ObjectNode childObj = mapper.createObjectNode();
  childObj.put("key", "value");
  ArrayNode childArray = mapper.createArrayNode();
  childArray.add("item1");
  childArray.add("item2");
  ObjectNode writeRoot = mapper.createObjectNode();
  writeRoot.put("object", childObj);
  writeRoot.put("array",  childArray);

  // create a handle for the JSON structure
  JacksonHandle writeHandle = new JacksonHandle(writeRoot);

  // write the document to the database
  docMgr.write(docId, writeHandle);

  // create a handle to receive the database content as a Jackson structure
  JacksonHandle readHandle = new JacksonHandle();

  // read the document content from the database as a Jackson structure
  docMgr.read(docId, readHandle);

  // access the document content
  JsonNode readRoot = readHandle.get();
  assertNotNull("Wrote null Jackson JSON structure", readRoot);
  assertTrue("Jackson JSON structures not equal",
    readRoot.equals(writeRoot));

  // delete the document
  docMgr.delete(docId);
}
 
Example 19
Source File: WampMessages.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public JsonNode toObjectArray(ObjectMapper mapper) throws WampError {
    ArrayNode messageNode = mapper.createArrayNode();
    messageNode.add(ID);
    messageNode.add(sessionId);
    if (details != null)
        messageNode.add(details);
    else
        messageNode.add(mapper.createObjectNode());
    return messageNode;
}
 
Example 20
Source File: JsonExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void exportWebFonts() throws IOException
{
	HtmlResourceHandler fontHandler = getExporterOutput().getFontHandler();
	ReportContext reportContext = getReportContext();

	if (
		fontHandler != null
		&& reportContext != null 
		&& reportContext.containsParameter(REPORT_CONTEXT_PARAMETER_WEB_FONTS)
		) 
	{
		Map<String, HtmlFontFamily> fontsToProcess = 
			(Map<String, HtmlFontFamily>)reportContext.getParameterValue(REPORT_CONTEXT_PARAMETER_WEB_FONTS);

		ObjectMapper mapper = new ObjectMapper();
		ArrayNode webFonts = mapper.createArrayNode();

		for (HtmlFontFamily htmlFontFamily : fontsToProcess.values())
		{
			ObjectNode objNode = mapper.createObjectNode();
			objNode.put("id", htmlFontFamily.getId());
			objNode.put("path", fontHandler.getResourcePath(htmlFontFamily.getId()));
			webFonts.add(objNode);
		}
		
		if (gotFirstJsonFragment)
		{
			writer.write(",\n");
		} else
		{
			gotFirstJsonFragment = true;
		}
		writer.write("\"webfonts_" + (webFonts.hashCode() & 0x7FFFFFFF) + "\": {");

		writer.write("\"id\": \"webfonts_" + (webFonts.hashCode() & 0x7FFFFFFF) + "\",");
		writer.write("\"type\": \"webfonts\",");
		writer.write("\"webfonts\": " + jacksonUtil.getJsonString(webFonts));

		writer.write("}");
	}
}