-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.hpp
112 lines (111 loc) · 3.42 KB
/
json.hpp
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
#ifndef BLOCKY3D_JSON_HPP
#define BLOCKY3D_JSON_HPP
#include<unordered_map>
#include<vector>
#include<memory>
#include<string>
#include"exc.hpp"
namespace blocky{
#define inherits : public
enum class JsonKind{
integer,boolean,list,hashtable,nothing
};
struct Index{
int numind;//unions need weird destructors
std::string strind;
bool is_numerical=false;
Index(int x){
numind = x;
is_numerical = true;
}
Index(std::string x){
strind = x;
is_numerical = false;
}
};
struct Json{
public:
JsonKind kind;
virtual std::string dump() const = 0;
virtual std::shared_ptr<Json> operator[](Index ind) const{
throw json_error("Indexing on json kind "+std::to_string((int)kind));
}
virtual std::shared_ptr<Json>& operator[](Index ind){
throw json_error("Indexing on json kind "+std::to_string((int)kind));
}
};
struct IntegerJson inherits Json{
IntegerJson(){
kind=JsonKind::integer;
}
int data;
std::string dump() const override{
return std::to_string(data);
}
};
struct BooleanJson inherits Json{
BooleanJson(){
kind=JsonKind::boolean;
}
bool data;
std::string dump() const override{
return (data?"true":"false");
}
};
struct ListJson inherits Json{
ListJson(){
kind=JsonKind::list;
}
std::vector<std::shared_ptr<Json>> data;
std::string dump() const override{
if(data.empty())return "[]";
std::string str = "[";
for(size_t i=0;i<data.size();i++){
str += data[i]->dump();
if((i+1)<data.size())str += ",";
}
return str+"]";
}
std::shared_ptr<Json>& operator[](Index ind) override{
if(!ind.is_numerical)
throw json_error("Looking up key "+ind.strind+" on list json");
return data.at(ind.numind);
}
std::shared_ptr<Json> operator[](Index ind) const override{
if(!ind.is_numerical)
throw json_error("Looking up key "+ind.strind+" on list json");
return data.at(ind.numind);
}
};
struct DictJson inherits Json{
DictJson(){
kind = JsonKind::hashtable;
}
std::unordered_map<std::string,std::shared_ptr<Json>> data;
std::string dump() const override{
if(data.empty())return "{}";
std::string str = "{";
size_t i=0;
for(auto& [k,v] : data){
str += k+":"+v->dump();
i++;
if(i<data.size()){
str += ",";
}
}
return str+"}";
}
std::shared_ptr<Json>& operator[](Index ind) override{
if(ind.is_numerical)
throw json_error("Indexing #"+std::to_string(ind.numind)+" on dict json");
return data.at(ind.strind);
}
std::shared_ptr<Json> operator[](Index ind) const override{
if(ind.is_numerical)
throw json_error("Indexing #"+std::to_string(ind.numind)+" on dict json");
return data.at(ind.strind);
}
};
#undef inherits
}
#endif