Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_JSON_PARSER_H
12 : #define HCCLV2_JSON_PARSER_H
13 :
14 : #include <vector>
15 : #include <string>
16 : #include <linux/limits.h>
17 : #include <iostream>
18 : #include <fstream>
19 : #include "nlohmann/json.hpp"
20 : #include "exception_util.h"
21 : #include "internal_exception.h"
22 : #include "invalid_params_exception.h"
23 :
24 : namespace Hccl {
25 :
26 : std::string GetJsonProperty(const nlohmann::json &obj, const char *propName, bool required = true);
27 : u32 GetJsonPropertyUInt(const nlohmann::json &obj, const char *propName, bool required = true, u32 defaultValue = 0);
28 : s32 GetJsonPropertySInt(const nlohmann::json &obj, const char *propName, bool required = true, s32 defaultValue = 0);
29 : void GetJsonPropertyList(const nlohmann::json &obj, const char *propName, nlohmann::json &listObj);
30 :
31 : class JsonParser {
32 : public:
33 : template <typename T> void ParseString(const std::string &jsonString, T &info) const;
34 : template <typename T> void ParseFile(const std::string &filePath, T &info) const;
35 : template <typename T> void GetString(const T &info, std::string &infoStr) const;
36 : void ParseFileToJson(const std::string &filePath, nlohmann::json &parseInformation) const;
37 :
38 : private:
39 : template <typename T> void ParseInformation(nlohmann::json &parseInformation, T &information) const;
40 : };
41 :
42 82 : template <typename T> void JsonParser::ParseString(const std::string &jsonString, T &info) const
43 : {
44 82 : if (jsonString.empty()) {
45 0 : THROW<InvalidParamsException>(StringFormat("[Load][JsonString]errNo[0x%016llx] json string length is zero",
46 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
47 : }
48 82 : HCCL_INFO("waiting for json string load complete");
49 :
50 82 : nlohmann::json json;
51 82 : ParseInformation(json, jsonString);
52 :
53 : try {
54 81 : info.Deserialize(json);
55 58 : } catch (nlohmann::json::exception &e) {
56 0 : THROW<InvalidParamsException>(e.what());
57 : }
58 82 : }
59 :
60 0 : template <typename T> void JsonParser::ParseFile(const std::string &filePath, T &info) const
61 : {
62 : // 校验文件是否存在
63 0 : char resolvedPath[PATH_MAX] = {0};
64 0 : if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
65 0 : THROW<InvalidParamsException>(
66 0 : StringFormat("[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
67 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
68 : }
69 :
70 0 : HCCL_INFO("waiting for json file load complete");
71 0 : std::ifstream infoFile(resolvedPath, std::ifstream::in);
72 0 : if (!infoFile) {
73 0 : THROW<InternalException>(StringFormat("[Read][File]errNo[0x%016llx],open file %s failed",
74 : HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE), resolvedPath));
75 : }
76 :
77 0 : nlohmann::json json;
78 0 : ParseInformation(json, infoFile);
79 0 : infoFile.close();
80 :
81 : try {
82 0 : info.Deserialize(json);
83 0 : } catch (nlohmann::json::exception &e) {
84 0 : THROW<InvalidParamsException>(e.what());
85 : }
86 0 : }
87 :
88 : template <typename T> void JsonParser::GetString(const T &info, std::string &infoStr) const
89 : {
90 : nlohmann::json json;
91 : info.Serialize(json);
92 : infoStr = json.dump();
93 : }
94 :
95 82 : template <typename T> void JsonParser::ParseInformation(nlohmann::json &parseInformation, T &information) const
96 : {
97 : try {
98 83 : parseInformation = nlohmann::json::parse(information);
99 2 : } catch (const nlohmann::json::parse_error &e) {
100 1 : HCCL_ERROR("JSON parse error: %s at byte %d", e.what(), e.byte);
101 2 : THROW<InvalidParamsException>(StringFormat("[Parse][Information] errNo[0x%016llx] load allocated resource to "
102 : "json fail. JSON parse error: %s at byte %d"
103 : "please check json input!",
104 1 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), e.what(), e.byte));
105 0 : } catch (const nlohmann::json::exception &e) {
106 0 : HCCL_ERROR("JSON parse error: %s", e.what());
107 0 : THROW<InvalidParamsException>(StringFormat(
108 : "[Parse][Information] errNo[0x%016llx] load allocated resource to json fail. JSON parse error: %s"
109 : "please check json input!",
110 0 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), e.what()));
111 0 : } catch (...) {
112 0 : THROW<InvalidParamsException>(
113 0 : StringFormat("[Parse][Information] errNo[0x%016llx] load allocated resource to json fail. "
114 : "please check json input!",
115 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
116 : };
117 81 : }
118 :
119 : } // namespace Hccl
120 : #endif
|