Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
IJson.hpp
Go to the documentation of this file.
1//
2// Created by robin on 06.01.2021.
3//
4
5#ifndef TILESON_IJSON_HPP
6#define TILESON_IJSON_HPP
7
8namespace tson
9{
10 class IJson
11 {
12 public:
13
14 virtual IJson& operator[](std::string_view key) = 0;
15 virtual IJson &at(std::string_view key) = 0;
16 virtual IJson &at(size_t pos) = 0;
21 [[nodiscard]] virtual std::vector<std::unique_ptr<IJson>> array() = 0;
22 [[nodiscard]] virtual std::vector<std::unique_ptr<IJson>> &array(std::string_view key) = 0;
28 [[nodiscard]] virtual size_t size() const = 0;
29 [[nodiscard]] virtual bool parse(const fs::path &path) = 0;
30 [[nodiscard]] virtual bool parse(const void *data, size_t size) = 0;
31
32 template <typename T>
33 [[nodiscard]] T get(std::string_view key);
34 template <typename T>
35 [[nodiscard]] T get();
36 [[nodiscard]] virtual size_t count(std::string_view key) const = 0;
37 [[nodiscard]] virtual bool any(std::string_view key) const = 0;
38 [[nodiscard]] virtual bool isArray() const = 0;
39 [[nodiscard]] virtual bool isObject() const = 0;
40 [[nodiscard]] virtual bool isNull() const = 0;
41
47 [[nodiscard]] virtual fs::path directory() const = 0;
48 virtual void directory(const fs::path &directory) = 0;
49
53 virtual std::unique_ptr<IJson> create() = 0;
54
58 virtual ~IJson() = default;
59
60
61 protected:
62 [[nodiscard]] virtual int32_t getInt32(std::string_view key) = 0;
63 [[nodiscard]] virtual uint32_t getUInt32(std::string_view key) = 0;
64 [[nodiscard]] virtual int64_t getInt64(std::string_view key) = 0;
65 [[nodiscard]] virtual uint64_t getUInt64(std::string_view key) = 0;
66 [[nodiscard]] virtual double getDouble(std::string_view key) = 0;
67 [[nodiscard]] virtual float getFloat(std::string_view key) = 0;
68 [[nodiscard]] virtual std::string getString(std::string_view key) = 0;
69 [[nodiscard]] virtual bool getBool(std::string_view key) = 0;
70
71 [[nodiscard]] virtual int32_t getInt32() = 0;
72 [[nodiscard]] virtual uint32_t getUInt32() = 0;
73 [[nodiscard]] virtual int64_t getInt64() = 0;
74 [[nodiscard]] virtual uint64_t getUInt64() = 0;
75 [[nodiscard]] virtual double getDouble() = 0;
76 [[nodiscard]] virtual float getFloat() = 0;
77 [[nodiscard]] virtual std::string getString() = 0;
78 [[nodiscard]] virtual bool getBool() = 0;
79 };
80
81 template<typename T>
82 T IJson::get(std::string_view key)
83 {
84 if constexpr (std::is_same<T, double>::value)
85 return getDouble(key);
86 if constexpr (std::is_same<T, float>::value)
87 return getFloat(key);
88 else if constexpr (std::is_same<T, int32_t>::value)
89 return getInt32(key);
90 else if constexpr (std::is_same<T, uint32_t>::value)
91 return getUInt32(key);
92 else if constexpr (std::is_same<T, int64_t>::value)
93 return getInt64(key);
94 else if constexpr (std::is_same<T, uint64_t>::value)
95 return getUInt64(key);
96 else if constexpr (std::is_same<T, std::string>::value)
97 return getString(key);
98 else if constexpr (std::is_same<T, bool>::value)
99 return getBool(key);
100 else
101 return nullptr;
102 }
103
104 template<typename T>
106 {
107 if constexpr (std::is_same<T, double>::value)
108 return getDouble();
109 if constexpr (std::is_same<T, float>::value)
110 return getFloat();
111 else if constexpr (std::is_same<T, int32_t>::value)
112 return getInt32();
113 else if constexpr (std::is_same<T, uint32_t>::value)
114 return getUInt32();
115 else if constexpr (std::is_same<T, int64_t>::value)
116 return getInt64();
117 else if constexpr (std::is_same<T, uint64_t>::value)
118 return getUInt64();
119 else if constexpr (std::is_same<T, std::string>::value)
120 return getString();
121 else if constexpr (std::is_same<T, bool>::value)
122 return getBool();
123 else
124 return nullptr;
125 }
126
127}
128
129#endif //TILESON_IJSON_HPP
Definition IJson.hpp:11
virtual bool any(std::string_view key) const =0
virtual std::string getString(std::string_view key)=0
virtual float getFloat(std::string_view key)=0
virtual std::unique_ptr< IJson > create()=0
virtual bool getBool(std::string_view key)=0
virtual bool parse(const fs::path &path)=0
T get()
Definition IJson.hpp:105
virtual float getFloat()=0
virtual std::vector< std::unique_ptr< IJson > > & array(std::string_view key)=0
virtual bool isArray() const =0
virtual uint32_t getUInt32()=0
virtual double getDouble()=0
virtual ~IJson()=default
virtual int64_t getInt64(std::string_view key)=0
virtual fs::path directory() const =0
virtual double getDouble(std::string_view key)=0
virtual uint32_t getUInt32(std::string_view key)=0
virtual std::string getString()=0
virtual IJson & at(size_t pos)=0
virtual uint64_t getUInt64()=0
virtual bool getBool()=0
virtual IJson & operator[](std::string_view key)=0
virtual uint64_t getUInt64(std::string_view key)=0
virtual void directory(const fs::path &directory)=0
virtual size_t size() const =0
virtual bool isNull() const =0
virtual size_t count(std::string_view key) const =0
virtual int32_t getInt32()=0
virtual bool isObject() const =0
virtual bool parse(const void *data, size_t size)=0
virtual IJson & at(std::string_view key)=0
virtual std::vector< std::unique_ptr< IJson > > array()=0
virtual int64_t getInt64()=0
virtual int32_t getInt32(std::string_view key)=0
Definition Base64.hpp:12