26 #include <boost/regex.hpp>
31 : indenting(indenting), nesting(0), indentChars(indentChars), jsStyleKeys(jsStyleKeys)
39 stack.push(eEmptyObject);
46 if (stack.size() == 0)
48 throw LocalException(
"Stack is empty");
51 if (stack.top() != eEmptyObject && stack.top() != eObject)
53 throw LocalException(
"Wrong type");
66 if (jsStyleKeys &&
isId(key))
68 ss << key << (indenting == 2 ?
": " :
":");
72 ss <<
EscapeQuote(key) << (indenting == 2 ?
": " :
":");
80 stack.push(eEmptyArray);
87 if (stack.size() == 0)
89 throw LocalException(
"Stack is empty");
92 if (stack.top() != eEmptyArray && stack.top() != eArray)
94 throw LocalException(
"Wrong type");
118 boost::cmatch result;
119 return boost::regex_search(
str.c_str(), result, boost::regex(
"^[_A-Za-z]\\w*$"), boost::regex_constants::match_default);
129 std::ostringstream ss;
131 for (
auto iter =
str.cbegin(); iter !=
str.cend(); iter++)
166 ss <<
"\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*iter);
180 void JsonWriter::writeNewLine()
186 for (
int i = 0; i < nesting; i++)
193 void JsonWriter::beginValue()
195 if (stack.size() == 0)
200 if (stack.top() == eArray)
202 ss << (indenting == 1 ?
", " :
",");
205 if (stack.top() != eObjectKey)
210 if (stack.top() != eEmptyArray && stack.top() != eArray && stack.top() != eObjectKey)
212 throw LocalException(
"Wrong type");
216 void JsonWriter::beginKey()
218 if (stack.size() == 0)
220 throw LocalException(
"Stack is empty");
223 if (stack.top() != eEmptyObject && stack.top() != eObject)
225 throw LocalException(
"Wrong type");
228 if (stack.top() == eObject)
230 ss << (indenting == 1 ?
", " :
",");
236 void JsonWriter::endKey()
239 stack.push(eObjectKey);
242 void JsonWriter::endValue()
244 if (stack.size() == 0)
248 else if (stack.top() == eEmptyArray)
250 stack.top() = eArray;
252 else if (stack.top() == eObjectKey)
254 stack.top() = eObject;
258 void JsonWriter::endArrayOrObject()
260 if (stack.top() == eArray || stack.top() == eObject)