diff options
author | Rye Mutt <rye@alchemyviewer.org> | 2024-04-05 19:03:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-06 02:03:58 +0300 |
commit | 17e1f3692c5c1e9cbc6ba6895b312a8baae9aec2 (patch) | |
tree | 4e9bc0aa51758539dee0f01e53e8909ad71b7b09 /indra/newview | |
parent | 7dbdfda7d6fc94eda07dd8376fd47260dfd73964 (diff) |
Port from JsonCPP to Boost.Json for json parsing and serializing (#1054)
Diffstat (limited to 'indra/newview')
-rw-r--r-- | indra/newview/CMakeLists.txt | 1 | ||||
-rw-r--r-- | indra/newview/llappviewerwin32.cpp | 17 | ||||
-rw-r--r-- | indra/newview/llgltfmateriallist.cpp | 3 | ||||
-rw-r--r-- | indra/newview/llmarketplacefunctions.cpp | 2 | ||||
-rw-r--r-- | indra/newview/lltranslate.cpp | 226 | ||||
-rw-r--r-- | indra/newview/lltranslate.h | 5 | ||||
-rw-r--r-- | indra/newview/llwebprofile.cpp | 2 |
7 files changed, 113 insertions, 143 deletions
diff --git a/indra/newview/CMakeLists.txt b/indra/newview/CMakeLists.txt index 58aa04829b..3eb3fadbf1 100644 --- a/indra/newview/CMakeLists.txt +++ b/indra/newview/CMakeLists.txt @@ -19,7 +19,6 @@ include(EXPAT) include(Hunspell) include(ICU4C) include(JPEGEncoderBasic) -include(JsonCpp) include(LLAppearance) include(LLAudio) include(LLCA) diff --git a/indra/newview/llappviewerwin32.cpp b/indra/newview/llappviewerwin32.cpp index efda6e05b0..dcd48ebd33 100644 --- a/indra/newview/llappviewerwin32.cpp +++ b/indra/newview/llappviewerwin32.cpp @@ -75,7 +75,7 @@ // Bugsplat (http://bugsplat.com) crash reporting tool #ifdef LL_BUGSPLAT #include "BugSplat.h" -#include "json/reader.h" // JsonCpp +#include "boost/json.hpp" // Boost.Json #include "llagent.h" // for agent location #include "llviewerregion.h" #include "llvoavatarself.h" // for agent name @@ -722,24 +722,25 @@ bool LLAppViewerWin32::init() } else { - Json::Reader reader; - Json::Value build_data; - if (!reader.parse(inf, build_data, false)) // don't collect comments + boost::json::error_code ec; + boost::json::value build_data = boost::json::parse(inf, ec); + if(ec.failed()) { // gah, the typo is baked into Json::Reader API LL_WARNS("BUGSPLAT") << "Can't initialize BugSplat, can't parse '" << build_data_fname - << "': " << reader.getFormatedErrorMessages() << LL_ENDL; + << "': " << ec.what() << LL_ENDL; } else { - Json::Value BugSplat_DB = build_data["BugSplat DB"]; - if (!BugSplat_DB) + if (!build_data.is_object() || !build_data.as_object().contains("BugSplat DB")) { LL_WARNS("BUGSPLAT") << "Can't initialize BugSplat, no 'BugSplat DB' entry in '" << build_data_fname << "'" << LL_ENDL; } else { + boost::json::value BugSplat_DB = build_data.at("BugSplat DB"); + // Got BugSplat_DB, onward! std::wstring version_string(WSTRINGIZE(LL_VIEWER_VERSION_MAJOR << '.' << LL_VIEWER_VERSION_MINOR << '.' << @@ -761,7 +762,7 @@ bool LLAppViewerWin32::init() // have to convert normal wide strings to strings of __wchar_t sBugSplatSender = new MiniDmpSender( - WCSTR(BugSplat_DB.asString()), + WCSTR(boost::json::value_to<std::string>(BugSplat_DB)), WCSTR(LL_TO_WSTRING(LL_VIEWER_CHANNEL)), WCSTR(version_string), nullptr, // szAppIdentifier -- set later diff --git a/indra/newview/llgltfmateriallist.cpp b/indra/newview/llgltfmateriallist.cpp index 92c58a2dbc..7ff9277d73 100644 --- a/indra/newview/llgltfmateriallist.cpp +++ b/indra/newview/llgltfmateriallist.cpp @@ -47,9 +47,6 @@ #include "tinygltf/tiny_gltf.h" #include <strstream> -#include "json/reader.h" -#include "json/value.h" - #include <unordered_set> LLGLTFMaterialList gGLTFMaterialList; diff --git a/indra/newview/llmarketplacefunctions.cpp b/indra/newview/llmarketplacefunctions.cpp index 8784f403cb..2c98ba3d42 100644 --- a/indra/newview/llmarketplacefunctions.cpp +++ b/indra/newview/llmarketplacefunctions.cpp @@ -42,8 +42,6 @@ #include "llviewermedia.h" #include "llviewernetwork.h" #include "llviewerregion.h" -#include "json/reader.h" // JSON -#include "json/writer.h" // JSON #include "lleventcoro.h" #include "llcoros.h" #include "llcorehttputil.h" diff --git a/indra/newview/lltranslate.cpp b/indra/newview/lltranslate.cpp index 6526e1df92..02947c8c0f 100644 --- a/indra/newview/lltranslate.cpp +++ b/indra/newview/lltranslate.cpp @@ -36,11 +36,11 @@ #include "llversioninfo.h" #include "llviewercontrol.h" #include "llcoros.h" -#include "json/reader.h" #include "llcorehttputil.h" #include "llurlregistry.h" #include "stringize.h" +#include <boost/json.hpp> static const std::string AZURE_NOTRANSLATE_OPENING_TAG("<div translate=\"no\">"); static const std::string AZURE_NOTRANSLATE_CLOSING_TAG("</div>"); @@ -346,11 +346,11 @@ public: private: static void parseErrorResponse( - const Json::Value& root, + const boost::json::value& root, int& status, std::string& err_msg); static bool parseTranslation( - const Json::Value& root, + const boost::json::value& root, std::string& translation, std::string& detected_lang); static std::string getAPIKey(); @@ -399,19 +399,13 @@ bool LLGoogleTranslationHandler::parseResponse( std::string& detected_lang, std::string& err_msg) const { - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) - { - err_msg = reader.getFormatedErrorMessages(); - return false; - } - - if (!root.isObject()) // empty response? should not happen - { - return false; - } + boost::json::error_code ec; + boost::json::value root = boost::json::parse(body, ec); + if (ec.failed()) + { + err_msg = ec.what(); + return false; + } if (status != HTTP_OK) { @@ -432,48 +426,55 @@ bool LLGoogleTranslationHandler::isConfigured() const // static void LLGoogleTranslationHandler::parseErrorResponse( - const Json::Value& root, + const boost::json::value& root, int& status, std::string& err_msg) { - const Json::Value& error = root.get("error", 0); - if (!error.isObject() || !error.isMember("message") || !error.isMember("code")) - { - return; - } + boost::json::error_code ec; + auto message = root.find_pointer("/data/message", ec); + auto code = root.find_pointer("/data/code", ec); + if (!message || !code) + { + return; + } - err_msg = error["message"].asString(); - status = error["code"].asInt(); + auto message_val = boost::json::try_value_to<std::string>(*message); + auto code_val = boost::json::try_value_to<int>(*code); + if (!message_val || !code_val) + { + return; + } + + err_msg = message_val.value(); + status = code_val.value(); } // static bool LLGoogleTranslationHandler::parseTranslation( - const Json::Value& root, + const boost::json::value& root, std::string& translation, std::string& detected_lang) { - // JsonCpp is prone to aborting the program on failed assertions, - // so be super-careful and verify the response format. - const Json::Value& data = root.get("data", 0); - if (!data.isObject() || !data.isMember("translations")) - { - return false; - } + boost::json::error_code ec; + auto translated_text = root.find_pointer("/data/translations/0/translatedText", ec); + if (!translated_text) return false; - const Json::Value& translations = data["translations"]; - if (!translations.isArray() || translations.size() == 0) - { - return false; - } + auto text_val = boost::json::try_value_to<std::string>(*translated_text); + if (!text_val) + { + LL_WARNS() << "Failed to parse translation" << text_val.error() << LL_ENDL; + return false; + } - const Json::Value& first = translations[0U]; - if (!first.isObject() || !first.isMember("translatedText")) - { - return false; - } + translation = text_val.value(); + + auto language = root.find_pointer("/data/translations/0/detectedSourceLanguage", ec); + if (language) + { + auto lang_val = boost::json::try_value_to<std::string>(*language); + detected_lang = lang_val ? lang_val.value() : ""; + } - translation = first["translatedText"].asString(); - detected_lang = first.get("detectedSourceLanguage", "").asString(); return true; } @@ -655,12 +656,11 @@ bool LLAzureTranslationHandler::checkVerificationResponse( // Expected: "{\"error\":{\"code\":400000,\"message\":\"One of the request inputs is not valid.\"}}" // But for now just verify response is a valid json - Json::Value root; - Json::Reader reader; - - if (!reader.parse(response["error_body"].asString(), root)) + boost::json::error_code ec; + boost::json::value root = boost::json::parse(response["error_body"].asString(), ec); + if (ec.failed()) { - LL_DEBUGS("Translate") << "Failed to parse error_body:" << reader.getFormatedErrorMessages() << LL_ENDL; + LL_DEBUGS("Translate") << "Failed to parse error_body:" << ec.what() << LL_ENDL; return false; } @@ -679,57 +679,36 @@ bool LLAzureTranslationHandler::parseResponse( if (status != HTTP_OK) { if (http_response.has("error_body")) - err_msg = parseErrorResponse(http_response["error_body"].asString()); + err_msg = parseErrorResponse(http_response["error_body"].asString()); return false; } //Example: // "[{\"detectedLanguage\":{\"language\":\"en\",\"score\":1.0},\"translations\":[{\"text\":\"Hello, what is your name?\",\"to\":\"en\"}]}]" - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) - { - err_msg = reader.getFormatedErrorMessages(); - return false; - } - - if (!root.isArray()) // empty response? should not happen + boost::json::error_code ec; + boost::json::value root = boost::json::parse(body, ec); + if (ec.failed()) { + err_msg = ec.what(); return false; } + auto language = root.find_pointer("/0/detectedLanguage/language", ec); + if (!language) return false; - // Request succeeded, extract translation from the response. + auto translated_text = root.find_pointer("/0/translations/0/text", ec); + if (!translated_text) return false; - const Json::Value& data = root[0U]; - if (!data.isObject() - || !data.isMember("detectedLanguage") - || !data.isMember("translations")) + auto lang_val = boost::json::try_value_to<std::string>(*language); + auto text_val = boost::json::try_value_to<std::string>(*translated_text); + if (!lang_val || !text_val) { + LL_WARNS() << "Failed to parse translation" << lang_val.error() << text_val.error() << LL_ENDL; return false; } - const Json::Value& detectedLanguage = data["detectedLanguage"]; - if (!detectedLanguage.isObject() || !detectedLanguage.isMember("language")) - { - return false; - } - detected_lang = detectedLanguage["language"].asString(); - - const Json::Value& translations = data["translations"]; - if (!translations.isArray() || translations.size() == 0) - { - return false; - } - - const Json::Value& first = translations[0U]; - if (!first.isObject() || !first.isMember("text")) - { - return false; - } - - translation = first["text"].asString(); + detected_lang = lang_val.value(); + translation = text_val.value(); return true; } @@ -747,27 +726,25 @@ std::string LLAzureTranslationHandler::parseErrorResponse( // Expected: "{\"error\":{\"code\":400000,\"message\":\"One of the request inputs is not valid.\"}}" // But for now just verify response is a valid json with an error - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) + boost::json::error_code ec; + boost::json::value root = boost::json::parse(body, ec); + if (ec.failed()) { - return std::string(); + return {}; } - if (!root.isObject() || !root.isMember("error")) + auto err_msg = root.find_pointer("/error/message", ec); + if (!err_msg) { - return std::string(); + return {}; } - const Json::Value& error_map = root["error"]; - - if (!error_map.isObject() || !error_map.isMember("message")) + auto err_msg_val = boost::json::try_value_to<std::string>(*err_msg); + if (!err_msg_val) { - return std::string(); + return {}; } - - return error_map["message"].asString(); + return err_msg_val.value(); } // static @@ -974,39 +951,39 @@ bool LLDeepLTranslationHandler::parseResponse( //Example: // "{\"translations\":[{\"detected_source_language\":\"EN\",\"text\":\"test\"}]}" - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) + boost::json::error_code ec; + boost::json::value root = boost::json::parse(body, ec); + if (ec.failed()) { - err_msg = reader.getFormatedErrorMessages(); + err_msg = ec.message(); return false; } - if (!root.isObject() - || !root.isMember("translations")) // empty response? should not happen + auto detected_langp = root.find_pointer("/translations/0/detected_source_language", ec); + if (!detected_langp || ec.failed()) // empty response? should not happen { + err_msg = ec.message(); return false; } // Request succeeded, extract translation from the response. - const Json::Value& translations = root["translations"]; - if (!translations.isArray() || translations.size() == 0) + auto text_valp = root.find_pointer("/translations/0/text", ec); + if (!text_valp || ec.failed()) { + err_msg = ec.message(); return false; } - const Json::Value& data= translations[0U]; - if (!data.isObject() - || !data.isMember("detected_source_language") - || !data.isMember("text")) + auto lang_result = boost::json::try_value_to<std::string>(*detected_langp); + auto text_result = boost::json::try_value_to<std::string>(*text_valp); + if (!lang_result || !text_result) { return false; } - detected_lang = data["detected_source_language"].asString(); + detected_lang = lang_result.value(); LLStringUtil::toLower(detected_lang); - translation = data["text"].asString(); + translation = text_result.value(); return true; } @@ -1022,21 +999,24 @@ std::string LLDeepLTranslationHandler::parseErrorResponse( const std::string& body) { // Example: "{\"message\":\"One of the request inputs is not valid.\"}" - - Json::Value root; - Json::Reader reader; - - if (!reader.parse(body, root)) - { - return std::string(); + boost::json::error_code ec; + boost::json::value root = boost::json::parse(body, ec); + if (ec.failed()) + { + return {}; } - if (!root.isObject() || !root.isMember("message")) + auto message_ptr = root.find_pointer("/message", ec); + if (!message_ptr || ec.failed()) { - return std::string(); + return {}; } - return root["message"].asString(); + auto message_val = boost::json::try_value_to<std::string>(*message_ptr); + if (!message_val) + return {}; + + return message_val.value(); } // static diff --git a/indra/newview/lltranslate.h b/indra/newview/lltranslate.h index 4a5d80737c..30fb8d74df 100644 --- a/indra/newview/lltranslate.h +++ b/indra/newview/lltranslate.h @@ -32,11 +32,6 @@ #include "llsingleton.h" -namespace Json -{ - class Value; -} - class LLTranslationAPIHandler; /** * Entry point for machine translation services. diff --git a/indra/newview/llwebprofile.cpp b/indra/newview/llwebprofile.cpp index ade9bbd847..45388becc7 100644 --- a/indra/newview/llwebprofile.cpp +++ b/indra/newview/llwebprofile.cpp @@ -42,7 +42,7 @@ #include "llcorehttputil.h" // third-party -#include "json/reader.h" // JSON + /* * Workflow: |