-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move rt utils from test to rt folder
- Loading branch information
1 parent
e8c8f8e
commit cf9c94d
Showing
6 changed files
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace nigiri::rt { | ||
|
||
std::string json_to_protobuf(std::string const& json); | ||
std::string protobuf_to_json(std::string const& protobuf); | ||
|
||
} // namespace nigiri::rt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "nigiri/rt/util.h" | ||
|
||
#include "google/protobuf/util/json_util.h" | ||
|
||
#include "utl/verify.h" | ||
|
||
#include "gtfsrt/gtfs-realtime.pb.h" | ||
|
||
namespace nigiri::rt { | ||
|
||
std::string json_to_protobuf(std::string const& json) { | ||
transit_realtime::FeedMessage msg; | ||
auto const status = google::protobuf::util::JsonStringToMessage(json, &msg); | ||
utl::verify(status.ok(), "json_to_protobuf: {}", status.message()); | ||
return msg.SerializeAsString(); | ||
} | ||
|
||
std::string protobuf_to_json(std::string const& protobuf) { | ||
transit_realtime::FeedMessage msg; | ||
auto const success = | ||
msg.ParseFromArray(reinterpret_cast<void const*>(protobuf.data()), | ||
static_cast<int>(protobuf.size())); | ||
utl::verify(success, "json_to_protobuf: read protobuf FeedMessage failed"); | ||
|
||
auto json = std::string{}; | ||
auto const status = google::protobuf::util::MessageToJsonString( | ||
msg, &json, {.add_whitespace = true}); | ||
utl::verify(status.ok(), "protobuf_to_json: {}", status.message()); | ||
return json; | ||
} | ||
|
||
} // namespace nigiri::rt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters