Java GTFS-realtime and JSon Mapping

691 views
Skip to first unread message

Lele Martufi

unread,
Apr 18, 2016, 10:52:10 AM4/18/16
to GTFS-realtime
Seems that isn't possible to map java FeedMessage (GTFSRealTime.FeedMessage) with JSon mapping (jackson.map.ObjectMapper) since the following error occur:"JsonMappingException: Direct self-reference leading to cycle"

Sean Barbeau

unread,
Apr 19, 2016, 9:43:08 AM4/19/16
to GTFS-realtime
Lele,
GTFS-realtime feeds are in the Protocol Buffer format, and that's the format intended to be machine-readable - the plain text format that looks like JSON is just intended to be human-readable for debugging purposes.

Instead of using Jackson, you'll want to use this project instead, and consume the Protocol Buffer format:
https://github.com/google/gtfs-realtime-bindings/tree/master/java

It will effectively do the same thing - deserialize the data feed into Java objects you can work with in code.

Sean

Landon Reed

unread,
Jun 23, 2017, 4:30:13 PM6/23/17
to GTFS-realtime
Hi all,

I was investigating the same thing and came across the use of the com.googlecode.protobuf.format.JsonFormat class which has a method printToString() that should handle any JSON string you might need:

GtfsRealtime.FeedMessage feed = GtfsRealtime.FeedMessage.parseFrom(url.openStream());
String string = JsonFormat.printToString(feed);

NOTE: the code above is using version 1.2 of the protobuf library:

<dependency>
    <groupId>com.googlecode.protobuf-java-format</groupId>
    <artifactId>protobuf-java-format</artifactId>
    <version>1.2</version>
</dependency>
Reply all
Reply to author
Forward
0 new messages