Apollo 10.0
自动驾驶开放平台
apollo::common::util::JsonUtil类 参考

#include <json_util.h>

apollo::common::util::JsonUtil 的协作图:

静态 Public 成员函数

static nlohmann::json ProtoToTypedJson (const std::string &json_type, const google::protobuf::Message &proto)
 Convert proto to a json string.
 
static nlohmann::json ProtoToJson (const google::protobuf::Message &proto)
 Convert proto to a json string.
 
static bool GetString (const nlohmann::json &json, const std::string &key, std::string *value)
 Get a string value from the given json[key].
 
template<class T >
static bool GetNumber (const nlohmann::json &json, const std::string &key, T *value)
 Get a number value from the given json[key].
 
static bool GetBoolean (const nlohmann::json &json, const std::string &key, bool *value)
 Get a boolean 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 GetJsonByPath (const nlohmann::json &json, const std::vector< std::string > &paths, nlohmann::json *value)
 Get the json from the given json and path.
 
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 bool GetBooleanByPath (const nlohmann::json &json, const std::string &path, bool *value)
 Get a bool value from the given json and path.
 
template<class T >
static bool GetNumberByPath (const nlohmann::json &json, const std::string &path, T *value)
 Get a number value from the given json and path.
 

详细描述

在文件 json_util.h32 行定义.

成员函数说明

◆ GetBoolean()

bool apollo::common::util::JsonUtil::GetBoolean ( const nlohmann::json &  json,
const std::string &  key,
bool *  value 
)
static

Get a boolean value from the given json[key].

返回
Whether the field exists and is a valid boolean.

在文件 json_util.cc137 行定义.

138 {
139 const auto iter = json.find(key);
140 if (iter == json.end()) {
141 AERROR << "The json has no such key: " << key;
142 return false;
143 }
144 if (!iter->is_boolean()) {
145 AERROR << "The value of json[" << key << "] is not a boolean";
146 return false;
147 }
148 *value = *iter;
149 return true;
150}
#define AERROR
Definition log.h:44

◆ GetBooleanByPath()

bool apollo::common::util::JsonUtil::GetBooleanByPath ( const nlohmann::json &  json,
const std::string &  path,
bool *  value 
)
static

Get a bool value from the given json and path.

参数
patheg:"a.b.c" json[a][b][c]
返回
Whether the field exists and is a valid string.

在文件 json_util.cc152 行定义.

153 {
154 std::vector<std::string> paths = absl::StrSplit(path, '.');
155 std::string key = paths.back();
156 paths.pop_back();
157 Json upper_layer_json;
158 if (!GetJsonByPath(json, paths, &upper_layer_json)) {
159 return false;
160 }
161 return GetBoolean(upper_layer_json, key, value);
162}
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.
Definition json_util.cc:73
static bool GetBoolean(const nlohmann::json &json, const std::string &key, bool *value)
Get a boolean value from the given json[key].
Definition json_util.cc:137
nlohmann::json Json

◆ GetJsonByPath()

bool apollo::common::util::JsonUtil::GetJsonByPath ( const nlohmann::json &  json,
const std::vector< std::string > &  paths,
nlohmann::json *  value 
)
static

Get the json from the given json and path.

参数
patheg:"a.b.c" json[a][b][c]
返回
Whether the field exists and is a json.

在文件 json_util.cc73 行定义.

75 {
76 Json upper_layer_json = json;
77 for (auto &field : paths) {
78 if (field.empty()) {
79 AERROR << "Invalid path: " << field;
80 return false;
81 }
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;
85 return false;
86 }
87 if (!iter->is_object()) {
88 AERROR << "Required json but not,invalid type.";
89 return false;
90 }
91 upper_layer_json = *iter;
92 }
93 *value = upper_layer_json;
94 return true;
95}

◆ GetNumber()

template<class T >
static bool apollo::common::util::JsonUtil::GetNumber ( const nlohmann::json &  json,
const std::string &  key,
T *  value 
)
inlinestatic

Get a number value from the given json[key].

返回
Whether the field exists and is a valid number.

在文件 json_util.h59 行定义.

60 {
61 const auto iter = json.find(key);
62 if (iter == json.end()) {
63 AERROR << "The json has no such key: " << key;
64 return false;
65 }
66 if (!iter->is_number()) {
67 AERROR << "The value of json[" << key << "] is not a number";
68 return false;
69 }
70 *value = *iter;
71 return true;
72 }

◆ GetNumberByPath()

template<class T >
static bool apollo::common::util::JsonUtil::GetNumberByPath ( const nlohmann::json &  json,
const std::string &  path,
T *  value 
)
inlinestatic

Get a number value from the given json and path.

参数
patheg:"a.b.c" json[a][b][c]
返回
Whether the field exists and is a valid number.

在文件 json_util.h120 行定义.

121 {
122 std::vector<std::string> paths = absl::StrSplit(path, '.');
123 std::string key = paths.back();
124 paths.pop_back();
125 nlohmann::json upper_layer_json = json;
126 for (auto &field : paths) {
127 if (field.empty()) {
128 AERROR << "Invalid path: " << path;
129 return false;
130 }
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;
134 return false;
135 }
136 upper_layer_json = *iter;
137 }
138 return GetNumber(upper_layer_json, key, value);
139 }
static bool GetNumber(const nlohmann::json &json, const std::string &key, T *value)
Get a number value from the given json[key].
Definition json_util.h:59

◆ GetString()

bool apollo::common::util::JsonUtil::GetString ( const nlohmann::json &  json,
const std::string &  key,
std::string *  value 
)
static

Get a string value from the given json[key].

返回
Whether the field exists and is a valid string.

在文件 json_util.cc58 行定义.

59 {
60 const auto iter = json.find(key);
61 if (iter == json.end()) {
62 AERROR << "The json has no such key: " << key;
63 return false;
64 }
65 if (!iter->is_string()) {
66 AERROR << "The value of json[" << key << "] is not a string";
67 return false;
68 }
69 *value = *iter;
70 return true;
71}

◆ GetStringByPath()

bool apollo::common::util::JsonUtil::GetStringByPath ( const nlohmann::json &  json,
const std::string &  path,
std::string *  value 
)
static

Get a string value from the given json and path.

参数
patheg:"a.b.c" json[a][b][c]
返回
Whether the field exists and is a valid string.

在文件 json_util.cc97 行定义.

98 {
99 std::vector<std::string> paths = absl::StrSplit(path, '.');
100 std::string key = paths.back();
101 paths.pop_back();
102 Json upper_layer_json;
103 if (!GetJsonByPath(json, paths, &upper_layer_json)) {
104 return false;
105 }
106 return GetString(upper_layer_json, key, value);
107}
static bool GetString(const nlohmann::json &json, const std::string &key, std::string *value)
Get a string value from the given json[key].
Definition json_util.cc:58

◆ GetStringVector()

bool apollo::common::util::JsonUtil::GetStringVector ( const nlohmann::json &  json,
const std::string &  key,
std::vector< std::string > *  value 
)
static

Get a string vector from the given json[key].

返回
Whether the field exists and is a valid string vector.

在文件 json_util.cc109 行定义.

110 {
111 const auto iter = json.find(key);
112 if (iter == json.end()) {
113 AERROR << "The json has no such key: " << key;
114 return false;
115 }
116 if (!iter->is_array()) {
117 AERROR << "The value of json[" << key << "] is not an array";
118 return false;
119 }
120
121 bool ret = true;
122 value->clear();
123 value->reserve(iter->size());
124 for (const auto &elem : *iter) {
125 // Note that we still try to get all string values though there are invalid
126 // elements.
127 if (!elem.is_string()) {
128 AWARN << "The value of json[" << key << "] contains non-string element";
129 ret = false;
130 } else {
131 value->push_back(elem);
132 }
133 }
134 return ret;
135}
#define AWARN
Definition log.h:43

◆ ProtoToJson()

nlohmann::json apollo::common::util::JsonUtil::ProtoToJson ( const google::protobuf::Message &  proto)
static

Convert proto to a json string.

返回
A json object from a proto.

在文件 json_util.cc50 行定义.

50 {
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);
55 return json_obj;
56}

◆ ProtoToTypedJson()

nlohmann::json apollo::common::util::JsonUtil::ProtoToTypedJson ( const std::string &  json_type,
const google::protobuf::Message &  proto 
)
static

Convert proto to a json string.

返回
A json with two fields: {type:<json_type>, data:<proto_to_json>}.

在文件 json_util.cc37 行定义.

38 {
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();
43
44 Json json_obj;
45 json_obj["type"] = json_type;
46 json_obj["data"] = Json::parse(json_string);
47 return json_obj;
48}
#define ACHECK(cond)
Definition log.h:80

该类的文档由以下文件生成: