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