Skip to content

Commit b09cfd3

Browse files
committed
Add cppjson::Object
Signed-off-by: TymianekPL <tymi@tymi.org>
1 parent d61bbbe commit b09cfd3

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

cppjson/include/cppjson/object.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <cstddef>
88
#include <format>
9+
#include <unordered_map>
910

1011
namespace cppjson
1112
{
@@ -42,6 +43,31 @@ namespace cppjson
4243

4344
friend struct std::formatter<cppjson::JsonObject>;
4445
};
46+
47+
class Object
48+
{
49+
public:
50+
explicit Object() = default;
51+
Object(const Object&) = default;
52+
Object(Object&&) = default;
53+
Object& operator=(const Object&) = default;
54+
Object& operator=(Object&&) = default;
55+
~Object() = default;
56+
57+
template <typename T>
58+
T& operator[](const std::string& key)
59+
{
60+
return this->_nodes[key].As<T>();
61+
}
62+
template <typename T>
63+
const T& operator[](const std::string& key) const
64+
{
65+
if (!this->_nodes.contains(key)) throw std::logic_error("Invalid key" + key);
66+
return this->_nodes.at(key).As<T>();
67+
}
68+
private:
69+
std::unordered_map<std::string, JsonObject> _nodes{};
70+
};
4571
}
4672

4773
template<>

cppjson/src/object.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <new>
33
#include <stdexcept>
44

5-
constexpr std::size_t DataStorageSize = std::max({ sizeof(std::string), sizeof(cppjson::JsonObject), sizeof(double), sizeof(bool) });
5+
constexpr std::size_t DataStorageSize = std::max({ sizeof(std::string), sizeof(cppjson::Object), sizeof(double), sizeof(bool) });
66

77
cppjson::JsonObject::JsonObject()
88
: _dataStorage(static_cast<std::byte*>(::operator new(DataStorageSize)))
@@ -67,6 +67,19 @@ bool& cppjson::JsonObject::As<bool>() noexcept(false)
6767
return DangerousAs<bool>();
6868
}
6969

70+
template<>
71+
cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() noexcept(false)
72+
{
73+
if (this->_dataType == JsonType::Null)
74+
{
75+
this->_dataType = JsonType::Object;
76+
return *new(this->_dataStorage) cppjson::Object{};
77+
}
78+
79+
if (this->_dataType != JsonType::Object) throw std::logic_error("Cannot convert this object to a bool");
80+
return DangerousAs<cppjson::Object>();
81+
}
82+
7083
template<>
7184
std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() noexcept(false)
7285
{
@@ -76,7 +89,6 @@ std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() noexcept(false)
7689
return *new(this->_dataStorage) std::nullptr_t{};
7790
}
7891

79-
8092
template<>
8193
const std::string& cppjson::JsonObject::As<std::string>() const noexcept(false)
8294
{
@@ -98,6 +110,14 @@ const bool& cppjson::JsonObject::As<bool>() const noexcept(false)
98110
return DangerousAs<bool>();
99111
}
100112

113+
template<>
114+
const cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() const noexcept(false)
115+
{
116+
if (this->_dataType != JsonType::Object) throw std::logic_error("Cannot convert this object to an object");
117+
return DangerousAs<cppjson::Object>();
118+
}
119+
120+
101121
template<>
102122
const std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() const noexcept(false)
103123
{

0 commit comments

Comments
 (0)