org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens. 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: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeEdgeProperty() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode propertyAsJson = converted.get(0);
    assertNotNull(propertyAsJson);

    assertEquals(123, propertyAsJson.get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
}
 
Example #2
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithComplexAcceptHeaderDefaultResultToJson() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "*.*;q=0.8,application/xhtml");
    httppost.addHeader("Accept", "*/*");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"1-1\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(0, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).asInt());
    }
}
 
Example #3
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertex() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper();
    final Vertex v = graph.vertices(convertToVertexId("marko")).next();
    final String json = mapper.writeValueAsString(v);
    final Map<String, Object> m = mapper.readValue(json, mapTypeReference);

    assertEquals(GraphSONTokens.VERTEX, m.get(GraphSONTokens.TYPE));
    assertEquals(v.label(), m.get(GraphSONTokens.LABEL));
    assertNotNull(m.get(GraphSONTokens.ID));
    final Map<String,List<Map<String,Object>>> properties = (Map<String,List<Map<String,Object>>>) m.get(GraphSONTokens.PROPERTIES);
    assertEquals(v.value("name").toString(), properties.get("name").get(0).get(GraphSONTokens.VALUE).toString());
    assertEquals((Integer) v.value("age"), properties.get("age").get(0).get(GraphSONTokens.VALUE));
    assertEquals(1, properties.get("name").size());
    assertEquals(1, properties.get("age").size());
    assertEquals(2, properties.size());
}
 
Example #4
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithAuthorizationHeaderExplicitHandlerSetting() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.addHeader("Authorization", "Basic " + encoder.encodeToString("stephen:password".getBytes()));
    httppost.setEntity(new StringEntity("{\"gremlin\":\"1-1\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(0, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).intValue());
    }
}
 
Example #5
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithAuthorizationHeader() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.addHeader("Authorization", "Basic " + encoder.encodeToString("stephen:password".getBytes()));
    httppost.setEntity(new StringEntity("{\"gremlin\":\"1-1\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(0, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).intValue());
    }
}
 
Example #6
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithAnyAcceptHeaderDefaultResultToJson() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.addHeader("Accept", "*/*");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"1-1\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(0, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).asInt());
    }
}
 
Example #7
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyAndMapBindings() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"x\", \"bindings\":{\"x\":{\"y\":1}}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals("g:Map", node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get("@type").asText());
        assertEquals(1, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).get(1).get(GraphSONTokens.VALUEPROP).asInt());
    }
}
 
Example #8
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeDeserialize() throws Exception {
    final GryoMapper mapper = builder.get().create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        final Map<String,Object> props = new HashMap<>();
        final List<Map<String, Object>> propertyNames = new ArrayList<>(1);
        final Map<String,Object> propertyName = new HashMap<>();
        propertyName.put(GraphSONTokens.ID, "x");
        propertyName.put(GraphSONTokens.KEY, "x");
        propertyName.put(GraphSONTokens.VALUE, "no-way-this-will-ever-work");
        propertyNames.add(propertyName);
        props.put("x", propertyNames);
        final DetachedVertex v = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props);

        kryo.writeClassAndObject(out, v);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final DetachedVertex readX = (DetachedVertex) kryo.readClassAndObject(input);
            assertEquals("no-way-this-will-ever-work", readX.value("x"));
        }
    }
}
 
Example #9
Source File: StarGraphGraphSONDeserializer.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * A helper function for reading a serialized {@link StarGraph} from a {@code Map} generated by
 * {@link StarGraphGraphSONSerializerV1d0}.
 */
public static StarGraph readStarGraphVertex(final Map<String, Object> vertexData) throws IOException {
    final StarGraph starGraph = StarGraph.open();
    starGraph.addVertex(T.id, vertexData.get(GraphSONTokens.ID), T.label, vertexData.get(GraphSONTokens.LABEL));
    if (vertexData.containsKey(GraphSONTokens.PROPERTIES)) {
        final Map<String, List<Map<String, Object>>> properties = (Map<String, List<Map<String, Object>>>) vertexData.get(GraphSONTokens.PROPERTIES);
        for (Map.Entry<String, List<Map<String, Object>>> property : properties.entrySet()) {
            for (Map<String, Object> p : property.getValue()) {
                final StarGraph.StarVertexProperty vp = (StarGraph.StarVertexProperty) starGraph.getStarVertex().property(VertexProperty.Cardinality.list, property.getKey(), p.get(GraphSONTokens.VALUE), T.id, p.get(GraphSONTokens.ID));
                if (p.containsKey(GraphSONTokens.PROPERTIES)) {
                    final Map<String, Object> edgePropertyData = (Map<String, Object>) p.get(GraphSONTokens.PROPERTIES);
                    for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
                        vp.property(epd.getKey(), epd.getValue());
                    }
                }
            }
        }
    }

    return starGraph;
}
 
Example #10
Source File: StarGraphGraphSONDeserializer.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * A helper function for reading vertex edges from a serialized {@link StarGraph} (i.e. a {@code Map}) generated by
 * {@link StarGraphGraphSONSerializerV1d0}.
 */
public static void readStarGraphEdges(final Function<Attachable<Edge>, Edge> edgeMaker,
                                      final StarGraph starGraph,
                                      final Map<String, Object> vertexData,
                                      final String direction) throws IOException {
    final Map<String, List<Map<String,Object>>> edgeDatas = (Map<String, List<Map<String,Object>>>) vertexData.get(direction);
    for (Map.Entry<String, List<Map<String,Object>>> edgeData : edgeDatas.entrySet()) {
        for (Map<String,Object> inner : edgeData.getValue()) {
            final StarGraph.StarEdge starEdge;
            if (direction.equals(GraphSONTokens.OUT_E))
                starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addOutEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.IN)), T.id, inner.get(GraphSONTokens.ID));
            else
                starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addInEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.OUT)), T.id, inner.get(GraphSONTokens.ID));

            if (inner.containsKey(GraphSONTokens.PROPERTIES)) {
                final Map<String, Object> edgePropertyData = (Map<String, Object>) inner.get(GraphSONTokens.PROPERTIES);
                for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
                    starEdge.property(epd.getKey(), epd.getValue());
                }
            }

            if (edgeMaker != null) edgeMaker.apply(starEdge);
        }
    }
}
 
Example #11
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyForJavaTime() throws Exception {
    // basic test of java.time.* serialization over JSON from the server perspective. more complete tests
    // exist in gremlin-core
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"java.time.Instant.MAX\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(Instant.MAX, Instant.parse(node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).asText()));
    }
}
 
Example #12
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeToJsonMapWithElementForKey() throws Exception {
    final TinkerGraph graph = TinkerFactory.createClassic();
    final GraphTraversalSource g = graph.traversal();
    final Map<Vertex, Integer> map = new HashMap<>();
    map.put(g.V().has("name", "marko").next(), 1000);

    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(map).create());
    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);

    // with no embedded types the key (which is a vertex) simply serializes out to an id
    // {"result":{"1":1000},"code":200,"requestId":"2d62161b-9544-4f39-af44-62ec49f9a595","type":0}
    assertEquals(1000, converted.get("1").get(GraphSONTokens.VALUEPROP).asInt());
}
 
Example #13
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTTransactionalGraphInStrictMode() throws Exception {
    assumeNeo4jIsPresent();

    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"g1.addV()\",\"aliases\":{\"g1\":\"g\"}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(1, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).size());
    }
}
 
Example #14
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeTraversalMetrics() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper();
    final TraversalMetrics tm = g.V().both().profile().next();
    final String json = mapper.writeValueAsString(tm);
    final Map<String, Object> m = mapper.readValue(json, mapTypeReference);

    assertTrue(m.containsKey(GraphSONTokens.DURATION));
    assertTrue(m.containsKey(GraphSONTokens.METRICS));

    final List<Map<String, Object>> metrics = (List<Map<String, Object>>) m.get(GraphSONTokens.METRICS);
    assertEquals(tm.getMetrics().size(), metrics.size());

    final Map<String, Object> metrics0 = metrics.get(0);
    assertTrue(metrics0.containsKey(GraphSONTokens.ID));
    assertTrue(metrics0.containsKey(GraphSONTokens.NAME));
    assertTrue(metrics0.containsKey(GraphSONTokens.COUNTS));
    assertTrue(metrics0.containsKey(GraphSONTokens.DURATION));
}
 
Example #15
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyAndArrayBindings() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"x\", \"bindings\":{\"x\":[1,2,3]}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(true, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).isArray());
        assertEquals(1, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).intValue());
        assertEquals(2, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(1).get(GraphSONTokens.VALUEPROP).intValue());
        assertEquals(3, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(2).get(GraphSONTokens.VALUEPROP).intValue());
    }
}
 
Example #16
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyWithTinkerGraphResult() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory.createModern()\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode resultJson = mapper.readTree(json);
        final JsonNode data = resultJson.get("result").get("data");
        assertEquals(1, data.get(GraphSONTokens.VALUEPROP).size());

        assertEquals(6, data.get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VERTICES).size());
        assertEquals(6, data.get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.EDGES).size());
    }
}
 
Example #17
Source File: TinkerIoRegistryV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
}
 
Example #18
Source File: TinkerIoRegistryV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
}
 
Example #19
Source File: AbstractGraphSONMessageSerializerV1d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public void ser(final ResponseMessage responseMessage, final JsonGenerator jsonGenerator,
                final SerializerProvider serializerProvider,
                final TypeSerializer typeSerializer) throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();
    if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());

    jsonGenerator.writeStringField(SerTokens.TOKEN_REQUEST, responseMessage.getRequestId() != null ? responseMessage.getRequestId().toString() : null);
    jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_STATUS);

    if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
    jsonGenerator.writeStringField(SerTokens.TOKEN_MESSAGE, responseMessage.getStatus().getMessage());
    jsonGenerator.writeNumberField(SerTokens.TOKEN_CODE, responseMessage.getStatus().getCode().getValue());
    jsonGenerator.writeObjectField(SerTokens.TOKEN_ATTRIBUTES, responseMessage.getStatus().getAttributes());
    jsonGenerator.writeEndObject();

    jsonGenerator.writeObjectFieldStart(SerTokens.TOKEN_RESULT);
    if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName());
    if (null == responseMessage.getResult().getData())
        jsonGenerator.writeNullField(SerTokens.TOKEN_DATA);
    else
        GraphSONUtil.writeWithType(SerTokens.TOKEN_DATA, responseMessage.getResult().getData(), jsonGenerator, serializerProvider, typeSerializer);
    jsonGenerator.writeObjectField(SerTokens.TOKEN_META, responseMessage.getResult().getMeta());
    jsonGenerator.writeEndObject();

    jsonGenerator.writeEndObject();
}
 
Example #20
Source File: CustomTest.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Ignore
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeTraversalMetrics() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build()).mapper().create().createMapper();
    final TraversalMetrics tm = (TraversalMetrics) g.V().both().profile().next();
    final String json = mapper.writeValueAsString(tm);
    final Map<String, Object> m = mapper.readValue(json, mapTypeReference);

    assertTrue(m.containsKey(GraphSONTokens.DURATION));
    assertTrue(m.containsKey(GraphSONTokens.METRICS));

    final List<Map<String, Object>> metrics = (List<Map<String, Object>>) m.get(GraphSONTokens.METRICS);
    assertEquals(2, metrics.size());

    final Map<String, Object> metrics0 = metrics.get(0);
    assertTrue(metrics0.containsKey(GraphSONTokens.ID));
    assertTrue(metrics0.containsKey(GraphSONTokens.NAME));
    assertTrue(metrics0.containsKey(GraphSONTokens.COUNTS));
    assertTrue(metrics0.containsKey(GraphSONTokens.DURATION));
}
 
Example #21
Source File: TinkerIoRegistryV2d0.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
}
 
Example #22
Source File: TinkerIoRegistryV3d0.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
}
 
Example #23
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.CREW)
public void shouldSerializeVertexPropertyWithProperties() throws Exception {
    final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper();
    final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next();
    final String json = mapper.writeValueAsString(vp);
    final Map<String, Object> m = mapper.readValue(json, mapTypeReference);

    assertEquals(vp.label(), m.get(GraphSONTokens.LABEL));
    assertNotNull(m.get(GraphSONTokens.ID));
    assertEquals(vp.value(), m.get(GraphSONTokens.VALUE));
    assertEquals(vp.values("startTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("startTime"));
    assertEquals(vp.values("endTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("endTime"));
}
 
Example #24
Source File: CustomId.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(final CustomId customId, final JsonGenerator jsonGenerator,
                              final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException, JsonProcessingException {
    // when the type is included add "class" as a key and then try to utilize as much of the
    // default serialization provided by jackson data-bind as possible.  for example, write
    // the uuid as an object so that when jackson serializes it, it uses the uuid serializer
    // to write it out with the type.  in this way, data-bind should be able to deserialize
    // it back when types are embedded.
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField(GraphSONTokens.CLASS, CustomId.class.getName());
    jsonGenerator.writeStringField("cluster", customId.getCluster());
    jsonGenerator.writeObjectField("elementId", customId.getElementId());
    jsonGenerator.writeEndObject();
}
 
Example #25
Source File: TinkerIoRegistryV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartObject();

    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();

    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
    }

    jsonGenerator.writeEndArray();

    jsonGenerator.writeEndObject();
}
 
Example #26
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyAndStringBindings() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"x\", \"bindings\":{\"x\":\"10\"}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals("10", node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).textValue());
    }
}
 
Example #27
Source File: TinkerIoRegistryV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void serializeWithType(final TinkerGraph graph, final JsonGenerator jsonGenerator,
                              final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeStringField(GraphSONTokens.CLASS, TinkerGraph.class.getName());

    jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    jsonGenerator.writeStartArray();
    jsonGenerator.writeString(ArrayList.class.getName());
    jsonGenerator.writeStartArray();

    final Iterator<Vertex> vertices = graph.vertices();
    while (vertices.hasNext()) {
        GraphSONUtil.writeWithType(vertices.next(), jsonGenerator, serializerProvider, typeSerializer);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndArray();

    jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    jsonGenerator.writeStartArray();
    jsonGenerator.writeString(ArrayList.class.getName());
    jsonGenerator.writeStartArray();

    final Iterator<Edge> edges = graph.edges();
    while (edges.hasNext()) {
        GraphSONUtil.writeWithType(edges.next(), jsonGenerator, serializerProvider, typeSerializer);
    }

    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndArray();

    jsonGenerator.writeEndObject();
}
 
Example #28
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyAndNullBindings() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"x\", \"bindings\":{\"x\":null}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(true, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).isNull());
    }
}
 
Example #29
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyAndBooleanBindings() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"x\", \"bindings\":{\"x\":true}}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(true, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).booleanValue());
    }
}
 
Example #30
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyWithIteratorResult() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"gclassic.V()\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(6, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).size());
    }
}