86 enum class FrameType { Document, Element, Array };
93 std::string attributes;
94 std::set<std::string> attributeNames;
97 Frame(FrameType t, std::string n, std::size_t d) : type(t), name(std::move(n)), depth(d) {}
103 std::stringstream result_;
105 std::vector<Frame> frames_;
107 bool key_is_attribute_ =
false;
110 bool fail(
const std::string& message) {
117 bool failNotObject() {
118 return fail(
"a json object is expected at top level");
121 std::string indentation(std::size_t depth)
const {
122 return std::string(depth * tab_spaces_,
' ');
126 static std::size_t childDepth(
const Frame& frame) {
127 switch (frame.type) {
128 case FrameType::Document:
130 case FrameType::Element:
131 return frame.depth + 1;
139 return frames_.back().type == FrameType::Array ? frames_[frames_.size() - 2] : frames_.back();
143 std::string location()
const {
145 for (
const auto& frame : frames_) {
146 if (frame.type == FrameType::Element) result +=
"/" + frame.name;
148 const Frame& top = frames_.back();
149 if (top.type == FrameType::Array) {
150 result +=
"/" + top.name +
"[]";
153 result +=
"/" + (key_is_attribute_ ? std::string(1, attr_prefix_) : std::string()) + key_;
159 std::pair<std::string, std::size_t> newElement()
const {
160 const Frame& top = frames_.back();
161 return {top.type == FrameType::Array ? top.name : key_, childDepth(top)};
164 void appendElement(Frame& target,
const std::string& name, std::size_t depth,
165 const std::string& attributes,
const std::string& children) {
166 target.children += indentation(depth) +
"<" + name + attributes;
167 if (children.empty()) {
168 target.children +=
"/>\n";
171 target.children +=
">\n" + children + indentation(depth) +
"</" + name +
">\n";
175 static bool isNameStartChar(
unsigned char c) {
177 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || c ==
'_' || c ==
':' || c >= 0x80;
180 static bool isNameChar(
unsigned char c) {
181 return isNameStartChar(c) || (c >=
'0' && c <=
'9') || c ==
'-' || c ==
'.';
184 static bool isValidName(
const std::string& name) {
185 if (name.empty() || !isNameStartChar(
static_cast<unsigned char>(name[0])))
return false;
186 for (
char c : name) {
187 if (!isNameChar(
static_cast<unsigned char>(c)))
return false;
193 static bool escape(
const std::string& value, std::string& out) {
194 for (
char ch : value) {
195 const unsigned char c =
static_cast<unsigned char>(ch);
219 if (c < 0x20)
return false;
227 bool scalar(
const std::string& value,
const std::string& type) {
228 if (frames_.empty())
return failNotObject();
229 Frame& top = frames_.back();
230 if (top.type == FrameType::Array || !key_is_attribute_) {
231 return fail(type +
" value at '" + location() +
"': text content is not supported (only attributes have scalar values)");
234 if (!escape(value, escaped)) {
235 return fail(
"value of attribute '" + location() +
"' has a character not allowed in XML");
237 if (!top.attributeNames.insert(key_).second) {
238 return fail(
"repeated attribute '" + location() +
"'");
240 top.attributes +=
" " + key_ +
"=\"" + escaped +
"\"";
244 bool failAttributeType(
const std::string& type) {
245 return fail(type +
" value for attribute '" + location() +
"': must be a string, number or boolean");
257 tab_spaces_(tabSpaces >= 1 ? tabSpaces : 4),
258 attr_prefix_(attrPrefix) {}
271 return result_.str();
284 if (frames_.empty())
return failNotObject();
285 if (frames_.back().type != FrameType::Array && key_is_attribute_)
return failAttributeType(
"null");
286 const auto element = newElement();
287 appendElement(owner(), element.first, element.second,
"",
"");
293 return scalar(val ?
"true" :
"false",
"boolean");
298 return scalar(std::to_string(val),
"number");
303 return scalar(std::to_string(val),
"number");
308 return scalar(s,
"number");
313 return scalar(val,
"string");
318 if (frames_.empty())
return failNotObject();
319 return fail(
"binary value at '" + location() +
"' is not supported");
324 if (frames_.empty()) {
327 frames_.emplace_back(FrameType::Document,
"", 0);
330 if (frames_.back().type != FrameType::Array && key_is_attribute_)
return failAttributeType(
"object");
331 auto element = newElement();
332 frames_.emplace_back(FrameType::Element, std::move(element.first), element.second);
338 Frame frame = std::move(frames_.back());
341 if (frame.type == FrameType::Document) {
342 if (frame.children.empty())
return fail(
"the json object has no element");
343 frame.children.pop_back();
344 result_ << frame.children;
348 appendElement(owner(), frame.name, frame.depth, frame.attributes, frame.children);
354 if (frames_.empty())
return failNotObject();
355 if (frames_.back().type == FrameType::Array) {
356 return fail(
"nested array at '" + location() +
"' is not supported");
358 if (key_is_attribute_)
return failAttributeType(
"array");
359 auto element = newElement();
360 frames_.emplace_back(FrameType::Array, std::move(element.first), element.second);
370 bool key(string_t& val)
override
372 const bool attribute = !val.empty() && val[0] == attr_prefix_;
373 key_ = attribute ? val.substr(1) : val;
374 key_is_attribute_ = attribute;
376 if (!isValidName(key_)) {
377 return fail(std::string(attribute ?
"attribute" :
"element") +
" name '" + val +
"' at '" + location() +
"' is not a valid XML name");
379 if (attribute && frames_.back().type == FrameType::Document) {
380 return fail(
"attribute '" + val +
"' at top level: attributes belong to an element");
385 bool parse_error(std::size_t ,
const std::string& ,
const nlohmann::json::exception& ex)
override
387 return fail(ex.what());