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 ADUMP_JSON_PARSER_H
12 : #define ADUMP_JSON_PARSER_H
13 :
14 : #include "nlohmann/json.hpp"
15 :
16 : namespace Adx {
17 : class JsonParser {
18 : public:
19 : static int32_t ParseJsonFromMemory(const char *dumpConfigData, size_t dumpConfigSize,
20 : nlohmann::json &js, std::string &errMsg);
21 : static const nlohmann::json& GetCfgJsonByKey(const nlohmann::json &js, const std::string &key) {
22 : return js.at(key);
23 : }
24 15781 : static bool ContainKey(const nlohmann::json &js, const std::string &key) {
25 15781 : return js.contains(key);
26 : }
27 513 : static std::string GetCfgStrByKey(const nlohmann::json &js , const std::string &key) {
28 513 : return js.at(key).get<std::string>();
29 : }
30 :
31 6654 : static bool GetStringIfExist(const nlohmann::json &js, const std::string &key, std::string &value) {
32 6654 : auto it = js.find(key);
33 6654 : if (it == js.end()) {
34 4362 : return false;
35 : }
36 2292 : if (it->is_string()) {
37 2292 : value = it->get<std::string>();
38 2292 : return true;
39 : }
40 0 : return false;
41 : }
42 :
43 3195 : static std::string GetStringOrDefault(const nlohmann::json &js, const std::string &key,
44 : const std::string &defaultValue) {
45 3195 : auto it = js.find(key);
46 3195 : if (it == js.end()) {
47 2568 : return defaultValue;
48 : }
49 627 : if (it->is_string()) {
50 627 : std::string value = it->get<std::string>();
51 627 : return value.empty() ? defaultValue : value;
52 627 : }
53 0 : return defaultValue;
54 : }
55 :
56 : private:
57 : static void GetMaxNestedLayers(const char *const fileName, const size_t length,
58 : size_t &maxObjDepth, size_t &maxArrayDepth);
59 : static bool IsValidFileName(const char *const fileName);
60 :
61 : static bool ParseJson(const char *const fileName, nlohmann::json &js, const size_t fileLength);
62 : };
63 : }
64 : #endif
|