19#include "google/protobuf/util/json_util.h"
26using Json = nlohmann::json;
27using google::protobuf::util::MessageToJsonString;
29google::protobuf::util::JsonOptions JsonOption() {
30 google::protobuf::util::JsonOptions json_option;
31 json_option.always_print_primitive_fields =
true;
38 const std::string &json_type,
const google::protobuf::Message &proto) {
39 static const auto kJsonOption = JsonOption();
40 std::string json_string;
41 const auto status = MessageToJsonString(proto, &json_string, kJsonOption);
42 ACHECK(status.ok()) <<
"Cannot convert proto to json:" << proto.DebugString();
45 json_obj[
"type"] = json_type;
46 json_obj[
"data"] = Json::parse(json_string);
51 static const auto kJsonOption = JsonOption();
52 std::string json_string;
53 const auto status = MessageToJsonString(proto, &json_string, kJsonOption);
54 Json json_obj = Json::parse(json_string);
60 const auto iter = json.find(key);
61 if (iter == json.end()) {
62 AERROR <<
"The json has no such key: " << key;
65 if (!iter->is_string()) {
66 AERROR <<
"The value of json[" << key <<
"] is not a string";
74 const std::vector<std::string> &paths,
75 nlohmann::json *value) {
76 Json upper_layer_json = json;
77 for (
auto &field : paths) {
79 AERROR <<
"Invalid path: " << field;
82 const auto iter = upper_layer_json.find(field);
83 if (iter == upper_layer_json.end()) {
84 AERROR <<
"The json has no such key: " << field;
87 if (!iter->is_object()) {
88 AERROR <<
"Required json but not,invalid type.";
91 upper_layer_json = *iter;
93 *value = upper_layer_json;
99 std::vector<std::string> paths = absl::StrSplit(path,
'.');
100 std::string key = paths.back();
102 Json upper_layer_json;
106 return GetString(upper_layer_json, key, value);
110 std::vector<std::string> *value) {
111 const auto iter = json.find(key);
112 if (iter == json.end()) {
113 AERROR <<
"The json has no such key: " << key;
116 if (!iter->is_array()) {
117 AERROR <<
"The value of json[" << key <<
"] is not an array";
123 value->reserve(iter->size());
124 for (
const auto &elem : *iter) {
127 if (!elem.is_string()) {
128 AWARN <<
"The value of json[" << key <<
"] contains non-string element";
131 value->push_back(elem);
139 const auto iter = json.find(key);
140 if (iter == json.end()) {
141 AERROR <<
"The json has no such key: " << key;
144 if (!iter->is_boolean()) {
145 AERROR <<
"The value of json[" << key <<
"] is not a boolean";
154 std::vector<std::string> paths = absl::StrSplit(path,
'.');
155 std::string key = paths.back();
157 Json upper_layer_json;
161 return GetBoolean(upper_layer_json, key, value);
static bool GetStringVector(const nlohmann::json &json, const std::string &key, std::vector< std::string > *value)
Get a string vector from the given json[key].
static bool GetStringByPath(const nlohmann::json &json, const std::string &path, std::string *value)
Get a string value from the given json and path.
static nlohmann::json ProtoToJson(const google::protobuf::Message &proto)
Convert proto to a json string.
static bool GetJsonByPath(const nlohmann::json &json, const std::vector< std::string > &paths, nlohmann::json *value)
Get the json from the given json and path.
static nlohmann::json ProtoToTypedJson(const std::string &json_type, const google::protobuf::Message &proto)
Convert proto to a json string.
static bool GetBoolean(const nlohmann::json &json, const std::string &key, bool *value)
Get a boolean value from the given json[key].
static bool GetString(const nlohmann::json &json, const std::string &key, std::string *value)
Get a string value from the given json[key].
static bool GetBooleanByPath(const nlohmann::json &json, const std::string &path, bool *value)
Get a bool value from the given json and path.