summaryrefslogtreecommitdiff
path: root/json.cpp
blob: 042dde7ea1e3aac134ec9531ba81141e2736145d (plain)
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
#include "json.h"

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;

using namespace JSON;

bool JSON::parse(const std::string& s, Value& v) {
	typedef std::string::const_iterator Iterator;
	
	qi::rule<Iterator, bool()> bool_p = qi::lit("false")[qi::_val = false] | qi::lit("true")[qi::_val = true];
	
	qi::rule<Iterator, boost::variant<int, float>()> number_p = (qi::int_ >> !(qi::lit('.') | 'e' | 'E')) | qi::float_;
	
	qi::rule<Iterator, char()> char_p = (qi::char_ - '\"' - '\\') | '\\' >> (
		qi::char_('\"') |
		qi::char_('\\') |
		qi::char_('/') |
		'b' >> qi::attr('\b') |
		'f' >> qi::attr('\f') |
		'n' >> qi::attr('\n') |
		'r' >> qi::attr('\r') |
		't' >> qi::attr('\t')
	);
	
	qi::rule<Iterator, std::string()> string_p = '\"' >> *(char_p) >> '\"';
	
	qi::rule<Iterator, Value()> value_p;
	
	qi::rule<Iterator, Array()> array_p = '[' >> -(value_p % ',') >> ']';
	
	qi::rule<Iterator, std::pair<std::string, Value>()> key_value_p = string_p >> ":" >> value_p;
	qi::rule<Iterator, Object()> object_p = '{' >> -(key_value_p % ',') >> '}';
	
	value_p = qi::omit[*qi::space] >> (bool_p | number_p | string_p | array_p | object_p) >> qi::omit[*qi::space];
	
	return qi::parse(s.begin(), s.end(), value_p, v);
}

struct generate : public boost::static_visitor<std::ostream&> {
	std::ostream& stream;
	generate(std::ostream& s) : stream(s) {}
	
	std::ostream& operator()(const bool& x) const {
		return stream << (x ? "true" : "false");
	}
	
	std::ostream& operator()(const int& x) const {
		return stream << x;
	}
	
	std::ostream& operator()(const float& x) const {
		return stream << x;
	}
	
	std::ostream& operator()(const std::string& x) const {
		stream << '\"';
		for(std::string::const_iterator it = x.begin(); it != x.end(); it++) {
			switch(*it) {
				case '\"':
				case '\\':
					stream << '\\' << *it;
					break;
				
				case '\b':
					stream << "\\b";
					break;
				
				case '\f':
					stream << "\\f";
					break;
				
				case '\n':
					stream << "\\n";
					break;
				
				case '\r':
					stream << "\\r";
					break;
				
				case '\t':
					stream << "\\t";
					break;
				
				default:
					stream << *it;
			}
		}
		return stream << '\"';
	}
	
	std::ostream& operator()(const Array& x) const {
		stream << "[";
		for(Array::const_iterator it = x.begin();;) {
			boost::apply_visitor(generate(stream), *it);
			
			if(++it == x.end()) {
				break;
			}
			
			stream << ", ";
		}
		return stream << "]";
	}
	
	std::ostream& operator()(const Object& x) const {
		stream << "{";
		for(Object::const_iterator it = x.begin();;) {
			(*this)(it->first);
			
			stream << ": ";
			
			boost::apply_visitor(generate(stream), it->second);
			
			if(++it == x.end()) {
				break;
			}
			
			stream << ", ";
		}
		return stream << "}";
	}
};

std::ostream& operator<<(std::ostream& s, const Value& v) {
	return boost::apply_visitor(generate(s), v);
}