Apollo  6.0
Open source self driving car software
json_util.h
Go to the documentation of this file.
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 
25 #include "cyber/common/log.h"
26 
27 namespace apollo {
28 namespace common {
29 namespace util {
30 
31 class JsonUtil {
32  public:
37  static nlohmann::json ProtoToTypedJson(
38  const std::string &json_type, const google::protobuf::Message &proto);
39 
44  static bool GetString(const nlohmann::json &json, const std::string &key,
45  std::string *value);
46 
51  template <class T>
52  static bool GetNumber(const nlohmann::json &json, const std::string &key,
53  T *value) {
54  const auto iter = json.find(key);
55  if (iter == json.end()) {
56  AERROR << "The json has no such key: " << key;
57  return false;
58  }
59  if (!iter->is_number()) {
60  AERROR << "The value of json[" << key << "] is not a number";
61  return false;
62  }
63  *value = *iter;
64  return true;
65  }
66 
71  static bool GetBoolean(const nlohmann::json &json, const std::string &key,
72  bool *value);
73 
78  static bool GetStringVector(const nlohmann::json &json,
79  const std::string &key,
80  std::vector<std::string> *value);
81 };
82 
83 } // namespace util
84 } // namespace common
85 } // 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:52
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static nlohmann::json ProtoToTypedJson(const std::string &json_type, 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].
Definition: json_util.h:31
static bool GetBoolean(const nlohmann::json &json, const std::string &key, bool *value)
Get a boolean value from the given json[key].
apollo::cyber::base::std value
#define AERROR
Definition: log.h:44
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].