COMMON: Add helper function for using Strings with JSON::parse

This commit is contained in:
Cameron Cawley
2025-11-06 15:50:12 +00:00
committed by Filippos Karapetis
parent 095edf14f9
commit c3a29cacc6
7 changed files with 14 additions and 14 deletions
@@ -112,7 +112,7 @@ void OneDriveTokenRefresher::finishJson(const Common::JSONValue *json) {
void OneDriveTokenRefresher::finishError(const Networking::ErrorResponse &error, Networking::RequestState state) {
if (error.failed) {
Common::JSONValue *value = Common::JSON::parse(error.response.c_str());
Common::JSONValue *value = Common::JSON::parse(error.response);
//somehow OneDrive returns JSON with '.' in unexpected places, try fixing it
if (!value) {
@@ -121,7 +121,7 @@ void OneDriveTokenRefresher::finishError(const Networking::ErrorResponse &error,
if (fixedResponse[i] == '.')
fixedResponse.replace(i, 1, " ");
}
value = Common::JSON::parse(fixedResponse.c_str());
value = Common::JSON::parse(fixedResponse);
}
if (value) {
+2 -2
View File
@@ -188,7 +188,7 @@ void SavesSyncRequest::directoryListedErrorCallback(const Networking::ErrorRespo
bool irrecoverable = error.interrupted || error.failed;
if (error.failed) {
Common::JSONValue *value = Common::JSON::parse(error.response.c_str());
Common::JSONValue *value = Common::JSON::parse(error.response);
// Somehow OneDrive returns JSON with '.' in unexpected places, try fixing it
if (!value) {
@@ -197,7 +197,7 @@ void SavesSyncRequest::directoryListedErrorCallback(const Networking::ErrorRespo
if (fixedResponse[i] == '.')
fixedResponse.replace(i, 1, " ");
}
value = Common::JSON::parse(fixedResponse.c_str());
value = Common::JSON::parse(fixedResponse);
}
if (value) {
+3
View File
@@ -155,6 +155,9 @@ public:
/** Prepares raw bytes in a given stream to be parsed with Common::JSON::parse(). */
static char *zeroTerminateContents(Common::MemoryWriteStreamDynamic &stream);
static JSONValue *parse(const Common::String &data) {
return parse(data.c_str());
}
static JSONValue *parse(const char *data);
static String stringify(const JSONValue *value);
protected:
+1 -1
View File
@@ -111,7 +111,7 @@ void Lobby::receiveData() {
void Lobby::processLine(Common::String line) {
debugC(DEBUG_NETWORK, "LOBBY: Received Data: %s", line.c_str());
Common::JSONValue *json = Common::JSON::parse(line.c_str());
Common::JSONValue *json = Common::JSON::parse(line);
if (!json) {
warning("LOBBY: Received trunciated data from server! %s", line.c_str());
return;
+4 -4
View File
@@ -825,7 +825,7 @@ int Net::remoteSendData(int typeOfSend, int sendTypeParam, int type, Common::Str
reliable == true ? "true" : "false", data.c_str());
debugC(DEBUG_NETWORK, "NETWORK: Sending data: %s", res.c_str());
Common::JSONValue *str = Common::JSON::parse(res.c_str());
Common::JSONValue *str = Common::JSON::parse(res);
if (_isHost) {
_hostDataQueue.push(str);
_peerIndexQueue.push(sendTypeParam - 1);
@@ -975,7 +975,7 @@ void Net::serviceSessionServer() {
void Net::handleSessionServerData(Common::String data) {
debugC(DEBUG_NETWORK, "NETWORK: Received data from session server. Data: %s", data.c_str());
Common::JSONValue *json = Common::JSON::parse(data.c_str());
Common::JSONValue *json = Common::JSON::parse(data);
if (!json) {
warning("NETWORK: Received non-JSON string from session server, \"%s\", ignoring", data.c_str());
return;
@@ -1126,7 +1126,7 @@ bool Net::serviceBroadcast() {
void Net::handleBroadcastData(Common::String data, Common::String host, int port) {
debugC(DEBUG_NETWORK, "NETWORK: Received data from broadcast socket. Source: %s:%d Data: %s", host.c_str(), port, data.c_str());
Common::JSONValue *json = Common::JSON::parse(data.c_str());
Common::JSONValue *json = Common::JSON::parse(data);
if (!json) {
// Just about anything could come from the broadcast address, so do not warn.
debugC(DEBUG_NETWORK, "NETWORK: Not a JSON string, ignoring.");
@@ -1271,7 +1271,7 @@ void Net::remoteReceiveData() {
break;
}
Common::JSONValue *json = Common::JSON::parse(data.c_str());
Common::JSONValue *json = Common::JSON::parse(data);
if (!json) {
// Just about anything could come from the broadcast address, so do not warn.
warning("NETWORK: Received non-JSON string. Got: \"%s\"", data.c_str());
+1 -1
View File
@@ -48,7 +48,7 @@ static void parseFrame(const Common::String &key, const Common::JSONObject &valu
}
void SpriteSheet::parseSpriteSheet(const Common::String &contents) {
Common::ScopedPtr<Common::JSONValue> json(Common::JSON::parse(contents.c_str()));
Common::ScopedPtr<Common::JSONValue> json(Common::JSON::parse(contents));
const Common::JSONObject &obj = json->asObject()["frames"]->asObject();
for (auto it = obj.begin(); it != obj.end(); it++) {
parseFrame(it->_key, it->_value->asObject(), _frameTable[it->_key]);
+1 -4
View File
@@ -549,10 +549,7 @@ void CloudConnectionWizard::manualModeConnect() {
}
// parse JSON and display message if failed
Common::MemoryWriteStreamDynamic jsonStream(DisposeAfterUse::YES);
jsonStream.write(code.c_str(), code.size());
char *contents = Common::JSON::zeroTerminateContents(jsonStream);
Common::JSONValue *json = Common::JSON::parse(contents);
Common::JSONValue *json = Common::JSON::parse(code);
// pass JSON to the manager
_connecting = true;