Neo4j Cypher results as JSON: getting JsonMappingException when using JsonHelper.createJsonFrom (), but only with certain requests

I am working on neo4vertx, a module that allows talking to a Neo4j database using Vert.x. Specifically, I am working on a "request" function that allows the Vert.x user to send a Cypher request as an eventbus message and receive a JSON result set.

However, I seem to run into an unexpected issue when serializing to JSON using JsonHelper.createJsonFrom () with certain requests.

Quick example (for example there are things in the database):

// This Fails with JsonMappingException (see below):
String query="MATCH (n) RETURN n";

// This Succeeds:
String query="MATCH (n) RETURN n.something";

//Rest of code:
engine = new ExecutionEngine(graphDatabaseService);
ExecutionResult result;
result = engine.execute(query);

Object object = result.iterator();
String foo = JsonHelper.createJsonFrom(object);
System.out.println("DEBUG (foo): " + foo);

      

Does anyone know this? We essentially want to be able to send any request and return an empty json string or json representation that doesn't look like the result. json that you can pull from the neo4j web interface!

An exception:

testQuery(org.openpcf.neo4vertx.neo4j.Neo4jGraphTest)  Time elapsed: 2.362 sec  <<< ERROR!
org.neo4j.server.rest.domain.JsonBuildRuntimeException: org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.neo4j.kernel.InternalAbstractGraphDatabase$DependencyResolverImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: scala.collection.convert.MapWrapper["content"]->org.neo4j.kernel.impl.core.NodeProxy["graphDatabase"]->org.neo4j.test.["dependencyResolver"])

...

      

+3


source to share


3 answers


While ExecutionResult is an implementation Iterator<Map<String,Object>>

, it is currently just a java wrapper around the scala classes as well as the Neo4j classes (e.g. Node, Relationship, Path).

So, you should probably do 2 things:



  • recursively replace neo4j classes with corresponding maps and lists
  • possible: recursively replace scala lists and maps with java lists and maps for example. new LinkedHashMap(row)

I did something a while ago in my cypher-websocket-experiment

0


source


You can serialize ExecutionResult from Neo4J to JSON like this:

ExecutionResult result ...
CypherResultRepresentation repr = new CypherResultRepresentation(result, false, false);
OutputFormat format = new OutputFormat(new JsonFormat(), null, null);
String json = format.assemble(repr);

      



The next thing you need to do is create the appropriate vert.x JsonObjects.

@Rubin: I am currently adding this to neo4vertx;)

0


source


Using RESTAPI:

String query = "MATCH (a)-[r]-(b) RETURN a,r,b";
RestAPI restAPI = new RestAPIFacade(URI);
CypherResult result = restAPI.query(query, Collections.<String, Object>emptyMap());
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String stringResult = gson.toJson(result.asMap());
JSONObject jsonResult = new JSONObject(stringResult);

      

Using a bolt

    String query = "MATCH (a)-[r]-(b) RETURN a,r,b";
String stringConnection = "bolt://" + HOST + ":7687";
Driver driver = GraphDatabase.driver(stringConnection, AuthTokens.basic(USER, PASS));
Session session = driver.session();
StatementResult result = session.run(query);
session.close();
driver.close();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
while (result.hasNext()) {
    Record record = result.next();
    stringResult = gson.toJson(record.asMap());
}
JSONObject jsonResult = new JSONObject(stringResult);

      

0


source







All Articles