Skip to content

Commit fd5b15a

Browse files
committed
add kCanonicalJson to fileutil
1 parent 5496c87 commit fd5b15a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

ortools/util/file_util.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bool ReadFileToProto(absl::string_view filename,
6868
if (proto->ParseFromString(data)) {
6969
// NOTE(user): When using ParseFromString() from a generic
7070
// google::protobuf::Message, like we do here, all fields are stored, even
71-
// if their are unknown in the underlying proto type. Unless we explicitly
71+
// if they are unknown in the underlying proto type. Unless we explicitly
7272
// discard those 'unknown fields' here, our call to ByteSizeLong() will
7373
// still count the unknown payload.
7474
proto->DiscardUnknownFields();
@@ -124,7 +124,7 @@ bool WriteProtoToFile(absl::string_view filename,
124124
return false;
125125
}
126126
break;
127-
case ProtoWriteFormat::kJson:
127+
case ProtoWriteFormat::kJson: {
128128
google::protobuf::util::JsonPrintOptions options;
129129
options.add_whitespace = true;
130130
options.always_print_primitive_fields = true;
@@ -137,6 +137,18 @@ bool WriteProtoToFile(absl::string_view filename,
137137
}
138138
file_type_suffix = ".json";
139139
break;
140+
}
141+
case ProtoWriteFormat::kCanonicalJson:
142+
google::protobuf::util::JsonPrintOptions options;
143+
options.add_whitespace = true;
144+
if (!google::protobuf::util::MessageToJsonString(proto, &output_string,
145+
options)
146+
.ok()) {
147+
LOG(WARNING) << "Printing to stream failed.";
148+
return false;
149+
}
150+
file_type_suffix = ".json";
151+
break;
140152
}
141153
if (gzipped) {
142154
std::string gzip_string;

ortools/util/file_util.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ Proto ReadFileToProtoOrDie(absl::string_view filename) {
4444
return proto;
4545
}
4646

47-
enum class ProtoWriteFormat { kProtoText, kProtoBinary, kJson };
47+
// Specifies how the proto should be formatted when writing it to a file.
48+
// kCanonicalJson applies Proto3 to JSON encoding conventions and converts field
49+
// names to lower camel-case.
50+
enum class ProtoWriteFormat { kProtoText, kProtoBinary, kJson, kCanonicalJson };
4851

4952
// Writes a proto to a file. Supports the following formats: binary, text, JSON,
5053
// all of those optionally gzipped. Returns false on failure.
5154
// If 'proto_write_format' is kProtoBinary, ".bin" is appended to file_name. If
52-
// 'proto_write_format' is kJson, ".json" is appended to file_name. If 'gzipped'
53-
// is true, ".gz" is appended to file_name.
55+
// 'proto_write_format' is kJson or kCanonicalJson, ".json" is appended to
56+
// file_name. If 'gzipped' is true, ".gz" is appended to file_name.
5457
bool WriteProtoToFile(absl::string_view filename,
5558
const google::protobuf::Message& proto,
5659
ProtoWriteFormat proto_write_format, bool gzipped = false,

0 commit comments

Comments
 (0)