• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 JSON_PARSER_H
17 # define JSON_PARSER_H
18 
19 #include <cstdint>
20 #include <cmath>
21 #include <string>
22 
23 #include "cJSON.h"
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 class JsonParser {
28 public:
29     explicit JsonParser(const char *jsonStr);
30     ~JsonParser();
31     JsonParser(const JsonParser&) = delete;
32     JsonParser& operator=(const JsonParser&) = delete;
33     JsonParser(JsonParser&& other) noexcept;
34     JsonParser& operator=(JsonParser&& other) noexcept;
35     const cJSON* Get() const;
36 
37 private:
38     cJSON* json_ { nullptr };
39 
40 private:
41     static bool IsInteger(const cJSON *json);
42 
43 public:
44     static int32_t ParseInt32(const cJSON *json, const std::string &key, int32_t &value);
45     static int32_t ParseString(const cJSON *json, const std::string &key, std::string &value);
46     static int32_t ParseBool(const cJSON *json, const std::string &key, bool &value);
47     static int32_t ParseStringArray(const cJSON *json, const std::string &key, std::vector<std::string> &value,
48         int32_t maxSize);
49 };
50 } // namespace DeviceStatus
51 } // namespace Msdp
52 } // namespace OHOS
53 # endif //JSON_PARSER_H