16 #include "absl/status/statusor.h"
17 #include "absl/strings/str_cat.h"
18 #include "google/protobuf/descriptor.h"
19 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
20 #include "google/protobuf/message.h"
21 #include "google/protobuf/text_format.h"
22 #include "google/protobuf/util/json_util.h"
30 using ::google::protobuf::TextFormat;
37 std::string uncompressed;
38 if (
GunzipString(contents, &uncompressed)) contents.swap(uncompressed);
44 google::protobuf::Message*
proto) {
49 std::string uncompressed;
51 VLOG(1) <<
"ReadFileToProto(): input is gzipped";
52 data.swap(uncompressed);
65 constexpr
double kMaxBinaryProtoParseShrinkFactor = 2;
66 if (
proto->ParseFromString(data)) {
72 proto->DiscardUnknownFields();
73 if (
proto->ByteSizeLong() <
74 data.size() / kMaxBinaryProtoParseShrinkFactor) {
75 VLOG(1) <<
"ReadFileToProto(): input may be a binary proto, but of a "
78 VLOG(1) <<
"ReadFileToProto(): input seems to be a binary proto";
82 if (google::protobuf::TextFormat::ParseFromString(data,
proto)) {
83 VLOG(1) <<
"ReadFileToProto(): input is a text proto";
86 if (google::protobuf::util::JsonStringToMessage(
87 data,
proto, google::protobuf::util::JsonParseOptions())
89 constexpr
int kMaxJsonToBinaryShrinkFactor = 30;
90 if (
proto->ByteSizeLong() < data.size() / kMaxJsonToBinaryShrinkFactor) {
91 VLOG(1) <<
"ReadFileToProto(): input is probably JSON, but probably not"
92 " of the right proto";
94 VLOG(1) <<
"ReadFileToProto(): input is a proto JSON";
98 LOG(
WARNING) <<
"Could not parse protocol buffer";
103 const google::protobuf::Message&
proto,
105 bool append_extension_to_file_name) {
106 std::string file_type_suffix;
107 std::string output_string;
108 google::protobuf::io::StringOutputStream stream(&output_string);
109 switch (proto_write_format) {
111 if (!
proto.SerializeToZeroCopyStream(&stream)) {
112 LOG(
WARNING) <<
"Serialize to stream failed.";
115 file_type_suffix =
".bin";
118 if (!google::protobuf::TextFormat::PrintToString(
proto, &output_string)) {
124 google::protobuf::util::JsonPrintOptions options;
125 options.add_whitespace =
true;
126 options.always_print_primitive_fields =
true;
127 options.preserve_proto_field_names =
true;
128 if (!google::protobuf::util::MessageToJsonString(
proto, &output_string,
134 file_type_suffix =
".json";
138 std::string gzip_string;
140 output_string.swap(gzip_string);
141 file_type_suffix +=
".gz";
143 std::string output_filename(filename);
144 if (append_extension_to_file_name) output_filename += file_type_suffix;
145 VLOG(1) <<
"Writing " << output_string.size() <<
" bytes to "
#define VLOG(verboselevel)
void GzipString(absl::string_view uncompressed, std::string *compressed)
bool GunzipString(const std::string &str, std::string *out)
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
bool WriteProtoToFile(absl::string_view filename, const google::protobuf::Message &proto, ProtoWriteFormat proto_write_format, bool gzipped, bool append_extension_to_file_name)
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
#define RETURN_IF_ERROR(expr)