Search in sources :

Example 6 with JSONTokener

use of org.codehaus.jettison.json.JSONTokener in project blueprints by tinkerpop.

the class GraphSONUtilityTest method vertexFromJsonValid.

@Test
public void vertexFromJsonValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)), factory, GraphSONMode.NORMAL, null);
    Assert.assertSame(v, g.getVertex(1));
    // tinkergraph converts id to string
    Assert.assertEquals("1", v.getId());
    Assert.assertEquals("marko", v.getProperty("name"));
    Assert.assertEquals(29, v.getProperty("age"));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 7 with JSONTokener

use of org.codehaus.jettison.json.JSONTokener in project blueprints by tinkerpop.

the class RestHelper method post.

private static JSONObject post(final String uri, final String postData, final String contentType, final String accept, final boolean noResult) {
    try {
        final HttpURLConnection connection = createConnection(uri, contentType, accept);
        connection.setDoOutput(true);
        final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        // post data with Content-Length automatically set
        writer.write(postData);
        writer.close();
        if (noResult) {
            new InputStreamReader(connection.getInputStream()).close();
            return null;
        } else {
            return new JSONObject(new JSONTokener(convertStreamToString(connection.getInputStream())));
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JSONObject(org.codehaus.jettison.json.JSONObject) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 8 with JSONTokener

use of org.codehaus.jettison.json.JSONTokener in project blueprints by tinkerpop.

the class GraphSONUtilityTest method edgeFromJsonInputStreamCompactLabelOrIdOnEdge.

@Test
public void edgeFromJsonInputStreamCompactLabelOrIdOnEdge() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v1 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)), factory, GraphSONMode.COMPACT, null);
    Vertex v2 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)), factory, GraphSONMode.COMPACT, null);
    Edge e = GraphSONUtility.edgeFromJson(inputStreamEdgeJsonLight, v1, v2, factory, GraphSONMode.COMPACT, null);
    Assert.assertSame(v1, g.getVertex(1));
    Assert.assertSame(v2, g.getVertex(2));
    Assert.assertSame(e, g.getEdge(0));
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 9 with JSONTokener

use of org.codehaus.jettison.json.JSONTokener in project blueprints by tinkerpop.

the class GraphSONUtilityTest method edgeFromJsonIgnoreWeightValid.

@Test
public void edgeFromJsonIgnoreWeightValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v1 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)), factory, GraphSONMode.NORMAL, null);
    Vertex v2 = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)), factory, GraphSONMode.NORMAL, null);
    Set<String> ignoreWeight = new HashSet<String>();
    ignoreWeight.add("weight");
    ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(null, ignoreWeight);
    GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
    Edge e = utility.edgeFromJson(new JSONObject(new JSONTokener(edgeJson)), v1, v2);
    Assert.assertSame(v1, g.getVertex(1));
    Assert.assertSame(v2, g.getVertex(2));
    Assert.assertSame(e, g.getEdge(7));
    // tinkergraph converts id to string
    Assert.assertEquals("7", e.getId());
    Assert.assertNull(e.getProperty("weight"));
    Assert.assertEquals("knows", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) JSONTokener(org.codehaus.jettison.json.JSONTokener) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) JSONObject(org.codehaus.jettison.json.JSONObject) Edge(com.tinkerpop.blueprints.Edge) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with JSONTokener

use of org.codehaus.jettison.json.JSONTokener in project blueprints by tinkerpop.

the class RestHelper method get.

static JSONObject get(final String uri) {
    try {
        final URLConnection connection = createConnection(uri, null, RexsterTokens.APPLICATION_REXSTER_TYPED_JSON);
        connection.connect();
        return new JSONObject(new JSONTokener(convertStreamToString(connection.getInputStream())));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : JSONTokener(org.codehaus.jettison.json.JSONTokener) JSONObject(org.codehaus.jettison.json.JSONObject) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) IOException(java.io.IOException)

Aggregations

JSONObject (org.codehaus.jettison.json.JSONObject)10 JSONTokener (org.codehaus.jettison.json.JSONTokener)10 Graph (com.tinkerpop.blueprints.Graph)8 Vertex (com.tinkerpop.blueprints.Vertex)8 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)8 Test (org.junit.Test)8 Edge (com.tinkerpop.blueprints.Edge)5 HashSet (java.util.HashSet)3 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 URLConnection (java.net.URLConnection)1