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 <google/protobuf/util/json_util.h>
10 
11 #include "packager/base/logging.h"
12 
13 namespace shaka {
14 namespace media {
15 
16 std::string MessageToJsonString(const google::protobuf::Message& message) {
17  google::protobuf::util::JsonPrintOptions json_print_options;
18  json_print_options.preserve_proto_field_names = true;
19 
20  std::string result;
21  GOOGLE_CHECK_OK(google::protobuf::util::MessageToJsonString(
22  message, &result, json_print_options));
23  return result;
24 }
25 
26 bool JsonStringToMessage(const std::string& input,
27  google::protobuf::Message* message) {
28  google::protobuf::util::JsonParseOptions json_parse_options;
29  json_parse_options.ignore_unknown_fields = true;
30 
31  auto status = google::protobuf::util::JsonStringToMessage(input, message,
32  json_parse_options);
33  if (!status.ok()) {
34  LOG(ERROR) << "Failed to parse from JSON: " << input
35  << " error: " << status.error_message();
36  return false;
37  }
38  return true;
39 }
40 
41 } // namespace media
42 } // namespace shaka
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11