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
22namespace OHOS {
23namespace HiviewDFX {
24using KV = std::pair<std::string, std::string>;
25using PrintKvHandler = std::function<std::string(KV&)>;
26class JsonFlattenParser {
27public:
28    JsonFlattenParser(const std::string& json);
29
30public:
31    static void Initialize();
32
33public:
34    void Parse(const std::string& json);
35    std::string Print(PrintKvHandler handler);
36
37public:
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
43private:
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
50private:
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