jml icon indicating copy to clipboard operation
jml copied to clipboard

JSON parser: Add readJsonString(), refactor all other JSON string functions to use it.

Open LEW21 opened this issue 11 years ago • 1 comments

While applying Jeremy's comments to my former patch, I've found a better way to organize all that JSON string-related code. This way, all of the 4 jml's json string functions can be refactored to use just one implementation, and also the Unicode-aware parser is much simpler to write:

auto result = growing_buffer{};
readJsonString(context,
[&](char c) {
    result.push_back(c);
},
[&](char16_t c) {
    auto buffer = char[4]{};
    auto end = utf8::unchecked::append(c, buffer);
    for (auto i = buffer; buffer != end; ++i)
        result.push_back(*i);
});
return string{result};

(Updated soa pull request will come later.)

LEW21 avatar Aug 30 '13 15:08 LEW21

Ah, almost forgotten. JSON's Unicode escape sequences support only UTF-16, and that's why I'm using UTF-16 here, and not 32bit codepoints. To escape non-BMP chars, two consecutive escape sequences (like \u1234\u5678) are used.

This fact makes it a bit harder to write good Unicode-aware parser than I've written in the pull request description. Still, I'll write it.

(And the current soa's parser also does not support non-BMP characters. It would be a nightmare to add them to its spaghetti code...)

LEW21 avatar Aug 30 '13 16:08 LEW21