diff options
| author | Dylan <boss@tehbox.org> | 2026-06-14 22:24:52 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-06-14 22:24:52 +1200 |
| commit | 82a68f09ecf2440b22c2604fe207bbee1c5f4d3a (patch) | |
| tree | 73437353a456a41366cf5a48e52c70d480a925f7 | |
| parent | e742522e2f428a17cd389e030d167fadcd25c8ef (diff) | |
| download | tehjson-82a68f09ecf2440b22c2604fe207bbee1c5f4d3a.tar.gz tehjson-82a68f09ecf2440b22c2604fe207bbee1c5f4d3a.zip | |
feat: Added arrays for all literal types
| -rw-r--r-- | src/json.cpp | 30 | ||||
| -rw-r--r-- | src/json.h | 2 | ||||
| -rw-r--r-- | src/json.hpp | 19 | ||||
| -rw-r--r-- | test/main.cpp | 2 |
4 files changed, 41 insertions, 12 deletions
diff --git a/src/json.cpp b/src/json.cpp index 25779b9..9d53582 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -1,4 +1,4 @@ -#include "json.h" +#include "json.hpp" #include "tokenizer.h" #include <cstddef> @@ -131,19 +131,27 @@ namespace TehJSON case TokenType::LSquare: { consume(TokenType::LSquare); - std::vector<int> vec; - while(nextTokenType() != TokenType::RSquare) + switch(nextTokenType()) { - if(nextTokenType() != TokenType::IntLit) - throw std::runtime_error("Only int arrays are supported so far"); - vec.push_back(std::stoi(consume(TokenType::IntLit).content)); - if(nextTokenType() == TokenType::Comma) - { - consume(TokenType::Comma); - } + case TokenType::IntLit: + children[childName.content].set<int>( + readVector<int>(TokenType::IntLit, + [](std::string x){return std::stoi(x);}));break; + case TokenType::FloatLit: + children[childName.content].set<float>( + readVector<float>(TokenType::FloatLit, + [](std::string x){return std::stof(x);}));break; + case TokenType::StringLit: + children[childName.content].set<std::string>( + readVector<std::string>( + TokenType::StringLit, [](std::string x){return x;}));break; + case TokenType::BoolLit: + children[childName.content].set<bool>( + readVector<bool>( + TokenType::BoolLit, [](std::string x){return x=="true";}));break; + default: throw std::runtime_error("Arrays can only be of literals so far"); } consume(TokenType::RSquare); - children[childName.content].set<int>(vec); break; }; default: throw std::runtime_error("Token type is not a literal!"); @@ -51,6 +51,8 @@ namespace TehJSON Token consume(TokenType type); TokenType nextTokenType(); void readFromTokens(std::vector<Token> tokens); + template <typename T> + std::vector<T> readVector(TokenType type, T(*)(const std::string)); // Leaf data fields std::shared_ptr<void> data; diff --git a/src/json.hpp b/src/json.hpp index 8ecd947..fb3a0b0 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "json.h" #include <stdexcept> @@ -53,4 +55,21 @@ namespace TehJSON }; data = std::make_shared<T>(value); } + + template<typename T> + std::vector<T> JSON::readVector(TokenType type, T(*strToT)(const std::string)) + { + std::vector<T> vec; + while(nextTokenType() != TokenType::RSquare) + { + if(nextTokenType() != type) + throw std::runtime_error("Array must be of all the same type"); + vec.push_back(strToT(consume(type).content)); + if(nextTokenType() == TokenType::Comma) + { + consume(TokenType::Comma); + } + } + return vec; + } } diff --git a/test/main.cpp b/test/main.cpp index 0f9df3a..9ed4bc6 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -15,7 +15,7 @@ int main() jsonWriter["test_int"].set<int>(123); jsonWriter["test_float"].set<float>(51.8); jsonWriter["test_vecs"]["int"].set<int>({1, 2, 3}); - // jsonWriter["test_vecs"]["float"].set<float>({0.1, 0.2, 0.3}); + jsonWriter["test_vecs"]["float"].set<float>({0.1, 0.2, 0.3}); jsonWriter["test_true"].set(true); jsonWriter["test_false"].set(false); jsonWriter["test_object"]["test_int"].set<int>(100); |
