• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HISYSEVENT_FRAMEWORKS_NATIVE_INCLUDE_JSON_FLATTEN_PARSER_H
17 #define HISYSEVENT_FRAMEWORKS_NATIVE_INCLUDE_JSON_FLATTEN_PARSER_H
18 
19 #include <functional>
20 #include <vector>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 using KV = std::pair<std::string, std::string>;
25 using PrintKvHandler = std::function<std::string(KV&)>;
26 class JsonFlattenParser {
27 public:
28     JsonFlattenParser(const std::string& json);
29 
30 public:
31     static void Initialize();
32 
33 public:
34     void Parse(const std::string& json);
35     std::string Print(PrintKvHandler handler);
36 
37 public:
38     static constexpr uint8_t BRACKET_FLAG { 3 };
39     static constexpr uint8_t NUMBER_FLAG { 1 };
40     static constexpr uint8_t STRING_FLAG { 2 };
41     static constexpr int CHAR_RANGE { 256 };
42 
43 private:
44     std::string ParseBrackets(const std::string& json, char leftBracket);
45     std::string ParseKey(const std::string& json);
46     std::string ParseNumer(const std::string& json);
47     std::string ParseString(const std::string& json);
48     std::string ParseValue(const std::string& json);
49 
50 private:
51     static uint8_t charFilter[CHAR_RANGE];
52     std::vector<KV> kvList;
53     size_t curPos {0};
54 };
55 } // namespace HiviewDFX
56 } // namespace OHOS
57 
58 #endif // HISYSEVENT_FRAMEWORKS_NATIVE_INCLUDE_JSON_FLATTEN_PARSER_H
59