1 /* 2 * Copyright (c) 2023 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 BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_BUILDER_JSON_PARSER_H 17 #define BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_BUILDER_JSON_PARSER_H 18 19 #include "base/raw_data_base_def.h" 20 21 #include <string> 22 #include <vector> 23 24 #include "encoded/raw_data_builder.h" 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 namespace EventRaw { 29 class RawDataBuilderJsonParser { 30 public: 31 RawDataBuilderJsonParser(const std::string& jsonStr); 32 33 public: 34 std::shared_ptr<RawDataBuilder> Parse(); 35 36 private: 37 static constexpr int CHAR_RANGE = 256; 38 static constexpr int STATUS_NUM = 13; 39 40 static constexpr int STATUS_NONE = 0; 41 static constexpr int STATUS_RUN = 1; 42 static constexpr int STATUS_KEY_PARSE = 2; 43 static constexpr int STATUS_VALUE_PARSE = 3; 44 static constexpr int STATUS_STRING_PARSE = 4; 45 static constexpr int STATUS_DOUBLE_PARSE = 5; 46 static constexpr int STATUS_INT_PARSE = 6; 47 static constexpr int STATUS_ARRAY_PARSE = 7; 48 static constexpr int STATUS_STRING_ITEM_PARSE = 8; 49 static constexpr int STATUS_DOUBLE_ITEM_PARSE = 9; 50 static constexpr int STATUS_INT_ITEM_PARSE = 10; 51 static constexpr int STATUS_ESCAPE_CHAR_PARSE = 11; 52 static constexpr int STATUS_ESCAPE_CHAR_ITEM_PARSE = 12; 53 54 private: 55 void HandleStatusNone(); 56 void HandleStatusKeyParse(); 57 void HandleStatusRun(); 58 void HandleStatusValueParse(); 59 void HandleStatusArrayParse(); 60 void HandleStatusStringParse(); 61 void HandleStatusStringItemParse(); 62 void HandleStatusValueAppend(); 63 void AppendValueToBuilder(); 64 65 private: 66 void BuilderAppendStringValue(const std::string& key, const std::string& value); 67 void BuilderAppendIntValue(const std::string& key, const std::string& value); 68 void BuilderAppendFloatingValue(const std::string& key, const std::string& value); 69 void BuilderAppendStringArrayValue(const std::string& key, const std::vector<std::string>& values); 70 void BuilderAppendIntArrayValue(const std::string& key, const std::vector<std::string>& values); 71 void BuilderAppendFloatingArrayValue(const std::string& key, const std::vector<std::string>& values); 72 73 private: 74 void InitNoneStatus(); 75 void InitRunStatus(); 76 void InitKeyParseStatus(); 77 void InitValueParseStatus(); 78 void InitStringParseStatus(); 79 void InitDoubleParseStatus(); 80 void InitIntParseStatus(); 81 void InitArrayParseSatus(); 82 void InitStringItemParseStatus(); 83 void InitDoubleItemParseStatus(); 84 void InitIntItemParseStatus(); 85 void InitEscapeCharParseStatus(); 86 void InitEscapeCharItemParseStatus(); 87 void InitParamHandlers(std::unordered_map<int, std::function<void()>>& handlers); 88 89 private: 90 std::string jsonStr_; 91 std::shared_ptr<RawDataBuilder> builder_ = nullptr; 92 93 private: 94 int statusTabs_[STATUS_NUM][CHAR_RANGE]; 95 int status_ = STATUS_NONE; 96 int lastStatus_ = STATUS_NONE; 97 int lastValueParseStatus_ = STATUS_NONE; 98 std::string key_; 99 std::string value_; 100 std::vector<std::string> values_; 101 int charactor_; 102 }; 103 } // namespace EventRaw 104 } // namespace HiviewDFX 105 } // namespace OHOS 106 107 #endif // BASE_EVENT_RAW_ENCODE_INCLUDE_RAW_DATA_BUILDER_JSON_PARSER_H