GraphSON and Instant

128 views
Skip to first unread message

Philip Graff

unread,
Apr 9, 2018, 1:48:15 PM4/9/18
to Gremlin-users
Hi all,

I've run into an interesting issue with Java's Instant and GraphSON. I will illustrate with a simple example.

------------------------------------------------------------------------------------

Import Instant, open a graph, and add a vertex with a time value

gremlin> import java.time.Instant

gremlin> graph=TinkerGraph.open()

==>tinkergraph[vertices:0 edges:0]

gremlin> v = graph.addVertex()

==>v[0]

gremlin> v.property("time",Instant.now())

==>vp[time->2018-04-09T16:10:...]


This time is stored properly as an Instant

gremlin> v.value("time")

==>2018-04-09T16:10:43.688Z

gremlin> v.value("time").getClass()

==>class java.time.Instant


Save to GraphML, GraphSON, and Gryo

gremlin> graph.io(IoCore.graphml()).writeGraph("/tmp/graph.graphml")

==>null

gremlin> graph.io(IoCore.graphson()).writeGraph("/tmp/graph.graphson")

==>null

gremlin> graph.io(IoCore.gryo()).writeGraph("/tmp/graph.gryo")

==>null


Clear the graph

gremlin> graph.clear()

==>null


Import the graph from GraphML and check the time - it is now a String as expected

gremlin> graph.traversal().V().values("time").next().getClass()

==>class java.lang.String


Clear the graph


gremlin> graph.clear()

==>null


Import the graph from Gryo and check the time - it is an Instant as expected

gremlin> graph.io(IoCore.gryo()).readGraph("/tmp/graph.gryo")

==>null

gremlin> graph.traversal().V().values("time").next().getClass()

==>class java.time.Instant


Clear the graph


gremlin> graph.clear()

==>null


Import the graph from GraphSON and check the time - it is now a LinkedHashMap

gremlin> graph.io(IoCore.graphson()).readGraph("/tmp/graph.graphson")

==>null

gremlin> graph.traversal().V().values("time").next().getClass()

==>class java.util.LinkedHashMap


------------------------------------------------------------------------------------


Is this a known issue? Since GraphSON is typed shouldn't it store this as an Instant? Below is the pretty-printed GraphSON file which also seems to be storing the Instant in a weird way.

{

    "id": {

        "@type": "g:Int64",

        "@value": 0

    },

    "label": "vertex",

    "properties": {

        "time": [

            {

                "id": {

                    "@type": "g:Int64",

                    "@value": 1

                },

                "value": {

                    "nano": 688000000,

                    "epochSecond": {

                        "@type": "g:Int64",

                        "@value": 1523290243

                    }

                }

            }

        ]

    }

}



Thanks,
Phil

Stephen Mallette

unread,
Apr 9, 2018, 1:55:11 PM4/9/18
to Gremlin-users
Instant is part of the "extended" set of GraphSON types - you have to specify support for it explicitly:


I think that explains what you are seeing.

--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/c15dbd51-5a56-4e8f-8a5b-17f2db7c365f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Philip Graff

unread,
Apr 9, 2018, 4:35:19 PM4/9/18
to Gremlin-users
OK, thanks, that's probably it. My next question is how do I use the mapper obtained by following the code in the documentation (excerpted below)?

mapper = GraphSONMapper.build().
                        typeInfo
(TypeInfo.PARTIAL_TYPES).
                        addCustomModule
(GraphSONXModuleV3d0.build().create(
false)).
                        version
(GraphSONVersion.V3_0).create().createMapper()


Thanks,
Phil
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Stephen Mallette

unread,
Apr 9, 2018, 4:58:45 PM4/9/18
to Gremlin-users
The reader/writer instances have mapper() methods on their Builder classes:


writer = GraphSONWriter.build().mapper(mapperWithExtension).create()
reader = GraphSONReader.build().mapper(mapperWithExtension).create()




To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/bb99a9d3-4acb-4ab4-a071-d4134d558e35%40googlegroups.com.

Philip Graff

unread,
Apr 9, 2018, 5:29:43 PM4/9/18
to Gremlin-users
Got it, thanks!
Reply all
Reply to author
Forward
0 new messages