comparison vamp-json/VampJson.h @ 133:74a7c2a8d6b6

Merge from branch listargs
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 04 Nov 2016 10:43:49 +0000
parents 615fc5a47509
children 1c58149b64a2
comparison
equal deleted inserted replaced
126:2004ec2b653e 133:74a7c2a8d6b6
800 800
801 return Vamp::HostExt::PluginLoader::AdapterFlags(flags); 801 return Vamp::HostExt::PluginLoader::AdapterFlags(flags);
802 } 802 }
803 803
804 static json11::Json 804 static json11::Json
805 fromListRequest(const ListRequest &req) {
806 json11::Json::object jo;
807 json11::Json::array arr;
808 for (const auto &f: req.from) {
809 arr.push_back(f);
810 }
811 jo["from"] = arr;
812 return json11::Json(jo);
813 }
814
815 static ListRequest
816 toListRequest(json11::Json j, std::string &err) {
817
818 ListRequest req;
819 if (!j["from"].is_null() &&
820 !j["from"].is_array()) {
821 err = "array expected for from field";
822 return {};
823 }
824 for (const auto &a: j["from"].array_items()) {
825 if (!a.is_string()) {
826 err = "string expected for element in from array";
827 return {};
828 }
829 req.from.push_back(a.string_value());
830 }
831 return req;
832 }
833
834 static json11::Json
835 fromListResponse(const ListResponse &resp) {
836
837 json11::Json::array arr;
838 for (const auto &a: resp.available) {
839 arr.push_back(fromPluginStaticData(a));
840 }
841 json11::Json::object jo;
842 jo["available"] = arr;
843
844 return json11::Json(jo);
845 }
846
847 static ListResponse
848 toListResponse(json11::Json j, std::string &err) {
849
850 ListResponse resp;
851 for (const auto &a: j["result"]["available"].array_items()) {
852 resp.available.push_back(toPluginStaticData(a, err));
853 if (failed(err)) return {};
854 }
855 return resp;
856 }
857
858 static json11::Json
805 fromLoadRequest(const LoadRequest &req) { 859 fromLoadRequest(const LoadRequest &req) {
806 860
807 json11::Json::object jo; 861 json11::Json::object jo;
808 jo["key"] = req.pluginKey; 862 jo["key"] = req.pluginKey;
809 jo["inputSampleRate"] = req.inputSampleRate; 863 jo["inputSampleRate"] = req.inputSampleRate;
1014 } 1068 }
1015 1069
1016 private: // go private briefly for a couple of helper functions 1070 private: // go private briefly for a couple of helper functions
1017 1071
1018 static void 1072 static void
1019 checkTypeField(json11::Json j, std::string expected, std::string &err) { 1073 checkRpcRequestType(json11::Json j, std::string expected, std::string &err) {
1020 if (!j["method"].is_string()) { 1074 if (!j["method"].is_string()) {
1021 err = "string expected for method"; 1075 err = "string expected for method";
1022 return; 1076 return;
1023 } 1077 }
1024 if (j["method"].string_value() != expected) { 1078 if (j["method"].string_value() != expected) {
1025 err = "expected value \"" + expected + "\" for type"; 1079 err = "expected value \"" + expected + "\" for type";
1026 return; 1080 return;
1081 }
1082 if (!j["params"].is_null() &&
1083 !j["params"].is_object()) {
1084 err = "object expected for params";
1085 return;
1086 }
1087 if (!j["id"].is_null() &&
1088 !j["id"].is_number() &&
1089 !j["id"].is_string()) {
1090 err = "number or string expected for id";
1091 return;
1092 }
1093 if (!j["jsonrpc"].is_null() &&
1094 !j["jsonrpc"].is_string()) {
1095 err = "string expected for jsonrpc";
1096 return;
1097 }
1098 for (const auto &kv: j.object_items()) {
1099 if (kv.first != "method" &&
1100 kv.first != "params" &&
1101 kv.first != "id" &&
1102 kv.first != "jsonrpc") {
1103 err = "unexpected field \"" + kv.first + "\" in rpc request object";
1104 return;
1105 }
1027 } 1106 }
1028 } 1107 }
1029 1108
1030 static bool 1109 static bool
1031 successful(json11::Json j, std::string &err) { 1110 successful(json11::Json j, std::string &err) {
1049 } 1128 }
1050 1129
1051 public: 1130 public:
1052 1131
1053 static json11::Json 1132 static json11::Json
1054 fromRpcRequest_List(const json11::Json &id) { 1133 fromRpcRequest_List(const ListRequest &req,
1134 const json11::Json &id) {
1055 1135
1056 json11::Json::object jo; 1136 json11::Json::object jo;
1057 markRPC(jo); 1137 markRPC(jo);
1058 1138
1059 jo["method"] = "list"; 1139 jo["method"] = "list";
1140 jo["params"] = fromListRequest(req);
1060 addId(jo, id); 1141 addId(jo, id);
1061 return json11::Json(jo); 1142 return json11::Json(jo);
1062 } 1143 }
1063 1144
1064 static json11::Json 1145 static json11::Json
1066 const json11::Json &id) { 1147 const json11::Json &id) {
1067 1148
1068 json11::Json::object jo; 1149 json11::Json::object jo;
1069 markRPC(jo); 1150 markRPC(jo);
1070 1151
1071 json11::Json::array arr;
1072 for (const auto &a: resp.available) {
1073 arr.push_back(fromPluginStaticData(a));
1074 }
1075 json11::Json::object po;
1076 po["available"] = arr;
1077
1078 jo["method"] = "list"; 1152 jo["method"] = "list";
1079 jo["result"] = po; 1153 jo["result"] = fromListResponse(resp);
1080 addId(jo, id); 1154 addId(jo, id);
1081 return json11::Json(jo); 1155 return json11::Json(jo);
1082 } 1156 }
1083 1157
1084 static json11::Json 1158 static json11::Json
1268 err = "unknown or unexpected request/response type \"" + type + "\""; 1342 err = "unknown or unexpected request/response type \"" + type + "\"";
1269 return RRType::NotValid; 1343 return RRType::NotValid;
1270 } 1344 }
1271 } 1345 }
1272 1346
1273 static void 1347 static ListRequest
1274 toRpcRequest_List(json11::Json j, std::string &err) { 1348 toRpcRequest_List(json11::Json j, std::string &err) {
1275 checkTypeField(j, "list", err); 1349
1350 checkRpcRequestType(j, "list", err);
1351 if (failed(err)) return {};
1352 return toListRequest(j["params"], err);
1276 } 1353 }
1277 1354
1278 static ListResponse 1355 static ListResponse
1279 toRpcResponse_List(json11::Json j, std::string &err) { 1356 toRpcResponse_List(json11::Json j, std::string &err) {
1280 1357
1281 ListResponse resp; 1358 ListResponse resp;
1282 if (successful(j, err) && !failed(err)) { 1359 if (successful(j, err) && !failed(err)) {
1283 for (const auto &a: j["result"]["available"].array_items()) { 1360 resp = toListResponse(j["result"], err);
1284 resp.available.push_back(toPluginStaticData(a, err)); 1361 }
1285 if (failed(err)) return {};
1286 }
1287 }
1288
1289 return resp; 1362 return resp;
1290 } 1363 }
1291 1364
1292 static LoadRequest 1365 static LoadRequest
1293 toRpcRequest_Load(json11::Json j, std::string &err) { 1366 toRpcRequest_Load(json11::Json j, std::string &err) {
1294 1367
1295 checkTypeField(j, "load", err); 1368 checkRpcRequestType(j, "load", err);
1296 if (failed(err)) return {}; 1369 if (failed(err)) return {};
1297 return toLoadRequest(j["params"], err); 1370 return toLoadRequest(j["params"], err);
1298 } 1371 }
1299 1372
1300 static LoadResponse 1373 static LoadResponse
1312 static ConfigurationRequest 1385 static ConfigurationRequest
1313 toRpcRequest_Configure(json11::Json j, 1386 toRpcRequest_Configure(json11::Json j,
1314 const PluginHandleMapper &pmapper, 1387 const PluginHandleMapper &pmapper,
1315 std::string &err) { 1388 std::string &err) {
1316 1389
1317 checkTypeField(j, "configure", err); 1390 checkRpcRequestType(j, "configure", err);
1318 if (failed(err)) return {}; 1391 if (failed(err)) return {};
1319 return toConfigurationRequest(j["params"], pmapper, err); 1392 return toConfigurationRequest(j["params"], pmapper, err);
1320 } 1393 }
1321 1394
1322 static ConfigurationResponse 1395 static ConfigurationResponse
1333 1406
1334 static ProcessRequest 1407 static ProcessRequest
1335 toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, 1408 toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper,
1336 BufferSerialisation &serialisation, std::string &err) { 1409 BufferSerialisation &serialisation, std::string &err) {
1337 1410
1338 checkTypeField(j, "process", err); 1411 checkRpcRequestType(j, "process", err);
1339 if (failed(err)) return {}; 1412 if (failed(err)) return {};
1340 return toProcessRequest(j["params"], pmapper, serialisation, err); 1413 return toProcessRequest(j["params"], pmapper, serialisation, err);
1341 } 1414 }
1342 1415
1343 static ProcessResponse 1416 static ProcessResponse
1359 1432
1360 static FinishRequest 1433 static FinishRequest
1361 toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper, 1434 toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper,
1362 std::string &err) { 1435 std::string &err) {
1363 1436
1364 checkTypeField(j, "finish", err); 1437 checkRpcRequestType(j, "finish", err);
1365 if (failed(err)) return {}; 1438 if (failed(err)) return {};
1366 FinishRequest req; 1439 FinishRequest req;
1367 req.plugin = pmapper.handleToPlugin 1440 req.plugin = pmapper.handleToPlugin
1368 (j["params"]["handle"].int_value()); 1441 (j["params"]["handle"].int_value());
1369 return req; 1442 return req;