Skip to content

Commit

Permalink
Fixed json parsing with hexadecimal characters, added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zjeffer committed Jan 14, 2024
1 parent e3875ba commit abd4cec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
37 changes: 24 additions & 13 deletions include/util/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <fmt/ostream.h>
#include <json/json.h>

#include <algorithm>
#include <codecvt>
#include <iostream>
#include <locale>
#include <regex>

#if (FMT_VERSION >= 90000)

template <>
Expand All @@ -12,25 +18,30 @@ struct fmt::formatter<Json::Value> : ostream_formatter {};

namespace waybar::util {

struct JsonParser {
class JsonParser {
public:
JsonParser() = default;

Json::Value parse(const std::string& data) const {
Json::Value root(Json::objectValue);
if (data.empty()) {
return root;
Json::Value parse(const std::string& jsonStr) {
Json::Value root;

// replace all occurrences of "\x" with "\u00", because JSON doesn't allow "\x" escape sequences
std::string modifiedJsonStr = replaceHexadecimalEscape(jsonStr);

std::istringstream jsonStream(modifiedJsonStr);
std::string errs;
if (!Json::parseFromStream(m_readerBuilder, jsonStream, &root, &errs)) {
throw std::runtime_error("Error parsing JSON: " + errs);
}
std::unique_ptr<Json::CharReader> const reader(builder_.newCharReader());
std::string err;
bool res = reader->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
if (!res) throw std::runtime_error(err);
return root;
}

~JsonParser() = default;

private:
Json::CharReaderBuilder builder_;
};
Json::CharReaderBuilder m_readerBuilder;

static std::string replaceHexadecimalEscape(const std::string& str) {
static std::regex re("\\\\x");
return std::regex_replace(str, re, "\\u00");
}
};
} // namespace waybar::util
29 changes: 24 additions & 5 deletions test/JsonParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,34 @@ TEST_CASE("Simple json", "[json]") {
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["number"].asInt() == 5);
REQUIRE(jsonValue["string"].asString() == "test");
REQUIRE(jsonValue["string"].asString() == "test");
}
}

TEST_CASE("Json with unicode", "[json]") {
SECTION("Parse json with unicode") {
std::string stringToTest = R"({"test": "\xab"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["test"].asString() == "\xab");
std::string stringToTest = R"({"test": "\xab"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
// compare with "\u00ab" because "\xab" is replaced with "\u00ab" in the parser
REQUIRE(jsonValue["test"].asString() == "\u00ab");
}
}

TEST_CASE("Json with emoji", "[json]") {
SECTION("Parse json with emoji") {
std::string stringToTest = R"({"test": "😊"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["test"].asString() == "😊");
}
}

TEST_CASE("Json with chinese characters", "[json]") {
SECTION("Parse json with chinese characters") {
std::string stringToTest = R"({"test": "你好"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["test"].asString() == "你好");
}
}

0 comments on commit abd4cec

Please sign in to comment.