Search code examples
azure-cosmosdbgremlin

Best way to parse a Gremlin.Net response?


What is the best way to get POCO's from a Gremlin.Net response?

Right now I manually cast to dictionaries:

var results = await gremlinClient.SubmitAsync<Dictionary<string, object>>("g.V()");
var result = results[0];
var properties = (Dictionary<string, object>)result["properties"];
var value = ((Dictionary<string, object>)properties["myValue"].Single())["value"];

Solution

  • I found that the GremlinClient can only return dynamic objects, if you put anything else as the type, it fails (unless I was just doing something wrong).

    What I ended up doing was serialising the dynamic object to JSON and then deserialising it back to the object type I wanted:

    var results = await gremlinClient.SubmitAsync<dynamic>("g.V()");
    JsonConvert.DeserializeObject<MyResult>(JsonConvert.SerializeObject(results));
    

    The dynamic object is just a Dictionary, but if you serialise it first it has the proper hierarchy of fields/properties which can then be deserialised to what you actually expect.

    Seems a bit of a pain to have to do the extra conversion, but only way I got it to work.