8

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"];
halllo
  • 858
  • 12
  • 25

3 Answers3

7

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.

ADringer
  • 2,614
  • 36
  • 63
2

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));
KenA
  • 21
  • 2
0

Try this approach

IGremlinClient janusClient = JanusGraphClientBuilder.BuildClientForServer(new  GremlinServer("localhost", 8182)).Create();
GraphTraversalSource g = Traversal().WithRemote(new DriverRemoteConnection(janusClient));

IList<Vertex> x =  g.V().HasLabel("YourLabel").Has("YourpPopertyName", "some value").ToList();