|
19 | 19 | #include <iomanip> |
20 | 20 | #include <iostream> |
21 | 21 | #include <sstream> |
| 22 | +#include <limits> |
22 | 23 |
|
23 | 24 | #ifdef _WIN32 |
24 | 25 | #include <time.h> |
@@ -67,93 +68,109 @@ extern double prog_start_time; |
67 | 68 | // JSON compiler definitions. |
68 | 69 | class JSONWriter { |
69 | 70 | public: |
70 | | - explicit JSONWriter(std::ostream& out) |
71 | | - : out_(out), indent_(0), state_(JSONOBJECT) {} |
| 71 | + explicit JSONWriter(std::ostream& out) : out_(out) {} |
72 | 72 |
|
73 | 73 | inline void indent() { indent_ += 2; } |
74 | 74 | inline void deindent() { indent_ -= 2; } |
75 | 75 | inline void advance() { |
76 | | - for (int i = 0; i < indent_; i++) out_ << " "; |
| 76 | + for (int i = 0; i < indent_; i++) out_ << ' '; |
77 | 77 | } |
78 | 78 |
|
79 | 79 | inline void json_start() { |
80 | | - if (state_ == JSONVALUE) out_ << ","; |
81 | | - out_ << "\n"; |
| 80 | + if (state_ == kAfterValue) out_ << ','; |
| 81 | + out_ << '\n'; |
82 | 82 | advance(); |
83 | | - out_ << "{"; |
| 83 | + out_ << '{'; |
84 | 84 | indent(); |
85 | | - state_ = JSONOBJECT; |
| 85 | + state_ = kObjectStart; |
86 | 86 | } |
87 | 87 |
|
88 | 88 | inline void json_end() { |
89 | | - out_ << "\n"; |
| 89 | + out_ << '\n'; |
90 | 90 | deindent(); |
91 | 91 | advance(); |
92 | | - out_ << "}"; |
93 | | - state_ = JSONVALUE; |
| 92 | + out_ << '}'; |
| 93 | + state_ = kAfterValue; |
94 | 94 | } |
95 | 95 | template <typename T> |
96 | 96 | inline void json_objectstart(T key) { |
97 | | - if (state_ == JSONVALUE) out_ << ","; |
98 | | - out_ << "\n"; |
| 97 | + if (state_ == kAfterValue) out_ << ','; |
| 98 | + out_ << '\n'; |
99 | 99 | advance(); |
100 | | - out_ << "\"" << key << "\"" |
101 | | - << ": {"; |
| 100 | + write_string(key); |
| 101 | + out_ << ": {"; |
102 | 102 | indent(); |
103 | | - state_ = JSONOBJECT; |
| 103 | + state_ = kObjectStart; |
104 | 104 | } |
105 | 105 |
|
106 | 106 | template <typename T> |
107 | 107 | inline void json_arraystart(T key) { |
108 | | - if (state_ == JSONVALUE) out_ << ","; |
109 | | - out_ << "\n"; |
| 108 | + if (state_ == kAfterValue) out_ << ','; |
| 109 | + out_ << '\n'; |
110 | 110 | advance(); |
111 | | - out_ << "\"" << key << "\"" |
112 | | - << ": ["; |
| 111 | + write_string(key); |
| 112 | + out_ << ": ["; |
113 | 113 | indent(); |
114 | | - state_ = JSONOBJECT; |
| 114 | + state_ = kObjectStart; |
115 | 115 | } |
116 | 116 | inline void json_objectend() { |
117 | | - out_ << "\n"; |
| 117 | + out_ << '\n'; |
118 | 118 | deindent(); |
119 | 119 | advance(); |
120 | | - out_ << "}"; |
121 | | - state_ = JSONVALUE; |
| 120 | + out_ << '}'; |
| 121 | + state_ = kAfterValue; |
122 | 122 | } |
123 | 123 |
|
124 | 124 | inline void json_arrayend() { |
125 | | - out_ << "\n"; |
| 125 | + out_ << '\n'; |
126 | 126 | deindent(); |
127 | 127 | advance(); |
128 | | - out_ << "]"; |
129 | | - state_ = JSONVALUE; |
| 128 | + out_ << ']'; |
| 129 | + state_ = kAfterValue; |
130 | 130 | } |
131 | 131 | template <typename T, typename U> |
132 | | - inline void json_keyvalue(T key, U value) { |
133 | | - if (state_ == JSONVALUE) out_ << ","; |
134 | | - out_ << "\n"; |
| 132 | + inline void json_keyvalue(const T& key, const U& value) { |
| 133 | + if (state_ == kAfterValue) out_ << ','; |
| 134 | + out_ << '\n'; |
135 | 135 | advance(); |
136 | | - out_ << "\"" << key << "\"" |
137 | | - << ": " |
138 | | - << "\""; |
139 | | - out_ << EscapeJsonChars(value) << "\""; |
140 | | - state_ = JSONVALUE; |
| 136 | + write_string(key); |
| 137 | + out_ << ": "; |
| 138 | + write_value(value); |
| 139 | + state_ = kAfterValue; |
141 | 140 | } |
142 | 141 |
|
143 | 142 | template <typename U> |
144 | | - inline void json_element(U value) { |
145 | | - if (state_ == JSONVALUE) out_ << ","; |
146 | | - out_ << "\n"; |
| 143 | + inline void json_element(const U& value) { |
| 144 | + if (state_ == kAfterValue) out_ << ','; |
| 145 | + out_ << '\n'; |
147 | 146 | advance(); |
148 | | - out_ << "\"" << EscapeJsonChars(value) << "\""; |
149 | | - state_ = JSONVALUE; |
| 147 | + write_value(value); |
| 148 | + state_ = kAfterValue; |
150 | 149 | } |
151 | 150 |
|
152 | 151 | private: |
153 | | - enum JSONState { JSONOBJECT, JSONVALUE }; |
| 152 | + template <typename T, |
| 153 | + typename test_for_number = typename std:: |
| 154 | + enable_if<std::numeric_limits<T>::is_specialized, bool>::type> |
| 155 | + inline void write_value(T number) { |
| 156 | + if (std::is_same<T, bool>::value) |
| 157 | + out_ << (number ? "true" : "false"); |
| 158 | + else |
| 159 | + out_ << number; |
| 160 | + } |
| 161 | + |
| 162 | + inline void write_value(const char* str) { write_string(str); } |
| 163 | + inline void write_value(const std::string& str) { write_string(str); } |
| 164 | + |
| 165 | + inline void write_string(const std::string& str) { |
| 166 | + out_ << '"' << EscapeJsonChars(str) << '"'; |
| 167 | + } |
| 168 | + inline void write_string(const char* str) { write_string(std::string(str)); } |
| 169 | + |
| 170 | + enum JSONState { kObjectStart, kAfterValue }; |
154 | 171 | std::ostream& out_; |
155 | | - int indent_; |
156 | | - int state_; |
| 172 | + int indent_ = 0; |
| 173 | + int state_ = kObjectStart; |
157 | 174 | }; |
158 | 175 |
|
159 | 176 | } // namespace report |
|
0 commit comments