Webserv
Loading...
Searching...
No Matches
Nodes.cpp
Go to the documentation of this file.
1#include "webserv.hpp"
2
4 if (type == OBJECT)
5 return *values.object;
6 throw std::logic_error("Improper return");
7}
9 if (type == LIST)
10 return *values.list;
11 throw std::logic_error("Improper return");
12}
13string JSONNode::str() {
14 if (type == STRING)
15 return *values.str;
16 throw std::logic_error("Improper return");
17}
19 if (type == NUMBER)
20 return values.nbr;
21 throw std::logic_error("Improper return");
22}
24 if (type == BOOLEAN)
25 return values.bValue;
26 throw std::logic_error("Improper return");
27}
28
30 this->values.object = object;
31 type = OBJECT;
32}
34 this->values.list = list;
35 type = LIST;
36}
37void JSONNode::setString(string *str) {
38 this->values.str = str;
39 type = STRING;
40}
41void JSONNode::setNumber(int nbr) {
42 this->values.nbr = nbr;
43 type = NUMBER;
44}
46 this->values.bValue = v;
47 type = BOOLEAN;
48}
50
51string JSONNode::stringify(int indentationLevel) {
52 string spaceString = string(indentationLevel, ' ');
53 // sstreams
54 std::stringstream output;
55 // cout < type << "\n";
56 switch (type) {
57 case STRING: {
58 output << spaceString << *values.str;
59 break;
60 }
61 case NUMBER: {
62 output << spaceString << values.nbr;
63 break;
64 }
65 case BOOLEAN: {
66 output << spaceString << (values.bValue ? "true" : "false");
67 break;
68 }
69 case NULL_TYPE: {
70 output << spaceString << "null";
71 break;
72 }
73 case LIST: {
74 // cout << "[";
75 output << spaceString << "[\n";
76 unsigned int index = 0;
77 for (JSONList::iterator i = (*values.list).begin();
78 i != (*values.list).end(); i++) {
79 output << (*i)->stringify(indentationLevel + 1);
80 if (index < (*values.list).size() - 1) {
81 output << ",\n";
82 }
83 index++;
84 };
85 output << "\n" << spaceString << "]\n";
86 break;
87 }
88 case OBJECT: {
89 output << spaceString << "{\n";
90 for (JSONObject::iterator i = (*values.object).begin();
91 i != (*values.object).end(); i++) {
92 output << spaceString << " "
93 << "\"" << i->first << "\""
94 << ": ";
95 output << i->second->stringify(indentationLevel + 1);
96 JSONObject::iterator next = i;
97 next++;
98 if ((next) != (*values.object).end()) {
99 output << ",\n";
100 }
101 output << spaceString << "\n";
102 }
103 output << spaceString << "}";
104 return output.str();
105 }
106 }
107 return output.str();
108}
@ LIST
Definition: Nodes.hpp:13
@ NUMBER
Definition: Nodes.hpp:13
@ STRING
Definition: Nodes.hpp:13
@ BOOLEAN
Definition: Nodes.hpp:13
@ NULL_TYPE
Definition: Nodes.hpp:13
@ OBJECT
Definition: Nodes.hpp:13
union JSONNode::Values values
void setObject(JSONObject *object)
Definition: Nodes.cpp:29
Type type
Definition: Nodes.hpp:21
int nbr()
Definition: Nodes.cpp:18
void setNumber(int nbr)
Definition: Nodes.cpp:41
JSONObject obj()
Definition: Nodes.cpp:3
void setNull()
Definition: Nodes.cpp:49
void setBoolean(bool v)
Definition: Nodes.cpp:45
string stringify(int indentationLevel)
Definition: Nodes.cpp:51
JSONList lst()
Definition: Nodes.cpp:8
string str()
Definition: Nodes.cpp:13
void setString(string *str)
Definition: Nodes.cpp:37
void setList(JSONList *list)
Definition: Nodes.cpp:33
bool boo()
Definition: Nodes.cpp:23
JSONList * list
Definition: Nodes.hpp:16
JSONObject * object
Definition: Nodes.hpp:15
string * str
Definition: Nodes.hpp:17
std::vector< JSONNode * > JSONList
Definition: webserv.hpp:49
std::map< string, JSONNode * > JSONObject
Definition: webserv.hpp:48