|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <concepts> |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdint> |
| 7 | +#include <format> |
| 8 | +#include <functional> |
| 9 | +#include <string> |
| 10 | +#include <unordered_map> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace cppjson |
| 14 | +{ |
| 15 | + enum struct JsonType |
| 16 | + { |
| 17 | + Null, |
| 18 | + String, |
| 19 | + Object, |
| 20 | + Number, |
| 21 | + Bool, |
| 22 | + Array |
| 23 | + }; |
| 24 | + |
| 25 | + class JsonObject |
| 26 | + { |
| 27 | + public: |
| 28 | + explicit JsonObject(); |
| 29 | + JsonObject(const JsonObject& other); |
| 30 | + JsonObject(JsonObject&& other) noexcept; |
| 31 | + JsonObject& operator=(const JsonObject& other); |
| 32 | + JsonObject& operator=(JsonObject&& other) noexcept; |
| 33 | + ~JsonObject(); |
| 34 | + |
| 35 | + template <typename T> |
| 36 | + T& As() noexcept(false); |
| 37 | + |
| 38 | + template <typename T> |
| 39 | + const T& As() const noexcept(false); |
| 40 | + |
| 41 | + private: |
| 42 | + JsonType _dataType{}; |
| 43 | + std::byte* _dataStorage{}; |
| 44 | + |
| 45 | + void Destroy(); |
| 46 | + template <typename T> |
| 47 | + T& DangerousAs() noexcept |
| 48 | + { |
| 49 | + return *std::launder(reinterpret_cast<T*>(this->_dataStorage)); |
| 50 | + } |
| 51 | + template <typename T> |
| 52 | + const T& DangerousAs() const noexcept |
| 53 | + { |
| 54 | + return *std::launder(reinterpret_cast<T*>(this->_dataStorage)); |
| 55 | + } |
| 56 | + |
| 57 | + friend struct std::formatter<cppjson::JsonObject>; |
| 58 | + }; |
| 59 | + |
| 60 | + class Object |
| 61 | + { |
| 62 | + public: |
| 63 | + explicit Object() = default; |
| 64 | + Object(const Object&) = default; |
| 65 | + Object(Object&&) = default; |
| 66 | + Object& operator=(const Object&) = default; |
| 67 | + Object& operator=(Object&&) = default; |
| 68 | + ~Object() = default; |
| 69 | + |
| 70 | + class ObjectProxy |
| 71 | + { |
| 72 | + public: |
| 73 | + explicit ObjectProxy(JsonObject& object) : _object(std::ref(object)) {} |
| 74 | + |
| 75 | + template <typename T> |
| 76 | + requires(!std::same_as<std::remove_cvref_t<T>, JsonObject>) |
| 77 | + explicit(false) operator T&() |
| 78 | + { |
| 79 | + return this->_object.get().As<T>(); |
| 80 | + } |
| 81 | + |
| 82 | + template <typename T> |
| 83 | + requires(!std::same_as<std::remove_cvref_t<T>, JsonObject>) |
| 84 | + explicit(false) operator const T&() const |
| 85 | + { |
| 86 | + return this->_object.get().As<T>(); |
| 87 | + } |
| 88 | + |
| 89 | + template <typename T> |
| 90 | + std::conditional_t<std::integral<T> && !std::same_as<T, bool>, void, T&> operator=(T&& assignment) |
| 91 | + { |
| 92 | + if constexpr (std::integral<T> && !std::same_as<T, bool>) static_cast<double&>(*this) = static_cast<double>(assignment); |
| 93 | + else |
| 94 | + return static_cast<T&>(*this) = std::forward<T>(assignment); |
| 95 | + } |
| 96 | + |
| 97 | + template <std::size_t N> |
| 98 | + std::string& operator=(const char (&str)[N]) |
| 99 | + { |
| 100 | + return static_cast<std::string&>(*this) = std::string{str}; |
| 101 | + } |
| 102 | + |
| 103 | + ObjectProxy operator[](const std::string& key); |
| 104 | + template <std::size_t N> |
| 105 | + ObjectProxy operator[](const char (&key)[N]) |
| 106 | + { |
| 107 | + return (*this)[std::string{key}]; |
| 108 | + } |
| 109 | + |
| 110 | + private: |
| 111 | + std::reference_wrapper<JsonObject> _object; |
| 112 | + |
| 113 | + friend struct std::formatter<cppjson::Object::ObjectProxy>; |
| 114 | + }; |
| 115 | + |
| 116 | + class ConstObjectProxy |
| 117 | + { |
| 118 | + public: |
| 119 | + explicit ConstObjectProxy(const JsonObject& object) : _object(std::ref(object)) {} |
| 120 | + template <typename T> |
| 121 | + explicit(false) operator const T&() const |
| 122 | + { |
| 123 | + return this->_object.get().As<T>(); |
| 124 | + } |
| 125 | + |
| 126 | + ConstObjectProxy operator[](const std::string& key) const; |
| 127 | + |
| 128 | + private: |
| 129 | + std::reference_wrapper<const JsonObject> _object; |
| 130 | + |
| 131 | + friend struct std::formatter<cppjson::Object::ConstObjectProxy>; |
| 132 | + }; |
| 133 | + |
| 134 | + ObjectProxy operator[](const std::string& key) { return ObjectProxy{this->_nodes[key]}; } |
| 135 | + |
| 136 | + ConstObjectProxy operator[](const std::string& key) const |
| 137 | + { |
| 138 | + if (!this->_nodes.contains(key)) throw std::logic_error("Invalid key" + key); |
| 139 | + |
| 140 | + return ConstObjectProxy{this->_nodes.at(key)}; |
| 141 | + } |
| 142 | + |
| 143 | + private: |
| 144 | + std::unordered_map<std::string, JsonObject> _nodes{}; |
| 145 | + |
| 146 | + friend struct std::formatter<cppjson::JsonObject>; |
| 147 | + friend struct std::formatter<cppjson::Object>; |
| 148 | + }; |
| 149 | + |
| 150 | + class Array |
| 151 | + { |
| 152 | + public: |
| 153 | + explicit Array() = default; |
| 154 | + ~Array() = default; |
| 155 | + |
| 156 | + Object::ObjectProxy operator[]() { return Object::ObjectProxy{this->_objects.emplace_back()}; } |
| 157 | + |
| 158 | + Object::ObjectProxy EmplaceBack(const auto& object) |
| 159 | + { |
| 160 | + if constexpr (std::same_as<decltype(object), std::nullptr_t>) return Object::ObjectProxy{this->_objects.emplace_back()}; |
| 161 | + else |
| 162 | + { |
| 163 | + auto& emplaced = this->_objects.emplace_back(); |
| 164 | + emplaced.As<std::remove_cvref_t<decltype(object)>>() = object; |
| 165 | + return Object::ObjectProxy{emplaced}; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + Object::ObjectProxy operator[](const std::size_t index) |
| 170 | + { |
| 171 | + if (index >= this->_objects.size()) throw std::logic_error("Out of bound"); |
| 172 | + return Object::ObjectProxy{this->_objects.at(index)}; |
| 173 | + } |
| 174 | + |
| 175 | + Object::ConstObjectProxy operator[](const std::size_t index) const |
| 176 | + { |
| 177 | + if (index >= this->_objects.size()) throw std::logic_error("Out of bound"); |
| 178 | + return Object::ConstObjectProxy{this->_objects.at(index)}; |
| 179 | + } |
| 180 | + |
| 181 | + private: |
| 182 | + std::vector<JsonObject> _objects{}; |
| 183 | + |
| 184 | + friend struct std::formatter<cppjson::JsonObject>; |
| 185 | + friend struct std::formatter<cppjson::Array>; |
| 186 | + }; |
| 187 | +} // namespace cppjson |
0 commit comments