Shaka Packager SDK
proto_json_util.cc
1 // Copyright 2018 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/base/proto_json_util.h>
8 
9 #include <absl/log/log.h>
10 #include <google/protobuf/util/json_util.h>
11 
12 namespace shaka {
13 namespace media {
14 
15 std::string MessageToJsonString(const google::protobuf::Message& message) {
16  google::protobuf::util::JsonPrintOptions json_print_options;
17  json_print_options.preserve_proto_field_names = true;
18 
19  std::string result;
20  ABSL_CHECK_OK(google::protobuf::util::MessageToJsonString(
21  message, &result, json_print_options));
22  return result;
23 }
24 
25 bool JsonStringToMessage(const std::string& input,
26  google::protobuf::Message* message) {
27  google::protobuf::util::JsonParseOptions json_parse_options;
28  json_parse_options.ignore_unknown_fields = true;
29 
30  auto status = google::protobuf::util::JsonStringToMessage(input, message,
31  json_parse_options);
32  if (!status.ok()) {
33  LOG(ERROR) << "Failed to parse from JSON: " << input
34  << " error: " << status.message();
35  return false;
36  }
37  return true;
38 }
39 
40 } // namespace media
41 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66