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