Search in sources :

Example 1 with JSONTokener

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

the class GraphSONUtilityTest method edgeFromJsonNormalLabelOrIdOnEdge.

@Test
public void edgeFromJsonNormalLabelOrIdOnEdge() 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);
    Edge e = GraphSONUtility.edgeFromJson(new JSONObject(new JSONTokener(edgeJsonLight)), v1, v2, factory, GraphSONMode.NORMAL, 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 2 with JSONTokener

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

the class GraphSONUtilityTest method vertexFromJsonIgnoreKeyValid.

@Test
public void vertexFromJsonIgnoreKeyValid() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Set<String> ignoreAge = new HashSet<String>();
    ignoreAge.add("age");
    ElementPropertyConfig config = ElementPropertyConfig.excludeProperties(ignoreAge, null);
    GraphSONUtility utility = new GraphSONUtility(GraphSONMode.NORMAL, factory, config);
    Vertex v = utility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)));
    Assert.assertSame(v, g.getVertex(1));
    // tinkergraph converts id to string
    Assert.assertEquals("1", v.getId());
    Assert.assertEquals("marko", v.getProperty("name"));
    Assert.assertNull(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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with JSONTokener

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

the class GraphSONUtilityTest method edgeFromJsonValid.

@Test
public void edgeFromJsonValid() 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);
    Edge e = GraphSONUtility.edgeFromJson(new JSONObject(new JSONTokener(edgeJson)), v1, v2, factory, GraphSONMode.NORMAL, null);
    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.assertEquals(0.5d, e.getProperty("weight"));
    Assert.assertEquals("knows", e.getLabel());
    Assert.assertEquals(v1, e.getVertex(Direction.OUT));
    Assert.assertEquals(v2, e.getVertex(Direction.IN));
}
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 4 with JSONTokener

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

the class GraphSONUtilityTest method vertexFromJsonExtendedTypesValid.

@Test
public void vertexFromJsonExtendedTypesValid() throws IOException, JSONException {
    // need to test those data types that are not covered by the toy graphs
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Vertex v = GraphSONUtility.vertexFromJson(new JSONObject(new JSONTokener(vertexJson3)), factory, GraphSONMode.EXTENDED, null);
    Assert.assertSame(v, g.getVertex(1));
    Assert.assertEquals((byte) 4, v.getProperty("byteValue"));
    Assert.assertEquals(new Short("10"), v.getProperty("shortValue"));
    Assert.assertEquals(true, v.getProperty("booleanValue"));
}
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 5 with JSONTokener

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

the class GraphSONUtilityTest method edgeFromJsonInputStreamCompactNoIdOnEdge.

@Test
public void edgeFromJsonInputStreamCompactNoIdOnEdge() throws IOException, JSONException {
    Graph g = new TinkerGraph();
    ElementFactory factory = new GraphElementFactory(g);
    Set<String> vertexKeys = new HashSet<String>() {

        {
            add(GraphSONTokens._ID);
        }
    };
    Set<String> edgeKeys = new HashSet<String>() {

        {
            add(GraphSONTokens._IN_V);
        }
    };
    GraphSONUtility graphson = new GraphSONUtility(GraphSONMode.COMPACT, factory, vertexKeys, edgeKeys);
    Vertex v1 = graphson.vertexFromJson(new JSONObject(new JSONTokener(vertexJson1)));
    Vertex v2 = graphson.vertexFromJson(new JSONObject(new JSONTokener(vertexJson2)));
    Edge e = graphson.edgeFromJson(inputStreamEdgeJsonLight, v1, v2);
    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) HashSet(java.util.HashSet) Test(org.junit.Test)

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