comparison vamp-json/VampJson.h @ 184:150cfa0c71e1

Merge pull request #1 from piper-audio/fix/regression-json-responses Fix/regression json responses
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 03 Feb 2017 13:00:42 +0000
parents 03b067abd91d
children 3eb00e5c76c4
comparison
equal deleted inserted replaced
172:cfa115746cb3 184:150cfa0c71e1
303 if (!j.is_object()) { 303 if (!j.is_object()) {
304 err = "object expected for output descriptor"; 304 err = "object expected for output descriptor";
305 return {}; 305 return {};
306 } 306 }
307 307
308 od = toConfiguredOutputDescriptor(j, err); 308 od = toConfiguredOutputDescriptor(j["configured"], err);
309 if (failed(err)) return {}; 309 if (failed(err)) return {};
310 310
311 toBasicDescriptor(j["basic"], od, err); 311 toBasicDescriptor(j["basic"], od, err);
312 if (failed(err)) return {}; 312 if (failed(err)) return {};
313 313
847 847
848 static ListResponse 848 static ListResponse
849 toListResponse(json11::Json j, std::string &err) { 849 toListResponse(json11::Json j, std::string &err) {
850 850
851 ListResponse resp; 851 ListResponse resp;
852 for (const auto &a: j["result"]["available"].array_items()) { 852 for (const auto &a: j["available"].array_items()) {
853 resp.available.push_back(toPluginStaticData(a, err)); 853 resp.available.push_back(toPluginStaticData(a, err));
854 if (failed(err)) return {}; 854 if (failed(err)) return {};
855 } 855 }
856 return resp; 856 return resp;
857 } 857 }
1107 } 1107 }
1108 } 1108 }
1109 1109
1110 static bool 1110 static bool
1111 successful(json11::Json j, std::string &err) { 1111 successful(json11::Json j, std::string &err) {
1112 if (!j["success"].is_bool()) { 1112 const bool hasResult = j["result"].is_object();
1113 err = "bool expected for success"; 1113 const bool hasError = j["error"].is_object();
1114 if (hasResult && hasError) {
1115 err = "valid response may contain only one of result and error objects";
1114 return false; 1116 return false;
1115 } 1117 } else if (hasError) {
1116 return j["success"].bool_value(); 1118 return false;
1119 } else if (!hasResult) {
1120 err = "either a result or an error object is required for a valid response";
1121 return false;
1122 } else {
1123 return true;
1124 }
1117 } 1125 }
1118 1126
1119 static void 1127 static void
1120 markRPC(json11::Json::object &jo) { 1128 markRPC(json11::Json::object &jo) {
1121 jo["jsonrpc"] = "2.0"; 1129 jo["jsonrpc"] = "2.0";
1296 } 1304 }
1297 1305
1298 static json11::Json 1306 static json11::Json
1299 fromError(std::string errorText, 1307 fromError(std::string errorText,
1300 RRType responseType, 1308 RRType responseType,
1301 const json11::Json &id) { 1309 const json11::Json &id,
1310 bool writeVerbatimError = false) {
1302 1311
1303 json11::Json::object jo; 1312 json11::Json::object jo;
1304 markRPC(jo); 1313 markRPC(jo);
1305 1314
1306 std::string type; 1315 std::string type;
1313 else type = "invalid"; 1322 else type = "invalid";
1314 1323
1315 json11::Json::object eo; 1324 json11::Json::object eo;
1316 eo["code"] = 0; 1325 eo["code"] = 0;
1317 1326
1318 if (responseType == RRType::NotValid) { 1327 if (responseType == RRType::NotValid || writeVerbatimError) {
1319 eo["message"] = errorText; 1328 eo["message"] = errorText;
1320 } else { 1329 } else {
1321 eo["message"] = 1330 eo["message"] =
1322 std::string("error in ") + type + " request: " + errorText; 1331 std::string("error in ") + type + " request: " + errorText;
1323 } 1332 }