Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"];
like image 874
halllo Avatar asked Mar 30 '18 18:03

halllo


2 Answers

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.

like image 157
ADringer Avatar answered Nov 18 '22 21:11

ADringer


You can get your properties by using MyClass similar to

    class ProviderProperties {
        public object Name { get; set; }
        public object contact { get; set; }
        public object requesttype { get; set; }
        public object address { get; set; }
        public object phone { get; set; }
        public object description { get; set; }
        public object otherState { get; set; }
        public object otherCity { get; set; }
        public object addressStreet { get; set; }
    }

    class MyClass {
        public string id { get; set; }
        public string label { get; set; }
        public string type { get; set; }
        public ProviderProperties properties { get; set; }
    }

and using it in

JsonConvert.DeserializeObject<MyClass>(JsonConvert.SerializeObject(results));
like image 38
KenA Avatar answered Nov 18 '22 19:11

KenA