arrume isso #include <iostream> #include <map> #include <string> #include <sstream>…
- User
arrume isso #include #include #include #include #include #include <winsock2.h> #include <windows.h>
// Função de conversão para string template std::string to_string(T value) { std::ostringstream oss; oss << value; return oss.str(); }
// Classe para gerenciar JSON class JSONManager { public: // Estrutura para representar um valor JSON struct JSONValue { enum Type { StringType, IntType, DoubleType }; Type type; std::string value;
JSONValue() : type(StringType), value("") {} JSONValue(Type t, const std::string& v) : type(t), value(v) {} JSONValue(int v) : type(IntType) { value = intToString(v); } JSONValue(double v) : type(DoubleType) { value = doubleToString(v); } JSONValue(const std::string& v) : type(StringType), value(v) {} std::string intToString(int n) { std::stringstream ss; ss << n; return ss.str(); } std::string doubleToString(double d) { std::stringstream ss; ss << d; return ss.str(); } }; // Método para serializar um objeto JSON para string static std::string serialize(const std::map<std::string, JSONValue>& jsonObject) { std::string result = "{"; std::map<std::string, JSONValue>::const_iterator it = jsonObject.begin(); while (it != jsonObject.end()) { result += "\"" + it->first + "\":" + "\"" + valueToString(it->second) + "\""; if (++it != jsonObject.end()) { result += ","; } } result += "}"; return result; } // Método para desserializar uma string JSON para um objeto JSON static std::map<std::string, JSONValue> deserialize(const std::string& jsonString) { std::map<std::string, JSONValue> result; size_t startPos = jsonString.find("{") + 1; size_t endPos = jsonString.rfind("}"); std::string jsonContent = jsonString.substr(startPos, endPos - startPos); size_t pos = 0; while (pos < jsonContent.size()) { size_t colonPos = jsonContent.find(":", pos); size_t commaPos = jsonContent.find(",", pos); std::string key = jsonContent.substr(pos, colonPos - pos); key = removeQuotes(key); std::string value; if (commaPos != std::string::npos) { value = jsonContent.substr(colonPos + 1, commaPos - colonPos - 1); } else { value = jsonContent.substr(colonPos + 1); } JSONValue typedValue = convertFromString(value); result.insert(std::make_pair(key, typedValue)); if (commaPos != std::string::npos) { pos = commaPos + 1; } else { break; } } return result; } // Método para modificar o valor de uma chave em um objeto JSON static bool modifyValue(std::map<std::string, JSONValue>& jsonObject, const std::string& key, const JSONValue& newValue) { std::map<std::string, JSONValue>::iterator it = jsonObject.find(key); if (it != jsonObject.end()) { it->second = newValue; return true; } else { return false; } }private: // Método auxiliar para converter um valor JSON para string static std::string valueToString(const JSONValue& value) { return value.value; }
- Assistant