22#include "google/protobuf/message.h"
23#include "nlohmann/json.hpp"
24#include "absl/strings/str_split.h"
39 const std::string &json_type,
const google::protobuf::Message &proto);
45 static nlohmann::json
ProtoToJson(
const google::protobuf::Message &proto);
51 static bool GetString(
const nlohmann::json &json,
const std::string &key,
59 static bool GetNumber(
const nlohmann::json &json,
const std::string &key,
61 const auto iter = json.find(key);
62 if (iter == json.end()) {
63 AERROR <<
"The json has no such key: " << key;
66 if (!iter->is_number()) {
67 AERROR <<
"The value of json[" << key <<
"] is not a number";
78 static bool GetBoolean(
const nlohmann::json &json,
const std::string &key,
86 const std::string &key,
87 std::vector<std::string> *value);
95 const std::vector<std::string> &paths,
96 nlohmann::json *value);
104 const std::string &path, std::string *value);
112 const std::string &path,
bool *value);
121 const std::string &path, T *value) {
122 std::vector<std::string> paths = absl::StrSplit(path,
'.');
123 std::string key = paths.back();
125 nlohmann::json upper_layer_json = json;
126 for (
auto &field : paths) {
128 AERROR <<
"Invalid path: " << path;
131 const auto iter = upper_layer_json.find(field);
132 if (iter == upper_layer_json.end()) {
133 AERROR <<
"The json has no such key: " << field;
136 upper_layer_json = *iter;
138 return GetNumber(upper_layer_json, key, value);
static bool GetNumber(const nlohmann::json &json, const std::string &key, T *value)
Get a number value from the given json[key].
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 GetNumberByPath(const nlohmann::json &json, const std::string &path, T *value)
Get a number value from the given json and path.
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.