18 #include "absl/status/statusor.h"
19 #include "absl/strings/str_cat.h"
20 #include "google/protobuf/descriptor.h"
21 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
22 #include "google/protobuf/message.h"
23 #include "google/protobuf/text_format.h"
24 #include "google/protobuf/util/json_util.h"
33 using google::protobuf::util::JsonParseOptions;
34 using google::protobuf::util::JsonStringToMessage;
41 std::string uncompressed;
42 if (
GunzipString(contents, &uncompressed)) contents.swap(uncompressed);
48 google::protobuf::Message*
proto) {
53 std::string uncompressed;
55 VLOG(1) <<
"ReadFileToProto(): input is gzipped";
56 data.swap(uncompressed);
69 constexpr
double kMaxBinaryProtoParseShrinkFactor = 2;
70 if (
proto->ParseFromString(data)) {
76 proto->DiscardUnknownFields();
77 if (
proto->ByteSizeLong() <
78 data.size() / kMaxBinaryProtoParseShrinkFactor) {
79 VLOG(1) <<
"ReadFileToProto(): input may be a binary proto, but of a "
82 VLOG(1) <<
"ReadFileToProto(): input seems to be a binary proto";
86 if (google::protobuf::TextFormat::ParseFromString(data,
proto)) {
87 VLOG(1) <<
"ReadFileToProto(): input is a text proto";
91 const auto status = JsonStringToMessage(data,
proto, JsonParseOptions());
99 constexpr
int kMaxJsonToBinaryShrinkFactor = 30;
100 if (
proto->ByteSizeLong() < data.size() / kMaxJsonToBinaryShrinkFactor) {
101 VLOG(1) <<
"ReadFileToProto(): input is probably JSON, but probably not"
102 " of the right proto";
104 VLOG(1) <<
"ReadFileToProto(): input is a proto JSON";
108 LOG(WARNING) <<
"Could not parse protocol buffer";
113 const google::protobuf::Message&
proto,
115 bool append_extension_to_file_name) {
116 std::string file_type_suffix;
117 std::string output_string;
118 google::protobuf::io::StringOutputStream stream(&output_string);
119 switch (proto_write_format) {
121 if (!
proto.SerializeToZeroCopyStream(&stream)) {
122 LOG(WARNING) <<
"Serialize to stream failed.";
125 file_type_suffix =
".bin";
128 if (!google::protobuf::TextFormat::PrintToString(
proto, &output_string)) {
129 LOG(WARNING) <<
"Printing to string failed.";
134 google::protobuf::util::JsonPrintOptions options;
135 options.add_whitespace =
true;
136 options.always_print_primitive_fields =
true;
137 options.preserve_proto_field_names =
true;
138 if (!google::protobuf::util::MessageToJsonString(
proto, &output_string,
141 LOG(WARNING) <<
"Printing to stream failed.";
144 file_type_suffix =
".json";
148 google::protobuf::util::JsonPrintOptions options;
149 options.add_whitespace =
true;
150 if (!google::protobuf::util::MessageToJsonString(
proto, &output_string,
153 LOG(WARNING) <<
"Printing to stream failed.";
156 file_type_suffix =
".json";
160 std::string gzip_string;
162 output_string.swap(gzip_string);
163 file_type_suffix +=
".gz";
165 std::string output_filename(filename);
166 if (append_extension_to_file_name) output_filename += file_type_suffix;
167 VLOG(1) <<
"Writing " << output_string.size() <<
" bytes to "
171 LOG(WARNING) <<
"Writing to file failed.";
#define RETURN_IF_ERROR(expr)
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)
Collection of objects used to extend the Constraint Solver library.
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 VLOG(verboselevel)