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 : #include "json_utils.h"
12 : #include "log.h"
13 :
14 : namespace hccl {
15 0 : HcclResult JsonUtils::GetJsonProperty(const nlohmann::json& obj, const std::string& propName, u32& propValue)
16 : {
17 : /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
18 0 : CHK_PRT_RET(
19 : obj.find(propName) == obj.end(),
20 : HCCL_ERROR("[Get][JsonProperty]json object has no property called %s", propName.c_str()), HCCL_E_INTERNAL);
21 :
22 : /* 所有属性值都必须是字符串 */
23 0 : if (!obj[propName].is_number()) {
24 0 : HCCL_ERROR("[Get][JsonProperty]property value of Name[%s] is not number!", propName.c_str());
25 0 : return HCCL_E_INTERNAL;
26 : }
27 :
28 0 : propValue = obj[propName];
29 0 : return HCCL_SUCCESS;
30 : }
31 :
32 0 : HcclResult JsonUtils::GetJsonProperty(const nlohmann::json& obj, const std::string& propName, std::string& propValue)
33 : {
34 : /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
35 0 : CHK_PRT_RET(
36 : obj.find(propName) == obj.end(),
37 : HCCL_ERROR("[Get][JsonProperty]json object has no property called %s", propName.c_str()), HCCL_E_INTERNAL);
38 :
39 : /* 所有属性值都必须是字符串 */
40 0 : if (obj[propName].is_string()) {
41 0 : propValue = obj[propName];
42 0 : return HCCL_SUCCESS;
43 : } else {
44 0 : printf("property value of Name[%s] is not string!", propName.c_str());
45 0 : return HCCL_E_INTERNAL;
46 : }
47 : }
48 :
49 2 : HcclResult JsonUtils::GetJsonProperty(const nlohmann::json& obj, const std::string& propName, nlohmann::json& propValue)
50 : {
51 : /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
52 2 : CHK_PRT_RET(
53 : obj.find(propName) == obj.end(),
54 : HCCL_ERROR("[Get][JsonProperty]json object has no property called %s", propName.c_str()), HCCL_E_INTERNAL);
55 :
56 2 : propValue = obj[propName];
57 2 : return HCCL_SUCCESS;
58 : }
59 :
60 776 : HcclResult JsonUtils::ParseInformation(nlohmann::json& parseInformation, const std::string& information)
61 : {
62 : try {
63 776 : parseInformation = nlohmann::json::parse(information);
64 0 : } catch (...) {
65 0 : HCCL_ERROR(
66 : "[Parse][Information] errNo[0x%016llx] load allocated resource to json fail. "
67 : "please check json input!",
68 : HCOM_ERROR_CODE(HCCL_E_PARA));
69 0 : return HCCL_E_PARA;
70 0 : }
71 775 : return HCCL_SUCCESS;
72 : }
73 : } // namespace hccl
|