Update the vendored kernel to the JSON writer fix
This commit is contained in:
1 parent
49f3a0f040
commit
ce3de51f8f
4 files changed
+105
-72
No files matched your search
@@ -7,6 +7,8 @@ namespace joc::json {
|
||||
|
||||
namespace {
|
||||
|
||||
const char kHexDigits[] = "0123456789abcdef";
|
||||
|
||||
void skip_space(const std::string& text, std::size_t* index) {
|
||||
while (*index < text.size() &&
|
||||
(text[*index] == ' ' || text[*index] == '\t' || text[*index] == '\n' ||
|
||||
@@ -109,6 +111,50 @@ bool read_compound(const std::string& text, std::size_t* index, std::string* out
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string quote(const std::string& text) {
|
||||
std::string out;
|
||||
out.reserve(text.size() + 2u);
|
||||
out.push_back('"');
|
||||
for (const char character : text) {
|
||||
const unsigned char value = static_cast<unsigned char>(character);
|
||||
switch (character) {
|
||||
case '"': out += "\\\""; break;
|
||||
case '\\': out += "\\\\"; break;
|
||||
case '\b': out += "\\b"; break;
|
||||
case '\f': out += "\\f"; break;
|
||||
case '\n': out += "\\n"; break;
|
||||
case '\r': out += "\\r"; break;
|
||||
case '\t': out += "\\t"; break;
|
||||
default:
|
||||
if (value < 0x20u) {
|
||||
out += "\\u00";
|
||||
out.push_back(kHexDigits[value >> 4u]);
|
||||
out.push_back(kHexDigits[value & 0x0Fu]);
|
||||
} else {
|
||||
out.push_back(character);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.push_back('"');
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string pretty_object(const std::vector<Field>& fields) {
|
||||
if (fields.empty()) {
|
||||
return "{}";
|
||||
}
|
||||
std::string out = "{";
|
||||
for (std::size_t index = 0u; index < fields.size(); ++index) {
|
||||
out += index == 0u ? "\n " : ",\n ";
|
||||
out += quote(fields[index].key);
|
||||
out += ": ";
|
||||
out += fields[index].value;
|
||||
}
|
||||
out += "\n}";
|
||||
return out;
|
||||
}
|
||||
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error) {
|
||||
out->clear();
|
||||
std::size_t index = 0;
|
||||
|
||||
@@ -16,6 +16,24 @@ struct Member {
|
||||
// Parses a top-level JSON object. Rejects non-objects and duplicate keys.
|
||||
bool parse_object(const std::string& text, std::vector<Member>* out, std::string* error);
|
||||
|
||||
// One member of an object being written: `key` is escaped here, `value` is
|
||||
// already JSON text (a quoted string, a number, a literal).
|
||||
struct Field {
|
||||
std::string key;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
// Escapes `text` as a JSON string literal, quotes included. Matches
|
||||
// json.dumps(..., ensure_ascii=False): the named escapes for `"` and `\` and
|
||||
// for U+0008/U+0009/U+000A/U+000C/U+000D, `\u00XX` for the other control
|
||||
// characters, and every other byte - UTF-8 included - passed through.
|
||||
std::string quote(const std::string& text);
|
||||
|
||||
// Renders a flat object the way json.dumps(members, ensure_ascii=False,
|
||||
// indent=2) does: two-space indentation, one member per line, the members in
|
||||
// the order given, and no trailing newline.
|
||||
std::string pretty_object(const std::vector<Field>& fields);
|
||||
|
||||
const Member* find(const std::vector<Member>& members, const std::string& key);
|
||||
|
||||
bool as_string(const Member& member, std::string* out);
|
||||
|
||||
Reference in new issue
Block a user