/ webserv / practice / config / saved2 / main.cpp
main.cpp
  1  #include "JsonParser.hpp"
  2  
  3  void	printJsonArray(std::vector<JsonData> const& jsonArray, int depth);
  4  void	printJson(JsonData const& jsonData, int depth);
  5  
  6  void	check_leaks(void)
  7  {
  8  	system("leaks a.out");
  9  }
 10  
 11  std::string	convertType(jsonType type)
 12  {
 13  	switch (type)
 14  	{
 15  		case TYPE_INTEGER:
 16  			return "TYPE_INTEGER";
 17  		case TYPE_FLOAT:
 18  			return "TYPE_FLOAT";
 19  		case TYPE_STRING:
 20  			return "TYPE_STRING";
 21  		case TYPE_BOOLEAN:
 22  			return "TYPE_BOOLEAN";
 23  		case TYPE_ARRAY:
 24  			return "TYPE_ARRAY";
 25  		case TYPE_OBJECT:
 26  			return "TYPE_OBJECT";
 27  		case TYPE_NULL:
 28  			return "TYPE_NULL";
 29  		default:
 30  			return "TYPE_ERROR";
 31  	}
 32  }
 33  
 34  void	printJsonArray(std::vector<JsonData> const& jsonArray, int depth)
 35  {
 36  	std::cout << "[" << std::endl;
 37  
 38  	for (int i = 0; i < jsonArray.size(); ++i)
 39  	{
 40  		for (int j = 0; j < depth + 1; ++j)
 41  			std::cout << "\t";
 42  
 43  		if (!jsonArray[i]._obj.empty())
 44  		{
 45  			std::cout << "{" << std::endl;
 46  			printJson(jsonArray[i], depth + 1);
 47  			for (int j = 0; j < depth + 1; ++j)
 48  				std::cout << "\t";
 49  			std::cout << "}" << " (" << convertType(jsonArray[i]._type) << ")";
 50  		}
 51  		else if (!jsonArray[i]._arr.empty())
 52  		{
 53  			printJsonArray(jsonArray[i]._arr, depth + 1);
 54  			std::cout << " (" << convertType(jsonArray[i]._type) << ")";
 55  		}
 56  		else
 57  		{
 58  			std::cout << jsonArray[i]._str << " (" << convertType(jsonArray[i]._type) << ")";
 59  		}
 60  
 61  		if (i < jsonArray.size() - 1)
 62  			std::cout << ",";
 63  		std::cout << std::endl;
 64  	}
 65  
 66  	for (int i = 0; i < depth; ++i)
 67  		std::cout << "\t";
 68  
 69  	std::cout << "]";
 70  }
 71  
 72  void	printJson(JsonData const& jsonData, int depth = 0)
 73  {
 74  	std::vector< std::pair<std::string, JsonData> > const& jsonObject
 75  		= jsonData._obj;
 76  
 77  	for (size_t i = 0; i < jsonObject.size(); ++i)
 78  	{
 79  		for (int j = 0; j < depth; ++j)
 80  			std::cout << "\t";
 81  
 82  		std::cout << jsonObject[i].first << ": ";
 83  
 84  		if (!jsonObject[i].second._obj.empty())
 85  		{
 86  			std::cout << "{" << std::endl;
 87  			printJson(jsonObject[i].second, depth + 1);
 88  			for (int j = 0; j < depth; ++j)
 89  				std::cout << "\t";
 90  			std::cout << "}" << " (" << convertType(jsonObject[i].second._type) << ")";
 91  		}
 92  		else if (!jsonObject[i].second._arr.empty())
 93  		{
 94  			printJsonArray(jsonObject[i].second._arr, depth);
 95  			std::cout << " (" << convertType(jsonObject[i].second._type) << ")";
 96  		}
 97  		else
 98  		{
 99  			std::cout << jsonObject[i].second._str << " (" << convertType(jsonObject[i].second._type) << ")";
100  		}
101  		std::cout << std::endl;
102  	}
103  }
104  
105  int	main(int argc, char *argv[])
106  {
107  	JsonParser	jsonParser;
108  	JsonData	json;
109  	std::string	text;
110  
111  //	atexit(check_leaks);
112  	if (argc == 2)
113  	{
114  		std::string	output;
115  		jsonParser.readFile(argv[1], text);
116  	}
117  	else
118  	{
119  		std::cerr << "Please test with file" << std::endl;
120  		return 1;
121  	}
122  	std::cout << text << std::endl;
123  	std::string::iterator start = text.begin();
124  
125  	json = jsonParser.parseObject(text, start);
126  	std::cout << "====== result =====" << std::endl;
127  	printJson(json);
128  	return 0;
129  }