Fixed json parsing with hexadecimal characters

* replace \x with \u00 to follow JSON spec
* fixes #2475 and #2495
* added unit tests for json parsing
This commit is contained in:
zjeffer
2023-09-10 20:36:27 +02:00
parent f744d906be
commit 8f5d0098d6
3 changed files with 71 additions and 14 deletions

45
test/JsonParser.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "util/json.hpp"
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif
TEST_CASE("Simple json", "[json]") {
SECTION("Parse simple json") {
std::string stringToTest = R"({"number": 5, "string": "test"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["number"].asInt() == 5);
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);
// 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() == "你好");
}
}

View File

@ -8,6 +8,7 @@ test_dep = [
]
test_src = files(
'main.cpp',
'JsonParser.cpp',
'SafeSignal.cpp',
'config.cpp',
'../src/config.cpp',