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