Tileson  1.3.0
A helpful json parser for Tiled maps
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 
8 namespace 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 
50 
51  protected:
52  [[nodiscard]] virtual int32_t getInt32(std::string_view key) = 0;
53  [[nodiscard]] virtual uint32_t getUInt32(std::string_view key) = 0;
54  [[nodiscard]] virtual int64_t getInt64(std::string_view key) = 0;
55  [[nodiscard]] virtual uint64_t getUInt64(std::string_view key) = 0;
56  [[nodiscard]] virtual double getDouble(std::string_view key) = 0;
57  [[nodiscard]] virtual float getFloat(std::string_view key) = 0;
58  [[nodiscard]] virtual std::string getString(std::string_view key) = 0;
59  [[nodiscard]] virtual bool getBool(std::string_view key) = 0;
60 
61  [[nodiscard]] virtual int32_t getInt32() = 0;
62  [[nodiscard]] virtual uint32_t getUInt32() = 0;
63  [[nodiscard]] virtual int64_t getInt64() = 0;
64  [[nodiscard]] virtual uint64_t getUInt64() = 0;
65  [[nodiscard]] virtual double getDouble() = 0;
66  [[nodiscard]] virtual float getFloat() = 0;
67  [[nodiscard]] virtual std::string getString() = 0;
68  [[nodiscard]] virtual bool getBool() = 0;
69  };
70 
71  template<typename T>
72  T IJson::get(std::string_view key)
73  {
74  if constexpr (std::is_same<T, double>::value)
75  return getDouble(key);
76  if constexpr (std::is_same<T, float>::value)
77  return getFloat(key);
78  else if constexpr (std::is_same<T, int32_t>::value)
79  return getInt32(key);
80  else if constexpr (std::is_same<T, uint32_t>::value)
81  return getUInt32(key);
82  else if constexpr (std::is_same<T, int64_t>::value)
83  return getInt64(key);
84  else if constexpr (std::is_same<T, uint64_t>::value)
85  return getUInt64(key);
86  else if constexpr (std::is_same<T, std::string>::value)
87  return getString(key);
88  else if constexpr (std::is_same<T, bool>::value)
89  return getBool(key);
90  else
91  return nullptr;
92  }
93 
94  template<typename T>
96  {
97  if constexpr (std::is_same<T, double>::value)
98  return getDouble();
99  if constexpr (std::is_same<T, float>::value)
100  return getFloat();
101  else if constexpr (std::is_same<T, int32_t>::value)
102  return getInt32();
103  else if constexpr (std::is_same<T, uint32_t>::value)
104  return getUInt32();
105  else if constexpr (std::is_same<T, int64_t>::value)
106  return getInt64();
107  else if constexpr (std::is_same<T, uint64_t>::value)
108  return getUInt64();
109  else if constexpr (std::is_same<T, std::string>::value)
110  return getString();
111  else if constexpr (std::is_same<T, bool>::value)
112  return getBool();
113  else
114  return nullptr;
115  }
116 
117 }
118 
119 #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 bool getBool(std::string_view key)=0
virtual bool parse(const fs::path &path)=0
T get()
Definition: IJson.hpp:95
virtual IJson & operator[](std::string_view key)=0
virtual IJson & at(std::string_view key)=0
virtual float getFloat()=0
virtual bool isArray() const =0
virtual IJson & at(size_t pos)=0
virtual uint32_t getUInt32()=0
virtual double getDouble()=0
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 uint64_t getUInt64()=0
virtual bool getBool()=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 std::vector< std::unique_ptr< IJson > > & array(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