1

I am using the Java Bolt driver (1.0.1) and I am wondering there is a way to convert the result to Json (possibly the same as in the REST api)?

I tried to use gson in this way:

Result r = null;
try ( Transaction tx = graphDb.beginTx() )
{
    r = graphDb.execute("MATCH...");
    tx.success();
} catch {...}

new Gson().toJson(result);

but what I get is:

java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:98)
    at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
    etc...
Randomize
  • 8,651
  • 18
  • 78
  • 133

2 Answers2

6

The API you show is not the Bolt-Driver, it's the embedded Java-API.

In the bolt-driver you can do

Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();

StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );

while ( result.hasNext() ) {
    Record record = result.next();
    gson.toJson(record.asMap());
}
session.close();
driver.close();
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • Thank you! Bit confused tho. In the test I am using the embedded version. But how can I use the bolt version for in-memory test (`new TestGraphDatabaseFactory().newImpermanentDatabase()`)? – Randomize Jun 03 '16 at 23:56
  • I have got an answer here: http://stackoverflow.com/questions/37628313/neo4j-unit-testing-the-bolt-driver-properly. – Randomize Jun 05 '16 at 10:12
  • hmmm it doesn't look the same like ur solution. I feel bit lost :( – Randomize Jun 05 '16 at 12:15
-2

I am developing an app in flask and need to do the same and then get it into a response but in Python. Im using jsonify instead of gson. Any suggestions??? Code right here:

@concepts_api.route('/concepts', methods=['GET'])
  def get_concepts_of_conceptgroup():
  try: 
      _json = request.json
      _group_name = _json['group_name']
      if _group_name  and request.method == 'GET':
          rows = concepts_service.get_concepts_of_conceptgroup(_group_name)
          resp = jsonify(rows)
          resp.status_code = 200
          return resp
    
      return not_found() 
  except:
      message = {
      'status': 500,
      'message': 'Error: Imposible to get concepts of conceptgroup.',
      }
      resp = jsonify(message)
      resp.status_code = 500
      return resp