From c9ee4387c330fa257a5e682f542feb4329475200 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 14 Jan 2018 06:47:07 +0500 Subject: Implemented basic chat-component parsing --- src/Chat.cpp | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Chat.cpp b/src/Chat.cpp index 0857d6c..193c1fd 100644 --- a/src/Chat.cpp +++ b/src/Chat.cpp @@ -1,33 +1,29 @@ #include "Chat.hpp" #include -#include -Chat::Chat(const std::string &str) { - using nlohmann::json; - json j = json::parse(str); +using nlohmann::json; - /*LOG(WARNING) << j.dump(4); +void Recursive(const json::iterator &iter, bool isIterObj, std::string &str) { + if (isIterObj && iter.key() == "text") { + str.append(iter->get() + " "); + } - std::function iterating = [&](json::iterator iter) { - json val = *iter; + if (iter->is_structured()) { + for (json::iterator it = iter->begin(); it != iter->end(); ++it) + Recursive(it, iter->is_object(), str); + } +} - if (val.is_object() && val.find("text") != val.end()) { - text.append(val["text"].get()); - } +Chat::Chat(const std::string &str) { + json j = json::parse(str); - if (val.is_array() || val.is_object()) { - for (auto it = val.begin(); it != val.end(); ++it) { - iterating(it); - } + if (j["translate"] == "chat.type.text") { + text = j["with"][0]["text"].get() + ": " + j["with"][1].get(); + } else + for (json::iterator it = j.begin(); it != j.end(); ++it) { + Recursive(it, j.is_object(), text); } - }; - - for (auto it = j.begin(); it != j.end(); ++it) { - iterating(it); - }*/ - - text = j.dump(4); } std::string Chat::ToJson() const { -- cgit v1.2.3