LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/misc/json_parser - json_parser.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 23.1 % 13 3
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 1 1

            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>
      34              :     void ParseString(const std::string& jsonString, T& info) const;
      35              :     template <typename T>
      36              :     void ParseFile(const std::string& filePath, T& info) const;
      37              :     template <typename T>
      38              :     void GetString(const T& info, std::string& infoStr) const;
      39              :     void ParseFileToJson(const std::string& filePath, nlohmann::json& parseInformation) const;
      40              : 
      41              : private:
      42              :     template <typename T>
      43              :     void ParseInformation(nlohmann::json& parseInformation, T& information) const;
      44              : };
      45              : 
      46              : template <typename T>
      47              : void JsonParser::ParseString(const std::string& jsonString, T& info) const
      48              : {
      49              :     if (jsonString.empty()) {
      50              :         THROW<InvalidParamsException>(StringFormat(
      51              :             "[Load][JsonString]errNo[0x%016llx] json string length is zero", HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
      52              :     }
      53              :     HCCL_INFO("waiting for json string load complete");
      54              : 
      55              :     nlohmann::json json;
      56              :     ParseInformation(json, jsonString);
      57              : 
      58              :     try {
      59              :         info.Deserialize(json);
      60              :     } catch (nlohmann::json::exception& e) {
      61              :         THROW<InvalidParamsException>(e.what());
      62              :     }
      63              : }
      64              : 
      65              : template <typename T>
      66              : void JsonParser::ParseFile(const std::string& filePath, T& info) const
      67              : {
      68              :     // 校验文件是否存在
      69              :     char resolvedPath[PATH_MAX] = {0};
      70              :     if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
      71              :         THROW<InvalidParamsException>(StringFormat(
      72              :             "[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
      73              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
      74              :     }
      75              : 
      76              :     HCCL_INFO("waiting for json file load complete");
      77              :     std::ifstream infoFile(resolvedPath, std::ifstream::in);
      78              :     if (!infoFile) {
      79              :         THROW<InternalException>(StringFormat(
      80              :             "[Read][File]errNo[0x%016llx],open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE),
      81              :             resolvedPath));
      82              :     }
      83              : 
      84              :     nlohmann::json json;
      85              :     ParseInformation(json, infoFile);
      86              :     infoFile.close();
      87              : 
      88              :     try {
      89              :         info.Deserialize(json);
      90              :     } catch (nlohmann::json::exception& e) {
      91              :         THROW<InvalidParamsException>(e.what());
      92              :     }
      93              : }
      94              : 
      95              : template <typename T>
      96              : void JsonParser::GetString(const T& info, std::string& infoStr) const
      97              : {
      98              :     nlohmann::json json;
      99              :     info.Serialize(json);
     100              :     infoStr = json.dump();
     101              : }
     102              : 
     103              : template <typename T>
     104            4 : void JsonParser::ParseInformation(nlohmann::json& parseInformation, T& information) const
     105              : {
     106              :     try {
     107            4 :         parseInformation = nlohmann::json::parse(information);
     108            0 :     } catch (const nlohmann::json::parse_error& e) {
     109            0 :         HCCL_ERROR("JSON parse error: %s at byte %d", e.what(), e.byte);
     110            0 :         THROW<InvalidParamsException>(StringFormat(
     111              :             "[Parse][Information] errNo[0x%016llx] load allocated resource to "
     112              :             "json fail. JSON parse error: %s at byte %d"
     113              :             "please check json input!",
     114            0 :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), e.what(), e.byte));
     115            0 :     } catch (const nlohmann::json::exception& e) {
     116            0 :         HCCL_ERROR("JSON parse error: %s", e.what());
     117            0 :         THROW<InvalidParamsException>(StringFormat(
     118              :             "[Parse][Information] errNo[0x%016llx] load allocated resource to json fail. JSON parse error: %s"
     119              :             "please check json input!",
     120            0 :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), e.what()));
     121            0 :     } catch (...) {
     122            0 :         THROW<InvalidParamsException>(StringFormat(
     123              :             "[Parse][Information] errNo[0x%016llx] load allocated resource to json fail. "
     124              :             "please check json input!",
     125              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA)));
     126              :     };
     127            4 : }
     128              : 
     129              : } // namespace Hccl
     130              : #endif
        

Generated by: LCOV version 2.0-1