Apollo 10.0
自动驾驶开放平台
json_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include "google/protobuf/util/json_util.h"
20
21namespace apollo {
22namespace common {
23namespace util {
24namespace {
25
26using Json = nlohmann::json;
27using google::protobuf::util::MessageToJsonString;
28
29google::protobuf::util::JsonOptions JsonOption() {
30 google::protobuf::util::JsonOptions json_option;
31 json_option.always_print_primitive_fields = true;
32 return json_option;
33}
34
35} // namespace
36
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();
43
44 Json json_obj;
45 json_obj["type"] = json_type;
46 json_obj["data"] = Json::parse(json_string);
47 return json_obj;
48}
49
50nlohmann::json JsonUtil::ProtoToJson(const google::protobuf::Message &proto) {
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}
57
58bool JsonUtil::GetString(const Json &json, const std::string &key,
59 std::string *value) {
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}
72
73bool JsonUtil::GetJsonByPath(const nlohmann::json &json,
74 const std::vector<std::string> &paths,
75 nlohmann::json *value) {
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}
96
97bool JsonUtil::GetStringByPath(const Json &json, const std::string &path,
98 std::string *value) {
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}
108
109bool JsonUtil::GetStringVector(const Json &json, const std::string &key,
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;
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}
136
137bool JsonUtil::GetBoolean(const nlohmann::json &json, const std::string &key,
138 bool *value) {
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}
151
152bool JsonUtil::GetBooleanByPath(const Json &json, const std::string &path,
153 bool *value) {
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}
163
164} // namespace util
165} // namespace common
166} // namespace apollo
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].
Definition json_util.cc:109
static bool GetStringByPath(const nlohmann::json &json, const std::string &path, std::string *value)
Get a string value from the given json and path.
Definition json_util.cc:97
static nlohmann::json ProtoToJson(const google::protobuf::Message &proto)
Convert proto to a json string.
Definition json_util.cc:50
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 nlohmann::json ProtoToTypedJson(const std::string &json_type, const google::protobuf::Message &proto)
Convert proto to a json string.
Definition json_util.cc:37
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
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
static bool GetBooleanByPath(const nlohmann::json &json, const std::string &path, bool *value)
Get a bool value from the given json and path.
Definition json_util.cc:152
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
nlohmann::json Json
class register implement
Definition arena_queue.h:37