blob: 25779b96486d5bef399e37a5290263ee88aec3d2 (
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#include "json.h"
#include "tokenizer.h"
#include <cstddef>
#include <stdexcept>
#include <string>
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();
}
Token JSON::consume()
{
if(tokenPos >= tokens.size())
throw std::out_of_range("No tokens left, but json not finished!");
return tokens[tokenPos++];
}
Token JSON::consume(TokenType type)
{
Token t = consume();
if(t.type != type)
throw std::runtime_error("Wrong token type, expected: " + getTokenName(type) + ", but got: " + getTokenName(t.type));
return t;
}
TokenType JSON::nextTokenType()
{
if(tokenPos >= tokens.size())
throw std::out_of_range("No tokens left, but json not finished!");
return tokens[tokenPos].type;
}
void JSON::readFromString(std::string s)
{
Tokenizer tokenizer;
tokenizer.appendInput(s);
std::vector<Token> stringTokens = tokenizer.tokenize();
readFromTokens(stringTokens);
}
void JSON::readFromTokens(std::vector<Token> tokens)
{
this->tokens = tokens;
tokenPos = 0;
consume(TokenType::LBrace);
while(nextTokenType() != TokenType::RBrace)
{
Token childName = consume(TokenType::StringLit);
// std::cout << "Child: " << childName.content << std::endl;
consume(TokenType::Colon);
switch(nextTokenType())
{
case TokenType::LBrace:
{
std::vector<Token> childTokens;
while(nextTokenType() != TokenType::RBrace)
{
childTokens.push_back(consume());
}
childTokens.push_back(consume(TokenType::RBrace));
children[childName.content].readFromTokens(childTokens);
break;
}
case TokenType::StringLit: children[childName.content].set<std::string>(consume(TokenType::StringLit).content); break;
case TokenType::IntLit: children[childName.content].set<int>(std::stoi(consume(TokenType::IntLit).content)); break;
case TokenType::FloatLit: children[childName.content].set<float>(std::stof(consume(TokenType::FloatLit).content)); break;
case TokenType::BoolLit: children[childName.content].set<bool>(consume(TokenType::BoolLit).content == "true"); break;
case TokenType::LSquare:
{
consume(TokenType::LSquare);
std::vector<int> vec;
while(nextTokenType() != TokenType::RSquare)
{
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);
}
}
consume(TokenType::RSquare);
children[childName.content].set<int>(vec);
break;
};
default: throw std::runtime_error("Token type is not a literal!");
}
if(nextTokenType() != TokenType::RBrace)
consume(TokenType::Comma);
}
consume(TokenType::RBrace);
}
// Serializers for basic types
template <> std::string TehJSON::JSON::serializeData<int>(int *data)
{
return std::to_string(*data);
}
template <> std::string TehJSON::JSON::serializeData<float>(float *data)
{
return std::to_string(*data);
}
template <> std::string TehJSON::JSON::serializeData<std::string>(std::string *data)
{
return '"' + *data + '"';
}
template <> std::string TehJSON::JSON::serializeData<bool>(bool *data)
{
return (*data)?"true":"false";
}
}
|