I'm trying to read information from a service which only provides it in Protocol Buffer format.

I'd never heard of it until now, and this is what I understand about it:

  1. It's binary
  2. you don't just read it like JSON, you have to know the format of it first
  3. you know the format because you get a proto file and parse that
  4. this can be done by https://metacpan.org/pod/Google::ProtocolBuffers
  5. Google::ProtocolBuffers returns a list of accessor objects, essentially modules created on the fly
  6. Then you grab data from your binary file by using one of the accessors

I've got as far as step 5. Now what?

I have the binary file, and the proto file which describes the format, but I can't figure out how to access the data.

The data is basically hundreds of these entities (bus information for Sydney Australia):

entity { id: "42558_202397_3000_41_1" vehicle { trip { trip_id: "892878" start_time: "16:05:00" start_date: "20190609" schedule_relationship: SCHEDULED route_id: "3000_41" } position { latitude: -33.03611 longitude: 151.66042 bearing: 332.0 speed: 2.2 } timestamp: 1560060858 congestion_level: UNKNOWN_CONGESTION_LEVEL vehicle { id: "42558_202397_3000_41_1" [transit_realtime.tfnsw_vehicle_descriptor] { air_conditioned: true wheelchair_accessible: 1 vehicle_model: "Volvo~B7RLE~Bustech~VST" special_vehicle_attributes: 0 } } occupancy_status: MANY_SEATS_AVAILABLE } }

And this is what the module tells me are the accessors:

[ 'TransitRealtime::Alert::Cause', 'TransitRealtime::Alert::Effect', 'TransitRealtime::FeedHeader::Incrementality', 'TransitRealtime::TripDescriptor::ScheduleRelationship', 'TransitRealtime::TripUpdate::StopTimeUpdate::ScheduleRelati +onship', 'TransitRealtime::VehiclePosition::CongestionLevel', 'TransitRealtime::VehiclePosition::OccupancyStatus', 'TransitRealtime::VehiclePosition::VehicleStopStatus', 'TransitRealtime::Alert', 'TransitRealtime::EntitySelector', 'TransitRealtime::FeedEntity', 'TransitRealtime::FeedHeader', 'TransitRealtime::FeedMessage', 'TransitRealtime::Position', 'TransitRealtime::TfnswVehicleDescriptor', 'TransitRealtime::TimeRange', 'TransitRealtime::TranslatedString', 'TransitRealtime::TranslatedString::Translation', 'TransitRealtime::TripDescriptor', 'TransitRealtime::TripUpdate', 'TransitRealtime::TripUpdate::StopTimeEvent', 'TransitRealtime::TripUpdate::StopTimeUpdate', 'TransitRealtime::VehicleDescriptor', 'TransitRealtime::VehiclePosition' ];

What I would like to do is parse it like I would parse JSON. Say I want to see where that bus (route_id: "3000_41") is from that data, what should I do? What's the equivalent of parsing through like

foreach $entity ( @entities ){ if($entity->route_id eq '3000_41'){ print "bus is at " . $position->{latitude} . ', ' . $position +->{longitude}; } }

In reply to Can anyone help with protocol buffers? by Cody Fendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.