List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory arrayNode
public ArrayNode arrayNode()
From source file:org.kiji.rest.TestKijiRestEntityId.java
@Test public void testShouldCreateListsOfEntityIds() throws Exception { final TableLayoutDesc desc = KijiTableLayouts.getLayout("org/kiji/rest/layouts/rkf_hashprefixed.json"); final KijiTableLayout layout = KijiTableLayout.newLayout(desc); final EntityIdFactory factory = EntityIdFactory.getFactory(layout); final byte[] rowKey = Bytes.toBytes(UNUSUAL_STRING_EID); final EntityId originalEid = factory.getEntityIdFromHBaseRowKey(rowKey); // test the creation of entity ids from raw hbase rowkey final KijiRestEntityId restEid1 = KijiRestEntityId.createFromUrl( String.format("hbase_hex=%s", new String(Hex.encodeHex(originalEid.getHBaseRowKey()))), layout); final KijiRestEntityId restEid2 = KijiRestEntityId.createFromUrl( String.format("hbase=%s", Bytes.toStringBinary(originalEid.getHBaseRowKey())), layout); final JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true); final JsonNode hbaseHexStringNode = jsonNodeFactory .textNode(String.format("hbase_hex=%s", new String(Hex.encodeHex(originalEid.getHBaseRowKey())))); final JsonNode hbaseBinaryStringNode = jsonNodeFactory .textNode(String.format("hbase_hex=%s", new String(Hex.encodeHex(originalEid.getHBaseRowKey())))); ArrayNode hbaseListNode = jsonNodeFactory.arrayNode(); hbaseListNode.add(hbaseHexStringNode); hbaseListNode.add(hbaseBinaryStringNode); final List<KijiRestEntityId> restEidList1 = KijiRestEntityId.createListFromUrl(hbaseListNode.toString(), layout);//from w ww . ja va 2 s. co m assertEquals(restEid1.resolve(layout), restEidList1.get(0).resolve(layout)); assertEquals(restEid2.resolve(layout), restEidList1.get(1).resolve(layout)); // test the creation of entity ids from various json strings final KijiRestEntityId restEid3 = KijiRestEntityId.createFromUrl("[\"Hello\",\"World\"]", layout); final List<KijiRestEntityId> restEidList3 = KijiRestEntityId.createListFromUrl("[[\"Hello\",\"World\"]]", layout); final KijiRestEntityId restEid4 = KijiRestEntityId.createFromUrl("[[],\"World\"]", layout); final List<KijiRestEntityId> restEidList4 = KijiRestEntityId .createListFromUrl("[[[],\"World\"],[\"Hello\",\"World\"]]", layout); assertEquals(restEid3.resolve(layout), restEidList3.get(0).resolve(layout)); assertEquals(restEid4.getStringEntityId(), restEidList4.get(0).getStringEntityId()); assertEquals(1, restEidList3.size()); assertEquals(2, restEidList4.size()); }
From source file:com.basho.riak.client.core.operations.MapReduceOperation.java
@Override protected Response convert(List<RiakKvPB.RpbMapRedResp> rawResponse) { // Riak streams the result back. Each message from Riak contains a int // that tells you what phase the result is from. The result from a phase // can span multiple messages. Each result chunk is a JSON array. final JsonNodeFactory factory = JsonNodeFactory.instance; final ObjectMapper mapper = new ObjectMapper(); final Map<Integer, ArrayNode> resultMap = new LinkedHashMap<Integer, ArrayNode>(); int phase = 0; for (RiakKvPB.RpbMapRedResp response : rawResponse) { if (response.hasPhase()) { phase = response.getPhase(); }/* w w w . ja v a 2 s . c o m*/ if (response.hasResponse()) { ArrayNode jsonArray; if (resultMap.containsKey(phase)) { jsonArray = resultMap.get(phase); } else { jsonArray = factory.arrayNode(); resultMap.put(phase, jsonArray); } JsonNode responseJson; try { responseJson = mapper.readTree(response.getResponse().toStringUtf8()); } catch (IOException ex) { logger.error("Mapreduce job returned non-JSON; {}", response.getResponse().toStringUtf8()); throw new RuntimeException("Non-JSON response from MR job", ex); } if (responseJson.isArray()) { jsonArray.addAll((ArrayNode) responseJson); } else { logger.error("Mapreduce job returned JSON that wasn't an array; {}", response.getResponse().toStringUtf8()); } } } return new Response(resultMap); }
From source file:org.kitesdk.apps.scheduled.Schedule.java
private TreeNode toJson() { JsonNodeFactory js = JsonNodeFactory.instance; ObjectNode root = js.objectNode();/* w w w . ja va 2 s . c o m*/ root.put(NAME, getName()); root.put(JOBCLASS, getJobClass().getName()); root.put(START_TIME, formatter.print(getStartTime())); root.put(FREQUENCY, getFrequency()); ArrayNode views = js.arrayNode(); for (ViewTemplate template : getViewTemplates().values()) { ObjectNode viewNode = views.addObject(); viewNode.put(NAME, template.getName()); viewNode.put(URI, template.getUriTemplate()); viewNode.put(FREQUENCY, template.getFrequency()); viewNode.put(TYPE, template.getInputType().getName()); viewNode.put(IS_INPUT, template.isInput); } root.put(VIEWS, views); return root; }
From source file:net.mostlyharmless.jghservice.connector.jira.CreateIssue.java
@Override public String getJson() throws JsonProcessingException { JsonNodeFactory factory = JsonNodeFactory.instance; ObjectNode newNode = factory.objectNode(); ObjectNode fields = factory.objectNode(); if (projectKey != null) { ObjectNode project = factory.objectNode(); project.put("key", projectKey); fields.put("project", project); }//from w w w . j av a 2 s . co m if (issuetype != null) { ObjectNode iType = factory.objectNode(); iType.put("name", issuetype); fields.put("issuetype", iType); } if (summary != null) { fields.put("summary", summary); } if (description != null) { fields.put("description", description); } if (!fixVersions.isEmpty()) { ArrayNode array = factory.arrayNode(); for (String version : fixVersions) { ObjectNode node = factory.objectNode(); node.put("name", version); array.add(node); } fields.put("fixVersions", array); } if (!affectsVersions.isEmpty()) { ArrayNode array = factory.arrayNode(); for (String version : affectsVersions) { ObjectNode node = factory.objectNode(); node.put("name", version); array.add(node); } fields.put("versions", array); } if (assignee != null) { ObjectNode node = factory.objectNode(); if (assignee.isEmpty()) { node.put("name", factory.nullNode()); } else { node.put("name", assignee); } fields.put("assignee", node); } for (Map.Entry<String, JsonNode> entry : customFields.entrySet()) { fields.put(entry.getKey(), entry.getValue()); } newNode.put("fields", fields); return new ObjectMapper().writeValueAsString(newNode); }
From source file:org.kiji.rest.TestRowsResource.java
protected final String createJsonArray(String... components) throws IOException { final JsonNodeFactory factory = new JsonNodeFactory(true); ArrayNode arrayNode = factory.arrayNode(); for (String component : components) { JsonNode node;/*from ww w . java 2 s .co m*/ if (component.startsWith(KijiRestEntityId.HBASE_ROW_KEY_PREFIX) || component.startsWith(KijiRestEntityId.HBASE_HEX_ROW_KEY_PREFIX)) { node = factory.textNode(component); } else { node = stringToJsonNode(component); } arrayNode.add(node); } return arrayNode.toString(); }
From source file:me.tfeng.toolbox.avro.AvroHelper.java
private static JsonNode convertToSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory); if (!value.isNull()) { newNode.set(fieldName, value); }/* w w w . ja va 2s.c om*/ } } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory); if (value.isNull()) { newNode.set(entry.getKey(), value); } } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.set(entry.getKey(), convertToSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { return convertToSimpleRecord(type, json.get(type.getFullName()), factory); } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.play.avro.AvroHelper.java
private static JsonNode convertToSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { JsonNode value = convertToSimpleRecord(field.schema(), node.get(fieldName), factory); if (!value.isNull()) { newNode.put(fieldName, value); }/*from ww w . j a va 2 s .c om*/ } } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); JsonNode value = convertToSimpleRecord(valueType, entry.getValue(), factory); if (value.isNull()) { newNode.put(entry.getKey(), value); } } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = AvroHelper.getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.put(entry.getKey(), convertToSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { return convertToSimpleRecord(type, json.get(type.getFullName()), factory); } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertToSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.toolbox.avro.AvroHelper.java
private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { newNode.set(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory)); } else if (field.defaultValue() != null) { newNode.set(fieldName, MAPPER.readTree(field.defaultValue().toString())); } else { newNode.set(fieldName, factory.nullNode()); }/*ww w .j av a2 s .co m*/ } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); newNode.set(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory)); } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.set(entry.getKey(), convertFromSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { ObjectNode newNode = factory.objectNode(); newNode.set(type.getFullName(), convertFromSimpleRecord(type, json, factory)); return newNode; } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
From source file:me.tfeng.play.avro.AvroHelper.java
private static JsonNode convertFromSimpleRecord(Schema schema, JsonNode json, JsonNodeFactory factory) throws IOException { if (json.isObject() && schema.getType() == Type.RECORD) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); for (Field field : schema.getFields()) { String fieldName = field.name(); if (node.has(fieldName)) { newNode.put(fieldName, convertFromSimpleRecord(field.schema(), node.get(fieldName), factory)); } else if (field.defaultValue() != null) { newNode.put(fieldName, Json.parse(field.defaultValue().toString())); } else { newNode.put(fieldName, factory.nullNode()); }// ww w . j av a 2 s .c o m } return newNode; } else if (json.isObject() && schema.getType() == Type.MAP) { ObjectNode node = (ObjectNode) json; ObjectNode newNode = factory.objectNode(); Schema valueType = schema.getValueType(); Iterator<Entry<String, JsonNode>> entries = node.fields(); while (entries.hasNext()) { Entry<String, JsonNode> entry = entries.next(); newNode.put(entry.getKey(), convertFromSimpleRecord(valueType, entry.getValue(), factory)); } return newNode; } else if (schema.getType() == Type.UNION) { Schema type = AvroHelper.getSimpleUnionType(schema); if (type == null) { if (json.isNull()) { return json; } else { ObjectNode node = (ObjectNode) json; Entry<String, JsonNode> entry = node.fields().next(); for (Schema unionType : schema.getTypes()) { if (unionType.getFullName().equals(entry.getKey())) { ObjectNode newNode = factory.objectNode(); newNode.put(entry.getKey(), convertFromSimpleRecord(unionType, entry.getValue(), factory)); return newNode; } } throw new IOException("Unable to get schema for type " + entry.getKey() + " in union"); } } else if (json.isNull()) { return json; } else { ObjectNode newNode = factory.objectNode(); newNode.put(type.getFullName(), convertFromSimpleRecord(type, json, factory)); return newNode; } } else if (json.isArray() && schema.getType() == Type.ARRAY) { ArrayNode node = (ArrayNode) json; ArrayNode newNode = factory.arrayNode(); Iterator<JsonNode> iterator = node.elements(); while (iterator.hasNext()) { newNode.add(convertFromSimpleRecord(schema.getElementType(), iterator.next(), factory)); } return newNode; } else { return json; } }
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); }//from w w w .j a v a 2 s. c o m 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); }