问题
I need to expose a public API, and i need to convert the result of a cypher query into json. I have begun the process, but i am having problems serializing an object of type scala.collection.convert.Wrappers$SeqWrapper that gets returned when using collect() in cypher.
Here is the cypher query:
MATCH (orders:Orders {id:'locationrestaurant'}), (order:Order), (orders)-[:hasOrder]-(order),(order)-[:orderedProduct]->(product),(client)-[:ordered]->(order) return (order),(client), collect(product) as products;
How can i handle this type of object? Can i cast it to a List? Also, are there any libraries for converting ExecutionResult to json?
If you need any more details, please ask. Here is my code
public QueryResult runCypher(String query, Map<String,Object> params)
{
QueryResult result = new QueryResult();
Transaction tx = service.beginTx();
ExecutionResult execResult = null;
boolean success = true;
System.out.println(query);
try
{
if(params!=null) execResult = engine.execute(query, params);
else execResult = engine.execute(query);
result.result = getReturnedObjectsToJson(execResult);
}
catch(Exception e)
{
System.out.println(e.getMessage()+" exception message");
result.result = e.getMessage();
success = false;
}
finally
{
if(success) tx.success();
else tx.failure();
}
tx.close();
result.success = success;
return result;
}
Basically, getReturnedObjectsToJson does the work.
回答1:
How can i handle this type of object?
data.get("labels") instanceof java.util.Collection
Can i cast it to a List?
yes
Also, are there any libraries for converting ExecutionResult to json?
ExecutionResult is iterable, i think you can use any popular java json framework, for example gson or jackson
回答2:
You can use Jacksons databind framework. That will allow you to turn it back into an execution result at the destination.
If you want to get the actual graph back from an execution result so that the Json is in some kind of graph schema. That is a lot more work.
I have been writing a library that will parse arbitary graphs into Cypher and execution results back into graphs to work with a virtual query builder, and it is reasonably hard work. Hopefully it will all be working soon. :)
来源:https://stackoverflow.com/questions/22618102/converting-executionresult-object-to-json