diff options
| author | Dylan <boss@tehbox.org> | 2026-06-06 18:05:19 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-06-06 18:05:19 +1200 |
| commit | 46c896bcd78d31130321562b0659e28230261b8e (patch) | |
| tree | 66174f484693763b7a7ca678fda8ea4b2b98cbe7 /src | |
| download | tehjson-46c896bcd78d31130321562b0659e28230261b8e.tar.gz tehjson-46c896bcd78d31130321562b0659e28230261b8e.zip | |
feat: Initial commit, json data structure and serializing to json
Diffstat (limited to 'src')
| -rw-r--r-- | src/json.cpp | 69 | ||||
| -rw-r--r-- | src/json.h | 40 | ||||
| -rw-r--r-- | src/json.hpp | 28 |
3 files changed, 137 insertions, 0 deletions
diff --git a/src/json.cpp b/src/json.cpp new file mode 100644 index 0000000..c11ca6e --- /dev/null +++ b/src/json.cpp @@ -0,0 +1,69 @@ +#include "json.h" + +#include <cstddef> +#include <stdexcept> + +namespace TehJSON +{ + JSON::JSON() + :children() + { + } + + JSON::~JSON() + { + } + + std::string JSON::leafType() + { + if(!isLeaf) + throw std::runtime_error("Node is not a leaf!"); + throw std::runtime_error("Not implemented yet"); + } + + std::string JSON::getSerialized() + { + return _getSerialized(0); + } + std::string JSON::_getSerialized(int currIndent) + { + if(isLeaf) + return dataSerializer(data); + + std::string serialized = "{\n"; + + int childrenLeft = childCount(); + for(auto [childName, child]: children) + { + childrenLeft--; + for(int i = 0; i < currIndent + 1; i++) + serialized += '\t'; + serialized += "\"" + childName + "\": "; + serialized += child._getSerialized(currIndent + 1); + if(childrenLeft != 0) + serialized += ','; + serialized += '\n'; + } + for(int i = 0; i < currIndent; i++) + serialized += '\t'; + serialized += "}"; + + return serialized; + } + + JSON& JSON::operator[](std::string name) + { + if(isLeaf) + throw std::runtime_error("Node is a leaf!"); + // if(children.count(name) == 0) + // throw std::out_of_range("Child \"" + name + "\" does not exist"); + return children[name]; + } + + size_t JSON::childCount() + { + if(isLeaf) + throw std::runtime_error("Node is a leaf!"); + return children.size(); + } +} diff --git a/src/json.h b/src/json.h new file mode 100644 index 0000000..d1d7938 --- /dev/null +++ b/src/json.h @@ -0,0 +1,40 @@ +#include <cstddef> +#include <map> +#include <memory> +#include <string> + +namespace TehJSON +{ + class JSON + { + public: + JSON(); + JSON(const JSON& other) = default; + ~JSON(); + + // Leaf methods + template <typename T> + T& get(); + template <typename T> + void set(T value); + std::string leafType(); + std::string getSerialized(); + std::string _getSerialized(int currIndent); + template <typename T> + static std::string serializeData(std::shared_ptr<void> data); + + // Non leaf methods + JSON& operator[](std::string name); + size_t childCount(); + // std::vector<std::string> childNames(); + private: + bool isLeaf = false; + + // Leaf data fields + std::shared_ptr<void> data; + std::string (*dataSerializer)(std::shared_ptr<void>); + + // Not leaf data fields + std::map<std::string, JSON> children; + }; +} diff --git a/src/json.hpp b/src/json.hpp new file mode 100644 index 0000000..6f19ddd --- /dev/null +++ b/src/json.hpp @@ -0,0 +1,28 @@ +#include "json.h" + +#include <stdexcept> +#include <iostream> + +namespace TehJSON +{ + + template<typename T> + T& JSON::get() + { + if(!isLeaf) + throw std::runtime_error("Node is not a leaf!"); + + return *static_cast<T*>(data.get()); + } + + template<typename T> + void JSON::set(T value) + { + if(children.size() != 0) + throw std::runtime_error("Node is not a leaf (has children already)!"); + isLeaf = true; + dataSerializer = JSON::serializeData<T>; + data = std::make_shared<T>(value); + } + +} |
