2018-05-04 21:26:39 +00:00
|
|
|
// Copyright 2018 Google LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/proto_json_util.h>
|
2018-05-04 21:26:39 +00:00
|
|
|
|
|
|
|
#include <google/protobuf/util/json_util.h>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <glog/logging.h>
|
2018-05-04 21:26:39 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
std::string MessageToJsonString(const google::protobuf::Message& message) {
|
|
|
|
google::protobuf::util::JsonPrintOptions json_print_options;
|
|
|
|
json_print_options.preserve_proto_field_names = true;
|
|
|
|
|
|
|
|
std::string result;
|
2023-07-14 19:40:12 +00:00
|
|
|
ABSL_CHECK_OK(google::protobuf::util::MessageToJsonString(
|
2018-05-04 21:26:39 +00:00
|
|
|
message, &result, json_print_options));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonStringToMessage(const std::string& input,
|
|
|
|
google::protobuf::Message* message) {
|
|
|
|
google::protobuf::util::JsonParseOptions json_parse_options;
|
|
|
|
json_parse_options.ignore_unknown_fields = true;
|
|
|
|
|
|
|
|
auto status = google::protobuf::util::JsonStringToMessage(input, message,
|
|
|
|
json_parse_options);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Failed to parse from JSON: " << input
|
2022-11-04 22:46:41 +00:00
|
|
|
<< " error: " << status.message();
|
2018-05-04 21:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|