xref: /ide/tools/previewer/util/JsonReader.h (revision 7c804472)
17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#ifndef JSONREADER_H
177c804472Sopenharmony_ci#define JSONREADER_H
187c804472Sopenharmony_ci
197c804472Sopenharmony_ci#include <string>
207c804472Sopenharmony_ci#include <memory>
217c804472Sopenharmony_ci#include <vector>
227c804472Sopenharmony_ci#include <unordered_map>
237c804472Sopenharmony_ci#include <variant>
247c804472Sopenharmony_ci#include <optional>
257c804472Sopenharmony_ci
267c804472Sopenharmony_cistruct cJSON;
277c804472Sopenharmony_ci
287c804472Sopenharmony_cinamespace Json2 {
297c804472Sopenharmony_ci    class Value {
307c804472Sopenharmony_ci    public:
317c804472Sopenharmony_ci        Value() = default;
327c804472Sopenharmony_ci        explicit Value(cJSON* object);
337c804472Sopenharmony_ci        Value(cJSON* object, bool isRoot);
347c804472Sopenharmony_ci        ~Value();
357c804472Sopenharmony_ci        // 重载实现obj["key"]形式调用
367c804472Sopenharmony_ci        Value operator[](const char* key);
377c804472Sopenharmony_ci        const Value operator[](const char* key) const;
387c804472Sopenharmony_ci        Value operator[](const std::string& key);
397c804472Sopenharmony_ci        const Value operator[](const std::string& key) const;
407c804472Sopenharmony_ci        // 获取所有成员键值
417c804472Sopenharmony_ci        using Members = std::vector<std::string>;
427c804472Sopenharmony_ci        Value::Members GetMemberNames() const;
437c804472Sopenharmony_ci        // convert string functions
447c804472Sopenharmony_ci        std::string ToString() const;
457c804472Sopenharmony_ci        std::string ToStyledString() const;
467c804472Sopenharmony_ci        const cJSON* GetJsonPtr() const;
477c804472Sopenharmony_ci        // check functions
487c804472Sopenharmony_ci        bool IsNull() const;
497c804472Sopenharmony_ci        bool IsValid() const;
507c804472Sopenharmony_ci        bool IsNumber() const;
517c804472Sopenharmony_ci        bool IsInt() const;
527c804472Sopenharmony_ci        bool IsUInt() const;
537c804472Sopenharmony_ci        bool IsInt64() const;
547c804472Sopenharmony_ci        bool IsUInt64() const;
557c804472Sopenharmony_ci        bool IsDouble() const;
567c804472Sopenharmony_ci        bool IsBool() const;
577c804472Sopenharmony_ci        bool IsString() const;
587c804472Sopenharmony_ci        bool IsObject() const;
597c804472Sopenharmony_ci        bool IsArray() const;
607c804472Sopenharmony_ci        bool IsMember(const char* key) const;
617c804472Sopenharmony_ci        // get functions
627c804472Sopenharmony_ci        int32_t GetInt(const char* key, int32_t defaultVal = 0) const;
637c804472Sopenharmony_ci        uint32_t GetUInt(const char* key, int32_t defaultVal = 0) const;
647c804472Sopenharmony_ci        int64_t GetInt64(const char* key, int32_t defaultVal = 0) const;
657c804472Sopenharmony_ci        float GetFloat(const char* key, float defaultVal = 0.0) const;
667c804472Sopenharmony_ci        double GetDouble(const char* key, double defaultVal = 0.0) const;
677c804472Sopenharmony_ci        bool GetBool(const char* key, bool defaultVal = false) const;
687c804472Sopenharmony_ci        std::string GetString(const char* key, const std::string defaultVal = "") const;
697c804472Sopenharmony_ci        Value GetValue(const char* key) const;
707c804472Sopenharmony_ci        int32_t AsInt() const;
717c804472Sopenharmony_ci        uint32_t AsUInt() const;
727c804472Sopenharmony_ci        int64_t AsInt64() const;
737c804472Sopenharmony_ci        float AsFloat() const;
747c804472Sopenharmony_ci        double AsDouble() const;
757c804472Sopenharmony_ci        bool AsBool() const;
767c804472Sopenharmony_ci        std::string AsString() const;
777c804472Sopenharmony_ci        // add functions for obj
787c804472Sopenharmony_ci        bool Add(const char* key, const char* value);
797c804472Sopenharmony_ci        bool Add(const char* key, bool value);
807c804472Sopenharmony_ci        bool Add(const char* key, int32_t value);
817c804472Sopenharmony_ci        bool Add(const char* key, uint32_t value);
827c804472Sopenharmony_ci        bool Add(const char* key, int64_t value);
837c804472Sopenharmony_ci        bool Add(const char* key, double value);
847c804472Sopenharmony_ci        bool Add(const char* key, const Value& value);
857c804472Sopenharmony_ci        // add functions for array
867c804472Sopenharmony_ci        bool Add(const char* value);
877c804472Sopenharmony_ci        bool Add(bool value);
887c804472Sopenharmony_ci        bool Add(int32_t value);
897c804472Sopenharmony_ci        bool Add(uint32_t value);
907c804472Sopenharmony_ci        bool Add(int64_t value);
917c804472Sopenharmony_ci        bool Add(double value);
927c804472Sopenharmony_ci        bool Add(const Value& value);
937c804472Sopenharmony_ci        // replace functions for obj
947c804472Sopenharmony_ci        bool Replace(const char* key, bool value);
957c804472Sopenharmony_ci        bool Replace(const char* key, int32_t value);
967c804472Sopenharmony_ci        bool Replace(const char* key, uint32_t value);
977c804472Sopenharmony_ci        bool Replace(const char* key, int64_t value);
987c804472Sopenharmony_ci        bool Replace(const char* key, double value);
997c804472Sopenharmony_ci        bool Replace(const char* key, const char* value);
1007c804472Sopenharmony_ci        bool Replace(const char* key, const Value& value);
1017c804472Sopenharmony_ci        // replace functions for array
1027c804472Sopenharmony_ci        bool Replace(int index, bool value);
1037c804472Sopenharmony_ci        bool Replace(int index, int32_t value);
1047c804472Sopenharmony_ci        bool Replace(int index, uint32_t value);
1057c804472Sopenharmony_ci        bool Replace(int index, int64_t value);
1067c804472Sopenharmony_ci        bool Replace(int index, double value);
1077c804472Sopenharmony_ci        bool Replace(int index, const char* value);
1087c804472Sopenharmony_ci        bool Replace(int index, const Value& value);
1097c804472Sopenharmony_ci        // array functions
1107c804472Sopenharmony_ci        uint32_t GetArraySize() const;
1117c804472Sopenharmony_ci        Value GetArrayItem(int32_t index) const;
1127c804472Sopenharmony_ci        // empty object
1137c804472Sopenharmony_ci        void Clear();
1147c804472Sopenharmony_ci        std::string GetKey();
1157c804472Sopenharmony_ci
1167c804472Sopenharmony_ci    private:
1177c804472Sopenharmony_ci        cJSON* jsonPtr = nullptr;
1187c804472Sopenharmony_ci        bool rootNode = true;
1197c804472Sopenharmony_ci    };
1207c804472Sopenharmony_ci}
1217c804472Sopenharmony_ci
1227c804472Sopenharmony_ciclass JsonReader {
1237c804472Sopenharmony_cipublic:
1247c804472Sopenharmony_ci    static std::string ReadFile(const std::string& path);
1257c804472Sopenharmony_ci    static Json2::Value ParseJsonData2(const std::string& jsonStr);
1267c804472Sopenharmony_ci    static std::string GetErrorPtr();
1277c804472Sopenharmony_ci    static Json2::Value CreateObject();
1287c804472Sopenharmony_ci    static Json2::Value CreateArray();
1297c804472Sopenharmony_ci    static Json2::Value CreateBool(const bool value);
1307c804472Sopenharmony_ci    static Json2::Value CreateString(const std::string& value);
1317c804472Sopenharmony_ci    static Json2::Value DepthCopy(const Json2::Value& value);
1327c804472Sopenharmony_ci    static Json2::Value CreateNull();
1337c804472Sopenharmony_ci};
1347c804472Sopenharmony_ci
1357c804472Sopenharmony_ci#endif // JSONREADER_H
136