Apollo 10.0
自动驾驶开放平台
json_util.h
浏览该文件的文档.
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
17#pragma once
18
19#include <string>
20#include <vector>
21
22#include "google/protobuf/message.h"
23#include "nlohmann/json.hpp"
24#include "absl/strings/str_split.h"
25
26#include "cyber/common/log.h"
27
28namespace apollo {
29namespace common {
30namespace util {
31
32class JsonUtil {
33 public:
38 static nlohmann::json ProtoToTypedJson(
39 const std::string &json_type, const google::protobuf::Message &proto);
40
45 static nlohmann::json ProtoToJson(const google::protobuf::Message &proto);
46
51 static bool GetString(const nlohmann::json &json, const std::string &key,
52 std::string *value);
53
58 template <class T>
59 static bool GetNumber(const nlohmann::json &json, const std::string &key,
60 T *value) {
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 }
73
78 static bool GetBoolean(const nlohmann::json &json, const std::string &key,
79 bool *value);
80
85 static bool GetStringVector(const nlohmann::json &json,
86 const std::string &key,
87 std::vector<std::string> *value);
88
94 static bool GetJsonByPath(const nlohmann::json &json,
95 const std::vector<std::string> &paths,
96 nlohmann::json *value);
97
103 static bool GetStringByPath(const nlohmann::json &json,
104 const std::string &path, std::string *value);
105
111 static bool GetBooleanByPath(const nlohmann::json &json,
112 const std::string &path, bool *value);
113
119 template <class T>
120 static bool GetNumberByPath(const nlohmann::json &json,
121 const std::string &path, T *value) {
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 }
140};
141
142} // namespace util
143} // namespace common
144} // namespace apollo
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
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 GetNumberByPath(const nlohmann::json &json, const std::string &path, T *value)
Get a number value from the given json and path.
Definition json_util.h:120
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 AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37