• 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 HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_FLAT_JSON_PARSER_H
17 #define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_FLAT_JSON_PARSER_H
18 
19 #include <vector>
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 class FlatJsonParser {
24 public:
25     FlatJsonParser(const std::string& jsonStr);
26 
27 public:
28     static void Initialize();
29 
30 public:
31     void AppendStringValue(const std::string& key, const std::string& value);
32     void AppendUInt64Value(const std::string& key, const uint64_t value);
33     void Parse(const std::string& jsonStr);
34     std::string Print();
35 
36 public:
37     static constexpr int CHAR_RANGE { 256 };
38     static constexpr uint8_t NUMBER_FLAG { 1 };
39     static constexpr uint8_t STRING_FLAG { 2 };
40     static constexpr uint8_t BRACKET_FLAG { 3 };
41 
42 private:
43     std::string ParseKey(const std::string& jsonStr);
44     std::string ParseValue(const std::string& jsonStr);
45     std::string ParseNumer(const std::string& jsonStr);
46     std::string ParseString(const std::string& jsonStr);
47     std::string ParseBrackets(const std::string& jsonStr, char leftBracket);
48 
49 private:
50     static uint8_t charFilter_[CHAR_RANGE];
51 
52 private:
53     std::vector<std::pair<std::string, std::string>> kvList_;
54     size_t index_ {0};
55 };
56 } // namespace HiviewDFX
57 } // namespace OHOS
58 
59 #endif // HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_FLAT_JSON_PARSER_H
60