1 /**
2 * Copyright 2021-2022 Huawei Technologies Co., Ltd
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 #ifndef MINDSPORE_INCLUDE_COMMON_UTILS_JSON_OPERATION_UTILS_H
18 #define MINDSPORE_INCLUDE_COMMON_UTILS_JSON_OPERATION_UTILS_H
19 #include <string>
20 #include <vector>
21 #include <nlohmann/json.hpp>
22 #include "ir/dtype.h"
23 #include "include/common/visible.h"
24
25 namespace mindspore {
26
27 template <typename T>
GetJsonValue(const nlohmann::json & json,const std::string & key)28 T GetJsonValue(const nlohmann::json &json, const std::string &key) {
29 auto obj_json = json.find(key);
30 if (obj_json != json.end()) {
31 try {
32 T value = obj_json.value();
33 return value;
34 } catch (std::exception &e) {
35 MS_LOG(ERROR) << "Get Json Value Error, error info: " << e.what();
36 MS_LOG(EXCEPTION) << "Get Json Value Error, target type: " << typeid(T).name() << ", key: [" << key << "]"
37 << ", json dump: " << json.dump();
38 }
39 } else {
40 MS_LOG(EXCEPTION) << "Get Json Value Error, can not find key [" << key << "], json dump: " << json.dump();
41 }
42 }
43
44 COMMON_EXPORT bool ParseJson(const std::string &str, nlohmann::json *des_json);
45
46 } // namespace mindspore
47 #endif // MINDSPORE_INCLUDE_COMMON_UTILS_JSON_OPERATION_UTILS_H
48