Converting ExecutionResult object to json

╄→尐↘猪︶ㄣ 提交于 2019-12-11 09:15:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!