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 FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 namespace OHOS {
24 enum class ItemType : uint8_t {
25     STRING = 0,
26     SIZE_T,
27     INT32,
28     INT64,
29     DOUBLE,
30     BOOL,
31     UOBJECT,
32 };
33 
34 class UObject {
35 public:
36     UObject() = default;
37     ~UObject() = default;
38 
39     void AddItemToObject(const std::string& key, const char* value);
40     void AddItemToObject(const std::string& key, const std::string& value);
41     void AddItemToObject(const std::string& key, size_t value);
42     void AddItemToObject(const std::string& key, int32_t value);
43     void AddItemToObject(const std::string& key, int64_t value);
44     void AddItemToObject(const std::string& key, double value);
45     void AddItemToObject(const std::string& key, bool value);
46     void AddItemToObject(const std::string& key, const std::shared_ptr<UObject>& value);
47 
48     std::string GetString(const std::string& key) const;
49     size_t GetSizeT(const std::string& key) const;
50     int32_t GetInt32(const std::string& key) const;
51     int64_t GetInt64(const std::string& key) const;
52     double GetDouble(const std::string& key) const;
53     bool GetBool(const std::string& key) const;
54     std::shared_ptr<UObject> GetObject(const std::string& key) const;
55 
56     bool Contains(const std::string& key) const;
57 
58     void Serialize(char* buffer, int32_t bufferLen);
59     void Deserialize(const char* buffer, int32_t bufferLen);
60 
61     size_t Hash();
62 
63     int32_t EstimateBufferSize();
64 
65 private:
66     void WriteChar(char value);
67     void WriteInt32(int32_t value);
68     void WriteInt64(int64_t value);
69     void WriteSizeT(size_t value);
70     void WriteDouble(double value);
71     void WriteString(const std::string& value);
72 
73     void WriteKV(const std::string& key, const std::string& value);
74     void WriteKV(const std::string& key, size_t value);
75     void WriteKV(const std::string& key, int32_t value);
76     void WriteKV(const std::string& key, int64_t value);
77     void WriteKV(const std::string& key, double value);
78     void WriteKV(const std::string& key, bool value);
79     void WriteObj(const std::string& key, const std::shared_ptr<UObject>& obj);
80 
81     char ReadChar();
82     int32_t ReadInt32();
83     int64_t ReadInt64();
84     size_t ReadSizeT();
85     double ReadDouble();
86     std::string ReadString(int32_t len);
87     std::shared_ptr<UObject> ReadObj(int32_t len);
88 
89     std::string ReadKey();
90     void ReadKV();
91 
92     char* buffer_ = nullptr;
93     const char* constBuffer_ = nullptr;
94     int32_t bufferLen_ = 0;
95     int32_t offset_ = 0;
96     size_t hashValue_ = 0;
97     std::unordered_map<std::string, std::string> stringItems_;
98     std::unordered_map<std::string, size_t> sizetItems_;
99     std::unordered_map<std::string, int32_t> int32Items_;
100     std::unordered_map<std::string, int64_t> int64Items_;
101     std::unordered_map<std::string, double> doubleItems_;
102     std::unordered_map<std::string, bool> boolItems_;
103     std::unordered_map<std::string, std::shared_ptr<UObject>> children_;
104 };
105 } // namespace OHOS
106 
107 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
108