Mapping Gremlin Traversal Results to Strongly-Typed Objects

566 views
Skip to first unread message

Bassem Naguib

unread,
Feb 20, 2021, 12:53:11 PM2/20/21
to Gremlin-users
Hello,

I am using Gremlin.Net and I am wondering if there is a way to map the Gremlin traversal results which are usually IList and IDictionary objects to strongly typed objects.

I think this is a very common need. But I could not find anything in Gremlin.Net that does it. Did someone already create a library in .NET or in any other language that does that? I think this library needs to be made specifically for Gremlin and needs to be aware of the list of types supported by GraphSON.

Here is the best way I could find so far. But of course it is inefficient to serialize and deserialize from JSON. Plus I am not sure if all the types will be mapped correctly.

IList<IDictionary<stringobject>> peopleDictionaries = await g.V()
    .HasLabel("person")
    .Project<object>("Id""FirstName""LastName")
    .By(T.Id)
    .By("firstName")
    .By("lastName")
    .Promise(res => res.ToList());

string peopleJson = JsonSerializer.Serialize(peopleDictionaries);
IEnumerable<PersonpeopleObjects = JsonSerializer.Deserialize<IEnumerable<Person>>(peopleJson);

Dave Bechberger

unread,
Feb 20, 2021, 1:16:29 PM2/20/21
to gremli...@googlegroups.com
Have you taken seen GremLinq. It is an object-graph mapper for .NET that may work for your needs. 

https://github.com/ExRam/ExRam.Gremlinq

Dave Bechberger


On Feb 20, 2021, at 8:53 AM, Bassem Naguib <bass...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/e7b908a3-7842-493b-8bdc-307e1027cb8fn%40googlegroups.com.

Bassem Naguib

unread,
Feb 20, 2021, 3:14:11 PM2/20/21
to Gremlin-users
Thank you David for replying!

I looked into Gremlinq quickly. But could not find what I am looking for.

I am not trying to make big changes to the way I write Gremlin traversals. I think what I am looking for is an extension method like .ToObject<MyClass>() that can be added to the end of the Gremlin traversal. I no one wrote this function already, I will try to write it and share the code here.

Florian Hockmann

unread,
Feb 23, 2021, 4:17:46 AM2/23/21
to Gremlin-users
You could do this by creating your own deserializers and then registering them with Gremlin.Net. You then either have to add your own custom GraphSON types for which you control the deserialization or you could add your own MapSerializer for the GraphSON type Map which then doesn't create a map but objects of your types. Note though that the serialization and deserialization in Gremlin.Net are not type aware so your serializer won't know the type, e.g., Person in your example. You probably have to include it therefore in the map that comes from the server for example as the label for the serializer.
If you decide to even add your own custom GraphSON types then you of course also have to add serializers for them on the server side and in all other clients.

The alternative to this would keep the serialization / deserialization in Gremlin.Net as is and then just work with dictionaries to create instances of your own types. Reflection or a code generator could be helpful for this if you have a lot of types.


I think this is in general an area where Gremlin.Net could be improved but I'm not sure yet how exactly. The problem is that we have the GraphSON / GraphBinary types (as described in the IO reference) that we get from the server that drive the deserialization right now. We would need to add the option for users to specify the expected return type and then use that in conjunction the type information returned from the server for the deserialization.
If you want to work on that, then I would of course be glad to help if you need any.

Bassem Naguib

unread,
Feb 23, 2021, 5:10:11 PM2/23/21
to Gremlin-users
Thank you so much Florian for all the information! I think I will start by trying to build objects from the lists and dictionaries returned by Gremlin.Net. But I will also start to familiarize myself with the Gremlin.Net code base and hopefully in the future I will find a way to deserialize the GraphSON returned by the server into a strongly typed .NET object directly.
Reply all
Reply to author
Forward
0 new messages