From 6c7e6a170fd7281aa8037455133830827a524362 Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 22:15:36 +0300 Subject: [PATCH] Use Void::Json typed accessors instead of local jHelpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace YouTubeClient's local jHas/jStr/jNum/jObj/jArr free-function helpers (and their ~77 call sites) with the new Void::Json::Value member accessors has/get_str/get_num/child_object/child_array. Same safe semantics (missing/wrong-type → default/nullptr), now shared across the project. Requires the VoidLibrary re-pin that adds the accessors. Co-Authored-By: Claude Opus 4.8 --- src/YouTubeClient.cpp | 190 ++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 111 deletions(-) diff --git a/src/YouTubeClient.cpp b/src/YouTubeClient.cpp index b753c9f..b3ff6c0 100644 --- a/src/YouTubeClient.cpp +++ b/src/YouTubeClient.cpp @@ -18,41 +18,9 @@ namespace { - // Safe Void::Json::Value accessors for navigating dynamic API responses - // (the former glz::generic helpers). Missing or wrong-typed fields yield a - // default value / nullptr instead of throwing. + // Local alias for navigating dynamic API responses; the safe typed getters + // (has/get_str/get_num/child_object/child_array) live on Void::Json::Value. using JValue = Void::Json::Value; - - [[nodiscard]] bool jHas(const JValue& v, std::string_view key) - { - return v.find(key) != nullptr; - } - - [[nodiscard]] std::string jStr(const JValue& v, std::string_view key) - { - const JValue* c = v.find(key); - return (c && c->is_string()) ? c->as_string() : std::string{}; - } - - [[nodiscard]] double jNum(const JValue& v, std::string_view key) - { - const JValue* c = v.find(key); - return (c && c->is_number()) ? c->as_number() : 0.0; - } - - // Returns a child only when it exists and is an object. - [[nodiscard]] const JValue* jObj(const JValue& v, std::string_view key) - { - const JValue* c = v.find(key); - return (c && c->is_object()) ? c : nullptr; - } - - // Returns a child only when it exists and is an array. - [[nodiscard]] const JValue* jArr(const JValue& v, std::string_view key) - { - const JValue* c = v.find(key); - return (c && c->is_array()) ? c : nullptr; - } } // namespace namespace Echoion::Core @@ -325,16 +293,16 @@ namespace Echoion::Core const JValue& json = *parsed; Api::TrackInfo info; - info.id = jStr(json, "id"); - if (jHas(json, "title")) { - info.title = Void::Text::html_unescape(jStr(json, "title")); + info.id = (json).get_str("id"); + if ((json).has("title")) { + info.title = Void::Text::html_unescape((json).get_str("title")); } - info.artist = jStr(json, "uploader"); - if (jHas(json, "duration")) { + info.artist = (json).get_str("uploader"); + if ((json).has("duration")) { info.duration = std::chrono::seconds( - static_cast(jNum(json, "duration"))); + static_cast((json).get_num("duration"))); } - info.thumbnailUrl = jStr(json, "thumbnail"); + info.thumbnailUrl = (json).get_str("thumbnail"); // Set originalUrl so we can extract video ID later for streaming info.originalUrl = @@ -516,29 +484,29 @@ namespace Echoion::Core result.query = std::string(query); // Extract total results - if (const JValue* pageInfo = jObj(json, "pageInfo")) { - if (jHas(*pageInfo, "totalResults")) { + if (const JValue* pageInfo = (json).child_object("pageInfo")) { + if ((*pageInfo).has("totalResults")) { result.totalResults = static_cast( - jNum(*pageInfo, "totalResults")); + (*pageInfo).get_num("totalResults")); } } // Store next page token if available - if (jHas(json, "nextPageToken")) { - result.nextPageToken = jStr(json, "nextPageToken"); + if ((json).has("nextPageToken")) { + result.nextPageToken = (json).get_str("nextPageToken"); } // Extract search results - if (const JValue* items = jArr(json, "items")) { + if (const JValue* items = (json).child_array("items")) { std::vector videoIds; for (const JValue& item : items->as_array()) { - const JValue* id = jObj(item, "id"); - if (!id || !jHas(*id, "videoId")) { + const JValue* id = (item).child_object("id"); + if (!id || !(*id).has("videoId")) { continue; } - std::string videoId = jStr(*id, "videoId"); + std::string videoId = (*id).get_str("videoId"); videoIds.push_back(videoId); Api::TrackInfo track; @@ -547,26 +515,26 @@ namespace Echoion::Core track.originalUrl = std::format( "https://www.youtube.com/watch?v={}", videoId); - if (const JValue* snippet = jObj(item, "snippet")) { - if (jHas(*snippet, "title")) { + if (const JValue* snippet = (item).child_object("snippet")) { + if ((*snippet).has("title")) { track.title = - Void::Text::html_unescape(jStr(*snippet, "title")); + Void::Text::html_unescape((*snippet).get_str("title")); } - if (jHas(*snippet, "channelTitle")) { + if ((*snippet).has("channelTitle")) { track.artist = Void::Text::html_unescape( - jStr(*snippet, "channelTitle")); + (*snippet).get_str("channelTitle")); } if (const JValue* thumbnails = - jObj(*snippet, "thumbnails")) { + (*snippet).child_object("thumbnails")) { // Get the highest quality thumbnail available - if (const JValue* t = jObj(*thumbnails, "high")) { - track.thumbnailUrl = jStr(*t, "url"); + if (const JValue* t = (*thumbnails).child_object("high")) { + track.thumbnailUrl = (*t).get_str("url"); } else if (const JValue* m = - jObj(*thumbnails, "medium")) { - track.thumbnailUrl = jStr(*m, "url"); + (*thumbnails).child_object("medium")) { + track.thumbnailUrl = (*m).get_str("url"); } else if (const JValue* d = - jObj(*thumbnails, "default")) { - track.thumbnailUrl = jStr(*d, "url"); + (*thumbnails).child_object("default")) { + track.thumbnailUrl = (*d).get_str("url"); } } } @@ -682,19 +650,19 @@ namespace Echoion::Core // Create a map of video ID to duration std::unordered_map durationMap; - if (const JValue* items = jArr(json, "items")) { + if (const JValue* items = (json).child_array("items")) { for (const JValue& item : items->as_array()) { const JValue* contentDetails = - jObj(item, "contentDetails"); - if (!jHas(item, "id") || !contentDetails) { + (item).child_object("contentDetails"); + if (!(item).has("id") || !contentDetails) { continue; } - std::string videoId = jStr(item, "id"); + std::string videoId = (item).get_str("id"); - if (jHas(*contentDetails, "duration")) { + if ((*contentDetails).has("duration")) { std::string durationStr = - jStr(*contentDetails, "duration"); + (*contentDetails).get_str("duration"); auto duration = Void::Time::parse_iso8601_interval(durationStr).value_or(std::chrono::seconds{0}); if (duration.count() > 0) { ECHOION_LOG_DEBUG( @@ -774,29 +742,29 @@ namespace Echoion::Core Api::SearchResult searchResult; searchResult.query = std::string(query); - if (const JValue* entries = jArr(json, "entries")) { + if (const JValue* entries = (json).child_array("entries")) { for (const JValue& entry : entries->as_array()) { - if (!jHas(entry, "id")) { + if (!(entry).has("id")) { continue; } Api::TrackInfo track; - track.id = jStr(entry, "id"); - if (jHas(entry, "title")) { + track.id = (entry).get_str("id"); + if ((entry).has("title")) { track.title = - Void::Text::html_unescape(jStr(entry, "title")); + Void::Text::html_unescape((entry).get_str("title")); } - if (jHas(entry, "uploader")) { + if ((entry).has("uploader")) { track.artist = - Void::Text::html_unescape(jStr(entry, "uploader")); + Void::Text::html_unescape((entry).get_str("uploader")); } - if (jHas(entry, "duration")) { + if ((entry).has("duration")) { track.duration = std::chrono::seconds(static_cast( - jNum(entry, "duration"))); + (entry).get_num("duration"))); } - if (jHas(entry, "thumbnail")) { - track.thumbnailUrl = jStr(entry, "thumbnail"); + if ((entry).has("thumbnail")) { + track.thumbnailUrl = (entry).get_str("thumbnail"); } // Set originalUrl so we can extract video ID later for // streaming @@ -1036,38 +1004,38 @@ namespace Echoion::Core } const JValue& json = *parsed; - if (const JValue* itemsArr = jArr(json, "items")) { + if (const JValue* itemsArr = (json).child_array("items")) { for (const JValue& item : itemsArr->as_array()) { - const JValue* snippet = jObj(item, "snippet"); + const JValue* snippet = (item).child_object("snippet"); const JValue* contentDetails = - jObj(item, "contentDetails"); + (item).child_object("contentDetails"); if (!snippet || !contentDetails) { continue; } Api::YouTubePlaylistItem playlistItem; - if (jHas(*contentDetails, "videoId")) { + if ((*contentDetails).has("videoId")) { playlistItem.videoId = - jStr(*contentDetails, "videoId"); + (*contentDetails).get_str("videoId"); } - if (jHas(*snippet, "title")) { - playlistItem.title = jStr(*snippet, "title"); + if ((*snippet).has("title")) { + playlistItem.title = (*snippet).get_str("title"); } if (const JValue* thumbnails = - jObj(*snippet, "thumbnails")) { + (*snippet).child_object("thumbnails")) { // Get the highest quality thumbnail available if (const JValue* t = - jObj(*thumbnails, "high")) { - playlistItem.thumbnailUrl = jStr(*t, "url"); + (*thumbnails).child_object("high")) { + playlistItem.thumbnailUrl = (*t).get_str("url"); } else if (const JValue* m = - jObj(*thumbnails, "medium")) { - playlistItem.thumbnailUrl = jStr(*m, "url"); + (*thumbnails).child_object("medium")) { + playlistItem.thumbnailUrl = (*m).get_str("url"); } else if (const JValue* d = - jObj(*thumbnails, "default")) { - playlistItem.thumbnailUrl = jStr(*d, "url"); + (*thumbnails).child_object("default")) { + playlistItem.thumbnailUrl = (*d).get_str("url"); } } @@ -1076,8 +1044,8 @@ namespace Echoion::Core } // Check for next page - if (jHas(json, "nextPageToken")) { - pageToken = jStr(json, "nextPageToken"); + if ((json).has("nextPageToken")) { + pageToken = (json).get_str("nextPageToken"); } else { pageToken = std::nullopt; } @@ -1125,22 +1093,22 @@ namespace Echoion::Core } const JValue& json = *parsed; - if (const JValue* entries = jArr(json, "entries")) { + if (const JValue* entries = (json).child_array("entries")) { for (const JValue& entry : entries->as_array()) { - if (!jHas(entry, "id")) { + if (!(entry).has("id")) { continue; } Api::YouTubePlaylistItem item; - item.videoId = jStr(entry, "id"); - if (jHas(entry, "title")) { - item.title = Void::Text::html_unescape(jStr(entry, "title")); + item.videoId = (entry).get_str("id"); + if ((entry).has("title")) { + item.title = Void::Text::html_unescape((entry).get_str("title")); } - if (const JValue* thumbs = jArr(entry, "thumbnails")) { + if (const JValue* thumbs = (entry).child_array("thumbnails")) { const auto& arr = thumbs->as_array(); if (!arr.empty() && arr.back().is_object() && arr.back().find("url")) { - item.thumbnailUrl = jStr(arr.back(), "url"); + item.thumbnailUrl = (arr.back()).get_str("url"); } } @@ -1208,19 +1176,19 @@ namespace Echoion::Core const JValue& json = *parsed; Api::TrackInfo track; - track.id = jStr(json, "id"); - if (jHas(json, "title")) { - track.title = Void::Text::html_unescape(jStr(json, "title")); + track.id = (json).get_str("id"); + if ((json).has("title")) { + track.title = Void::Text::html_unescape((json).get_str("title")); } - if (jHas(json, "uploader")) { - track.artist = Void::Text::html_unescape(jStr(json, "uploader")); + if ((json).has("uploader")) { + track.artist = Void::Text::html_unescape((json).get_str("uploader")); } - if (jHas(json, "duration")) { + if ((json).has("duration")) { track.duration = std::chrono::seconds( - static_cast(jNum(json, "duration"))); + static_cast((json).get_num("duration"))); } - if (jHas(json, "thumbnail")) { - track.thumbnailUrl = jStr(json, "thumbnail"); + if ((json).has("thumbnail")) { + track.thumbnailUrl = (json).get_str("thumbnail"); } track.originalUrl = url;