use of com.fasterxml.jackson.databind.node.ObjectNode in project flink by apache.
the class JsonRowSerializationSchema method serialize.
@Override
public byte[] serialize(Row row) {
if (row.getArity() != fieldNames.length) {
throw new IllegalStateException(String.format("Number of elements in the row %s is different from number of field names: %d", row, fieldNames.length));
}
ObjectNode objectNode = mapper.createObjectNode();
for (int i = 0; i < row.getArity(); i++) {
JsonNode node = mapper.valueToTree(row.getField(i));
objectNode.set(fieldNames[i], node);
}
try {
return mapper.writeValueAsBytes(objectNode);
} catch (Exception e) {
throw new RuntimeException("Failed to serialize row", e);
}
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project flink by apache.
the class JSONKeyValueDeserializationSchemaTest method testDeserializeWithMetadata.
@Test
public void testDeserializeWithMetadata() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectNode initialKey = mapper.createObjectNode();
initialKey.put("index", 4);
byte[] serializedKey = mapper.writeValueAsBytes(initialKey);
ObjectNode initialValue = mapper.createObjectNode();
initialValue.put("word", "world");
byte[] serializedValue = mapper.writeValueAsBytes(initialValue);
JSONKeyValueDeserializationSchema schema = new JSONKeyValueDeserializationSchema(true);
ObjectNode deserializedValue = schema.deserialize(serializedKey, serializedValue, "topic#1", 3, 4);
Assert.assertEquals(4, deserializedValue.get("key").get("index").asInt());
Assert.assertEquals("world", deserializedValue.get("value").get("word").asText());
Assert.assertEquals("topic#1", deserializedValue.get("metadata").get("topic").asText());
Assert.assertEquals(4, deserializedValue.get("metadata").get("offset").asInt());
Assert.assertEquals(3, deserializedValue.get("metadata").get("partition").asInt());
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project flink by apache.
the class JsonRowDeserializationSchemaTest method testMissingNode.
/**
* Tests deserialization with non-existing field name.
*/
@Test
public void testMissingNode() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
// Root
ObjectNode root = objectMapper.createObjectNode();
root.put("id", 123123123);
byte[] serializedJson = objectMapper.writeValueAsBytes(root);
JsonRowDeserializationSchema deserializationSchema = new JsonRowDeserializationSchema(new String[] { "name" }, new Class<?>[] { String.class });
Row row = deserializationSchema.deserialize(serializedJson);
assertEquals(1, row.getArity());
assertNull("Missing field not null", row.getField(0));
deserializationSchema.setFailOnMissingField(true);
try {
deserializationSchema.deserialize(serializedJson);
fail("Did not throw expected Exception");
} catch (IOException e) {
assertTrue(e.getCause() instanceof IllegalStateException);
}
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project orientdb by orientechnologies.
the class OGraphSONUtility method objectNodeFromElement.
/**
* Creates GraphSON for a single graph element.
*/
public ObjectNode objectNodeFromElement(final Element element) {
final boolean isEdge = element instanceof Edge;
final boolean showTypes = mode == GraphSONMode.EXTENDED;
final List<String> propertyKeys = isEdge ? this.edgePropertyKeys : this.vertexPropertyKeys;
final ElementPropertiesRule elementPropertyConfig = isEdge ? this.edgePropertiesRule : this.vertexPropertiesRule;
final ObjectNode jsonElement = createJSONMap(createPropertyMap(element, propertyKeys, elementPropertyConfig, normalized), propertyKeys, showTypes);
if ((isEdge && this.includeReservedEdgeId) || (!isEdge && this.includeReservedVertexId)) {
putObject(jsonElement, GraphSONTokens._ID, element.getId());
}
// are graph implementations that have Edge extend from Vertex
if (element instanceof Edge) {
final Edge edge = (Edge) element;
if (this.includeReservedEdgeId) {
putObject(jsonElement, GraphSONTokens._ID, element.getId());
}
if (this.includeReservedEdgeType) {
jsonElement.put(GraphSONTokens._TYPE, GraphSONTokens.EDGE);
}
if (this.includeReservedEdgeOutV) {
putObject(jsonElement, GraphSONTokens._OUT_V, edge.getVertex(Direction.OUT).getId());
}
if (this.includeReservedEdgeInV) {
putObject(jsonElement, GraphSONTokens._IN_V, edge.getVertex(Direction.IN).getId());
}
if (this.includeReservedEdgeLabel) {
jsonElement.put(GraphSONTokens._LABEL, edge.getLabel());
}
} else if (element instanceof Vertex) {
if (this.includeReservedVertexId) {
putObject(jsonElement, GraphSONTokens._ID, element.getId());
}
if (this.includeReservedVertexType) {
jsonElement.put(GraphSONTokens._TYPE, GraphSONTokens.VERTEX);
}
}
return jsonElement;
}
use of com.fasterxml.jackson.databind.node.ObjectNode in project orientdb by orientechnologies.
the class OGraphSONUtility method getValue.
private static Object getValue(Object value, final boolean includeType) {
Object returnValue = value;
// if the includeType is set to true then show the data types of the properties
if (includeType) {
// type will be one of: map, list, string, long, int, double, float.
// in the event of a complex object it will call a toString and store as a
// string
String type = determineType(value);
ObjectNode valueAndType = jsonNodeFactory.objectNode();
valueAndType.put(GraphSONTokens.TYPE, type);
if (type.equals(GraphSONTokens.TYPE_LIST)) {
// values of lists must be accumulated as ObjectNode objects under the value key.
// will return as a ArrayNode. called recursively to traverse the entire
// object graph of each item in the array.
ArrayNode list = (ArrayNode) value;
// there is a set of values that must be accumulated as an array under a key
ArrayNode valueArray = valueAndType.putArray(GraphSONTokens.VALUE);
for (int ix = 0; ix < list.size(); ix++) {
// the value of each item in the array is a node object from an ArrayNode...must
// get the value of it.
addObject(valueArray, getValue(getTypedValueFromJsonNode(list.get(ix)), includeType));
}
} else if (type.equals(GraphSONTokens.TYPE_MAP)) {
// maps are converted to a ObjectNode. called recursively to traverse
// the entire object graph within the map.
ObjectNode convertedMap = jsonNodeFactory.objectNode();
ObjectNode jsonObject = (ObjectNode) value;
Iterator keyIterator = jsonObject.fieldNames();
while (keyIterator.hasNext()) {
Object key = keyIterator.next();
// no need to getValue() here as this is already a ObjectNode and should have type info
convertedMap.put(key.toString(), jsonObject.get(key.toString()));
}
valueAndType.put(GraphSONTokens.VALUE, convertedMap);
} else {
// this must be a primitive value or a complex object. if a complex
// object it will be handled by a call to toString and stored as a
// string value
putObject(valueAndType, GraphSONTokens.VALUE, value);
}
// this goes back as a JSONObject with data type and value
returnValue = valueAndType;
}
return returnValue;
}
Aggregations