You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.4 KiB

#include "webserv.hpp"
#define DEBUG 0
JSONObject JSONNode::obj() {
2 years ago
if (type == OBJECT)
return *values.object;
throw std::logic_error("Improper return");
}
JSONList JSONNode::lst() {
2 years ago
if (type == LIST)
return *values.list;
throw std::logic_error("Improper return");
}
string JSONNode::str() {
2 years ago
if (type == STRING)
return *values.str;
throw std::logic_error("Improper return");
}
int JSONNode::nbr() {
2 years ago
if (type == NUMBER)
return values.nbr;
throw std::logic_error("Improper return");
}
bool JSONNode::boo() {
if (type == BOOLEAN)
return values.bValue;
2 years ago
throw std::logic_error("Improper return");
}
void JSONNode::setObject(JSONObject *object) {
2 years ago
this->values.object = object;
type = OBJECT;
}
void JSONNode::setList(JSONList *list) {
2 years ago
this->values.list = list;
type = LIST;
}
void JSONNode::setString(string *str) {
2 years ago
this->values.str = str;
type = STRING;
}
void JSONNode::setNumber(int nbr) {
2 years ago
this->values.nbr = nbr;
type = NUMBER;
}
void JSONNode::setBoolean(bool v) {
2 years ago
this->values.bValue = v;
type = BOOLEAN;
}
void JSONNode::setNull() { type = NULL_TYPE; }
string JSONNode::stringify(int indentationLevel) {
2 years ago
string spaceString = string(indentationLevel, ' ');
// sstreams
std::stringstream output;
// cout < type << "\n";
switch (type) {
case STRING: {
output << spaceString << *values.str;
break;
}
case NUMBER: {
output << spaceString << values.nbr;
break;
}
case BOOLEAN: {
output << spaceString << (values.bValue ? "true" : "false");
break;
}
case NULL_TYPE: {
output << spaceString << "null";
break;
}
case LIST: {
// cout << "[";
output << spaceString << "[\n";
unsigned int index = 0;
for (JSONList::iterator i = (*values.list).begin();
i != (*values.list).end(); i++) {
output << (*i)->stringify(indentationLevel + 1);
if (index < (*values.list).size() - 1) {
output << ",\n";
}
index++;
};
output << "\n" << spaceString << "]\n";
break;
}
case OBJECT: {
output << spaceString << "{\n";
for (JSONObject::iterator i = (*values.object).begin();
i != (*values.object).end(); i++) {
output << spaceString << " "
<< "\"" << i->first << "\""
<< ": ";
output << i->second->stringify(indentationLevel + 1);
JSONObject::iterator next = i;
next++;
if ((next) != (*values.object).end()) {
output << ",\n";
}
output << spaceString << "\n";
}
output << spaceString << "}";
return output.str();
}
}
return output.str();
}