123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include <memory>
2023b3eb3cSopenharmony_ci#include <string>
2123b3eb3cSopenharmony_ci#include <unordered_map>
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_cinamespace OHOS {
2423b3eb3cSopenharmony_cienum class ItemType : uint8_t {
2523b3eb3cSopenharmony_ci    STRING = 0,
2623b3eb3cSopenharmony_ci    SIZE_T,
2723b3eb3cSopenharmony_ci    INT32,
2823b3eb3cSopenharmony_ci    INT64,
2923b3eb3cSopenharmony_ci    DOUBLE,
3023b3eb3cSopenharmony_ci    BOOL,
3123b3eb3cSopenharmony_ci    UOBJECT,
3223b3eb3cSopenharmony_ci};
3323b3eb3cSopenharmony_ci
3423b3eb3cSopenharmony_ciclass UObject {
3523b3eb3cSopenharmony_cipublic:
3623b3eb3cSopenharmony_ci    UObject() = default;
3723b3eb3cSopenharmony_ci    ~UObject() = default;
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, const char* value);
4023b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, const std::string& value);
4123b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, size_t value);
4223b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, int32_t value);
4323b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, int64_t value);
4423b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, double value);
4523b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, bool value);
4623b3eb3cSopenharmony_ci    void AddItemToObject(const std::string& key, const std::shared_ptr<UObject>& value);
4723b3eb3cSopenharmony_ci
4823b3eb3cSopenharmony_ci    std::string GetString(const std::string& key) const;
4923b3eb3cSopenharmony_ci    size_t GetSizeT(const std::string& key) const;
5023b3eb3cSopenharmony_ci    int32_t GetInt32(const std::string& key) const;
5123b3eb3cSopenharmony_ci    int64_t GetInt64(const std::string& key) const;
5223b3eb3cSopenharmony_ci    double GetDouble(const std::string& key) const;
5323b3eb3cSopenharmony_ci    bool GetBool(const std::string& key) const;
5423b3eb3cSopenharmony_ci    std::shared_ptr<UObject> GetObject(const std::string& key) const;
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ci    bool Contains(const std::string& key) const;
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ci    void Serialize(char* buffer, int32_t bufferLen);
5923b3eb3cSopenharmony_ci    void Deserialize(const char* buffer, int32_t bufferLen);
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    size_t Hash();
6223b3eb3cSopenharmony_ci
6323b3eb3cSopenharmony_ci    int32_t EstimateBufferSize();
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ciprivate:
6623b3eb3cSopenharmony_ci    void WriteChar(char value);
6723b3eb3cSopenharmony_ci    void WriteInt32(int32_t value);
6823b3eb3cSopenharmony_ci    void WriteInt64(int64_t value);
6923b3eb3cSopenharmony_ci    void WriteSizeT(size_t value);
7023b3eb3cSopenharmony_ci    void WriteDouble(double value);
7123b3eb3cSopenharmony_ci    void WriteString(const std::string& value);
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, const std::string& value);
7423b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, size_t value);
7523b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, int32_t value);
7623b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, int64_t value);
7723b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, double value);
7823b3eb3cSopenharmony_ci    void WriteKV(const std::string& key, bool value);
7923b3eb3cSopenharmony_ci    void WriteObj(const std::string& key, const std::shared_ptr<UObject>& obj);
8023b3eb3cSopenharmony_ci
8123b3eb3cSopenharmony_ci    char ReadChar();
8223b3eb3cSopenharmony_ci    int32_t ReadInt32();
8323b3eb3cSopenharmony_ci    int64_t ReadInt64();
8423b3eb3cSopenharmony_ci    size_t ReadSizeT();
8523b3eb3cSopenharmony_ci    double ReadDouble();
8623b3eb3cSopenharmony_ci    std::string ReadString(int32_t len);
8723b3eb3cSopenharmony_ci    std::shared_ptr<UObject> ReadObj(int32_t len);
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_ci    std::string ReadKey();
9023b3eb3cSopenharmony_ci    void ReadKV();
9123b3eb3cSopenharmony_ci
9223b3eb3cSopenharmony_ci    char* buffer_ = nullptr;
9323b3eb3cSopenharmony_ci    const char* constBuffer_ = nullptr;
9423b3eb3cSopenharmony_ci    int32_t bufferLen_ = 0;
9523b3eb3cSopenharmony_ci    int32_t offset_ = 0;
9623b3eb3cSopenharmony_ci    size_t hashValue_ = 0;
9723b3eb3cSopenharmony_ci    std::unordered_map<std::string, std::string> stringItems_;
9823b3eb3cSopenharmony_ci    std::unordered_map<std::string, size_t> sizetItems_;
9923b3eb3cSopenharmony_ci    std::unordered_map<std::string, int32_t> int32Items_;
10023b3eb3cSopenharmony_ci    std::unordered_map<std::string, int64_t> int64Items_;
10123b3eb3cSopenharmony_ci    std::unordered_map<std::string, double> doubleItems_;
10223b3eb3cSopenharmony_ci    std::unordered_map<std::string, bool> boolItems_;
10323b3eb3cSopenharmony_ci    std::unordered_map<std::string, std::shared_ptr<UObject>> children_;
10423b3eb3cSopenharmony_ci};
10523b3eb3cSopenharmony_ci} // namespace OHOS
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_UOBJECT_H
108