blob: c566c2befff5363e2290a925536ae79b3396ac4d (
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
|
#include "json.h"
#include "tokenizer.h"
#include <cstddef>
#include <iostream>
#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, 0);
}
int JSON::readFromTokens(std::vector<Token> tokens, int pos)
{
this->tokens = tokens;
tokenPos = pos;
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: tokenPos = children[childName.content].readFromTokens(tokens, tokenPos); 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;
default: throw std::runtime_error("Token type is not a literal!");
}
if(nextTokenType() != TokenType::RBrace)
consume(TokenType::Comma);
}
consume(TokenType::RBrace);
return tokenPos;
}
// Serializers for basic types
template <> std::string TehJSON::JSON::serializeData<int>(std::shared_ptr<void> data)
{
return std::to_string(*static_cast<int*>(data.get()));
}
template <> std::string TehJSON::JSON::serializeData<float>(std::shared_ptr<void> data)
{
return std::to_string(*static_cast<float*>(data.get()));
}
template <> std::string TehJSON::JSON::serializeData<std::string>(std::shared_ptr<void> data)
{
return '"' + *static_cast<std::string*>(data.get()) + '"';
}
}
|