Search in sources :

Example 16 with ObjectNode

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 17 with ObjectNode

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());
}
Also used : JSONKeyValueDeserializationSchema(org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 18 with ObjectNode

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonRowDeserializationSchema(org.apache.flink.streaming.util.serialization.JsonRowDeserializationSchema) Row(org.apache.flink.types.Row) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 19 with ObjectNode

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;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ElementPropertiesRule(com.tinkerpop.blueprints.util.io.graphson.ElementPropertyConfig.ElementPropertiesRule) Edge(com.tinkerpop.blueprints.Edge)

Example 20 with ObjectNode

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JSONObject(org.codehaus.jettison.json.JSONObject) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)767 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)185 JsonNode (com.fasterxml.jackson.databind.JsonNode)165 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)165 Test (org.junit.Test)112 StringEntity (org.apache.http.entity.StringEntity)88 Deployment (org.activiti.engine.test.Deployment)84 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)67 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)64 Task (org.activiti.engine.task.Task)49 HttpPost (org.apache.http.client.methods.HttpPost)49 JCodeModel (com.sun.codemodel.JCodeModel)45 JPackage (com.sun.codemodel.JPackage)44 IOException (java.io.IOException)43 HttpPut (org.apache.http.client.methods.HttpPut)40 JType (com.sun.codemodel.JType)39 HashMap (java.util.HashMap)39 Cluster (org.apache.geode.tools.pulse.internal.data.Cluster)39 ArrayList (java.util.ArrayList)32 Map (java.util.Map)32