blob: fb3a0b027446ae2c64d9f8c46e4872b3a1cf7ef5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#pragma once
#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>
std::string TehJSON::JSON::serializeData(std::vector<T>* data)
{
std::string res = "[";
for(int i = 0; i < data->size(); i++)
{
T x = data->at(i);
// res += "inty";
res += TehJSON::JSON::serializeData<T>(&x);
if(i != data->size() - 1)
res += ", ";
}
res += "]";
return res;
}
template<typename T>
void TehJSON::JSON::set(std::vector<T> value)
{
if(children.size() != 0)
throw std::runtime_error("Node is not a leaf (has children already)!");
isLeaf = true;
dataSerializer = [](std::shared_ptr<void> data) -> std::string {
return TehJSON::JSON::serializeData<T>((std::vector<T>*)(data.get()));
};
data = std::make_shared<std::vector<T>>(value);
}
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 = [](std::shared_ptr<void> data) -> std::string {
return JSON::serializeData<T>((T*)(data.get()));
};
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;
}
}
|