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