Mercurial > hg > piper-cpp
comparison json/VampJson.h @ 67:db17657ac875
Validation fixes
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 05 Oct 2016 13:53:08 +0100 |
parents | 6f160dee1192 |
children | a5ba837bca28 |
comparison
equal
deleted
inserted
replaced
66:6f160dee1192 | 67:db17657ac875 |
---|---|
1007 } | 1007 } |
1008 } | 1008 } |
1009 | 1009 |
1010 return r; | 1010 return r; |
1011 } | 1011 } |
1012 | |
1013 private: // go private briefly for a couple of helper functions | |
1014 | |
1015 static void | |
1016 checkTypeField(json11::Json j, std::string expected, std::string &err) { | |
1017 if (!j["method"].is_string()) { | |
1018 err = "string expected for method"; | |
1019 return; | |
1020 } | |
1021 if (j["method"].string_value() != expected) { | |
1022 err = "expected value \"" + expected + "\" for type"; | |
1023 return; | |
1024 } | |
1025 } | |
1026 | |
1027 static bool | |
1028 successful(json11::Json j, std::string &err) { | |
1029 if (!j["success"].is_bool()) { | |
1030 err = "bool expected for success"; | |
1031 return false; | |
1032 } | |
1033 return j["success"].bool_value(); | |
1034 } | |
1035 | |
1036 static void | |
1037 markRPC(json11::Json::object &jo) { | |
1038 jo["jsonrpc"] = "2.0"; | |
1039 } | |
1040 | |
1041 public: | |
1012 | 1042 |
1013 static json11::Json | 1043 static json11::Json |
1014 fromVampRequest_List() { | 1044 fromVampRequest_List() { |
1015 | 1045 |
1016 json11::Json::object jo; | 1046 json11::Json::object jo; |
1017 jo["type"] = "list"; | 1047 markRPC(jo); |
1048 | |
1049 jo["method"] = "list"; | |
1018 return json11::Json(jo); | 1050 return json11::Json(jo); |
1019 } | 1051 } |
1020 | 1052 |
1021 static json11::Json | 1053 static json11::Json |
1022 fromVampResponse_List(const Vamp::HostExt::ListResponse &resp) { | 1054 fromVampResponse_List(const Vamp::HostExt::ListResponse &resp) { |
1023 | 1055 |
1024 json11::Json::object jo; | 1056 json11::Json::object jo; |
1025 jo["type"] = "list"; | 1057 markRPC(jo); |
1026 jo["success"] = true; | |
1027 | 1058 |
1028 json11::Json::array arr; | 1059 json11::Json::array arr; |
1029 for (const auto &a: resp.plugins) { | 1060 for (const auto &a: resp.plugins) { |
1030 arr.push_back(fromPluginStaticData(a)); | 1061 arr.push_back(fromPluginStaticData(a)); |
1031 } | 1062 } |
1032 json11::Json::object po; | 1063 json11::Json::object po; |
1033 po["plugins"] = arr; | 1064 po["plugins"] = arr; |
1034 | 1065 |
1035 jo["content"] = po; | 1066 jo["method"] = "list"; |
1067 jo["result"] = po; | |
1036 return json11::Json(jo); | 1068 return json11::Json(jo); |
1037 } | 1069 } |
1038 | 1070 |
1039 static json11::Json | 1071 static json11::Json |
1040 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { | 1072 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { |
1041 | 1073 |
1042 json11::Json::object jo; | 1074 json11::Json::object jo; |
1043 jo["type"] = "load"; | 1075 markRPC(jo); |
1044 jo["content"] = fromLoadRequest(req); | 1076 |
1077 jo["method"] = "load"; | |
1078 jo["params"] = fromLoadRequest(req); | |
1045 return json11::Json(jo); | 1079 return json11::Json(jo); |
1046 } | 1080 } |
1047 | 1081 |
1048 static json11::Json | 1082 static json11::Json |
1049 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, | 1083 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, |
1050 const PluginHandleMapper &pmapper) { | 1084 const PluginHandleMapper &pmapper) { |
1051 | 1085 |
1052 json11::Json::object jo; | 1086 if (resp.plugin) { |
1053 jo["type"] = "load"; | 1087 |
1054 jo["success"] = (resp.plugin != 0); | 1088 json11::Json::object jo; |
1055 jo["errorText"] = ""; | 1089 markRPC(jo); |
1056 jo["content"] = fromLoadResponse(resp, pmapper); | 1090 |
1057 return json11::Json(jo); | 1091 jo["method"] = "load"; |
1092 jo["result"] = fromLoadResponse(resp, pmapper); | |
1093 return json11::Json(jo); | |
1094 | |
1095 } else { | |
1096 return fromError("Failed to load plugin", RRType::Load); | |
1097 } | |
1058 } | 1098 } |
1059 | 1099 |
1060 static json11::Json | 1100 static json11::Json |
1061 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, | 1101 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, |
1062 const PluginHandleMapper &pmapper) { | 1102 const PluginHandleMapper &pmapper) { |
1063 | 1103 |
1064 json11::Json::object jo; | 1104 json11::Json::object jo; |
1065 jo["type"] = "configure"; | 1105 markRPC(jo); |
1066 jo["content"] = fromConfigurationRequest(req, pmapper); | 1106 |
1107 jo["method"] = "configure"; | |
1108 jo["params"] = fromConfigurationRequest(req, pmapper); | |
1067 return json11::Json(jo); | 1109 return json11::Json(jo); |
1068 } | 1110 } |
1069 | 1111 |
1070 static json11::Json | 1112 static json11::Json |
1071 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp, | 1113 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp, |
1072 const PluginHandleMapper &pmapper) { | 1114 const PluginHandleMapper &pmapper) { |
1073 | 1115 |
1074 json11::Json::object jo; | 1116 if (!resp.outputs.empty()) { |
1075 jo["type"] = "configure"; | 1117 |
1076 jo["success"] = (!resp.outputs.empty()); | 1118 json11::Json::object jo; |
1077 jo["errorText"] = ""; | 1119 markRPC(jo); |
1078 jo["content"] = fromConfigurationResponse(resp, pmapper); | 1120 |
1079 return json11::Json(jo); | 1121 jo["method"] = "configure"; |
1122 jo["result"] = fromConfigurationResponse(resp, pmapper); | |
1123 return json11::Json(jo); | |
1124 | |
1125 } else { | |
1126 return fromError("Failed to configure plugin", RRType::Configure); | |
1127 } | |
1080 } | 1128 } |
1081 | 1129 |
1082 static json11::Json | 1130 static json11::Json |
1083 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, | 1131 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, |
1084 const PluginHandleMapper &pmapper, | 1132 const PluginHandleMapper &pmapper, |
1085 BufferSerialisation serialisation) { | 1133 BufferSerialisation serialisation) { |
1086 | 1134 |
1087 json11::Json::object jo; | 1135 json11::Json::object jo; |
1088 jo["type"] = "process"; | 1136 markRPC(jo); |
1089 jo["content"] = fromProcessRequest(req, pmapper, serialisation); | 1137 |
1138 jo["method"] = "process"; | |
1139 jo["params"] = fromProcessRequest(req, pmapper, serialisation); | |
1090 return json11::Json(jo); | 1140 return json11::Json(jo); |
1091 } | 1141 } |
1092 | 1142 |
1093 static json11::Json | 1143 static json11::Json |
1094 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp, | 1144 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp, |
1095 const PluginHandleMapper &pmapper, | 1145 const PluginHandleMapper &pmapper, |
1096 BufferSerialisation serialisation) { | 1146 BufferSerialisation serialisation) { |
1097 | 1147 |
1098 json11::Json::object jo; | 1148 json11::Json::object jo; |
1099 jo["type"] = "process"; | 1149 markRPC(jo); |
1100 jo["success"] = true; | 1150 |
1101 jo["errorText"] = ""; | |
1102 json11::Json::object po; | 1151 json11::Json::object po; |
1103 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); | 1152 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); |
1104 po["features"] = fromFeatureSet(resp.features, | 1153 po["features"] = fromFeatureSet(resp.features, |
1105 *pmapper.pluginToOutputIdMapper(resp.plugin), | 1154 *pmapper.pluginToOutputIdMapper(resp.plugin), |
1106 serialisation); | 1155 serialisation); |
1107 jo["content"] = po; | 1156 jo["method"] = "process"; |
1157 jo["result"] = po; | |
1108 return json11::Json(jo); | 1158 return json11::Json(jo); |
1109 } | 1159 } |
1110 | 1160 |
1111 static json11::Json | 1161 static json11::Json |
1112 fromVampRequest_Finish(const Vamp::HostExt::FinishRequest &req, | 1162 fromVampRequest_Finish(const Vamp::HostExt::FinishRequest &req, |
1113 const PluginHandleMapper &pmapper) { | 1163 const PluginHandleMapper &pmapper) { |
1114 | 1164 |
1115 json11::Json::object jo; | 1165 json11::Json::object jo; |
1116 jo["type"] = "finish"; | 1166 markRPC(jo); |
1167 | |
1117 json11::Json::object fo; | 1168 json11::Json::object fo; |
1118 fo["pluginHandle"] = pmapper.pluginToHandle(req.plugin); | 1169 fo["pluginHandle"] = pmapper.pluginToHandle(req.plugin); |
1119 jo["content"] = fo; | 1170 |
1171 jo["method"] = "finish"; | |
1172 jo["params"] = fo; | |
1120 return json11::Json(jo); | 1173 return json11::Json(jo); |
1121 } | 1174 } |
1122 | 1175 |
1123 static json11::Json | 1176 static json11::Json |
1124 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp, | 1177 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp, |
1125 const PluginHandleMapper &pmapper, | 1178 const PluginHandleMapper &pmapper, |
1126 BufferSerialisation serialisation) { | 1179 BufferSerialisation serialisation) { |
1127 | 1180 |
1128 json11::Json::object jo; | 1181 json11::Json::object jo; |
1129 jo["type"] = "finish"; | 1182 markRPC(jo); |
1130 jo["success"] = true; | 1183 |
1131 jo["errorText"] = ""; | |
1132 json11::Json::object po; | 1184 json11::Json::object po; |
1133 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); | 1185 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); |
1134 po["features"] = fromFeatureSet(resp.features, | 1186 po["features"] = fromFeatureSet(resp.features, |
1135 *pmapper.pluginToOutputIdMapper(resp.plugin), | 1187 *pmapper.pluginToOutputIdMapper(resp.plugin), |
1136 serialisation); | 1188 serialisation); |
1137 jo["content"] = po; | 1189 jo["method"] = "finish"; |
1190 jo["result"] = po; | |
1138 return json11::Json(jo); | 1191 return json11::Json(jo); |
1139 } | 1192 } |
1140 | 1193 |
1141 static json11::Json | 1194 static json11::Json |
1142 fromError(std::string errorText, RRType responseType) { | 1195 fromError(std::string errorText, RRType responseType) { |
1143 | 1196 |
1144 json11::Json::object jo; | 1197 json11::Json::object jo; |
1198 markRPC(jo); | |
1199 | |
1145 std::string type; | 1200 std::string type; |
1146 | 1201 |
1147 if (responseType == RRType::List) type = "list"; | 1202 if (responseType == RRType::List) type = "list"; |
1148 else if (responseType == RRType::Load) type = "load"; | 1203 else if (responseType == RRType::Load) type = "load"; |
1149 else if (responseType == RRType::Configure) type = "configure"; | 1204 else if (responseType == RRType::Configure) type = "configure"; |
1150 else if (responseType == RRType::Process) type = "process"; | 1205 else if (responseType == RRType::Process) type = "process"; |
1151 else if (responseType == RRType::Finish) type = "finish"; | 1206 else if (responseType == RRType::Finish) type = "finish"; |
1152 else type = "invalid"; | 1207 else type = "invalid"; |
1153 | 1208 |
1154 jo["type"] = type; | 1209 json11::Json::object eo; |
1155 jo["success"] = false; | 1210 //!!! + need code |
1156 jo["errorText"] = std::string("error in ") + type + " request: " + errorText; | 1211 eo["message"] = |
1157 return json11::Json(jo); | 1212 std::string("error in ") + type + " request: " + errorText; |
1158 } | 1213 |
1159 | 1214 jo["method"] = type; |
1160 private: // go private briefly for a couple of helper functions | 1215 jo["error"] = eo; |
1161 | 1216 |
1162 static void | 1217 return json11::Json(jo); |
1163 checkTypeField(json11::Json j, std::string expected, std::string &err) { | 1218 } |
1164 if (!j["type"].is_string()) { | 1219 |
1165 err = "string expected for type"; | |
1166 return; | |
1167 } | |
1168 if (j["type"].string_value() != expected) { | |
1169 err = "expected value \"" + expected + "\" for type"; | |
1170 return; | |
1171 } | |
1172 } | |
1173 | |
1174 static bool | |
1175 successful(json11::Json j, std::string &err) { | |
1176 if (!j["success"].is_bool()) { | |
1177 err = "bool expected for success"; | |
1178 return false; | |
1179 } | |
1180 return j["success"].bool_value(); | |
1181 } | |
1182 | |
1183 public: | |
1184 static RRType | 1220 static RRType |
1185 getRequestResponseType(json11::Json j, std::string &err) { | 1221 getRequestResponseType(json11::Json j, std::string &err) { |
1186 | 1222 |
1187 if (!j["type"].is_string()) { | 1223 if (!j["method"].is_string()) { |
1188 err = "string expected for type"; | 1224 err = "string expected for method"; |
1189 return RRType::NotValid; | 1225 return RRType::NotValid; |
1190 } | 1226 } |
1191 | 1227 |
1192 std::string type = j["type"].string_value(); | 1228 std::string type = j["method"].string_value(); |
1193 | 1229 |
1194 if (type == "list") return RRType::List; | 1230 if (type == "list") return RRType::List; |
1195 else if (type == "load") return RRType::Load; | 1231 else if (type == "load") return RRType::Load; |
1196 else if (type == "configure") return RRType::Configure; | 1232 else if (type == "configure") return RRType::Configure; |
1197 else if (type == "process") return RRType::Process; | 1233 else if (type == "process") return RRType::Process; |
1211 static Vamp::HostExt::ListResponse | 1247 static Vamp::HostExt::ListResponse |
1212 toVampResponse_List(json11::Json j, std::string &err) { | 1248 toVampResponse_List(json11::Json j, std::string &err) { |
1213 | 1249 |
1214 Vamp::HostExt::ListResponse resp; | 1250 Vamp::HostExt::ListResponse resp; |
1215 if (successful(j, err) && !failed(err)) { | 1251 if (successful(j, err) && !failed(err)) { |
1216 for (const auto &a: j["content"]["plugins"].array_items()) { | 1252 for (const auto &a: j["result"]["plugins"].array_items()) { |
1217 resp.plugins.push_back(toPluginStaticData(a, err)); | 1253 resp.plugins.push_back(toPluginStaticData(a, err)); |
1218 if (failed(err)) return {}; | 1254 if (failed(err)) return {}; |
1219 } | 1255 } |
1220 } | 1256 } |
1221 | 1257 |
1225 static Vamp::HostExt::LoadRequest | 1261 static Vamp::HostExt::LoadRequest |
1226 toVampRequest_Load(json11::Json j, std::string &err) { | 1262 toVampRequest_Load(json11::Json j, std::string &err) { |
1227 | 1263 |
1228 checkTypeField(j, "load", err); | 1264 checkTypeField(j, "load", err); |
1229 if (failed(err)) return {}; | 1265 if (failed(err)) return {}; |
1230 return toLoadRequest(j["content"], err); | 1266 return toLoadRequest(j["params"], err); |
1231 } | 1267 } |
1232 | 1268 |
1233 static Vamp::HostExt::LoadResponse | 1269 static Vamp::HostExt::LoadResponse |
1234 toVampResponse_Load(json11::Json j, | 1270 toVampResponse_Load(json11::Json j, |
1235 const PluginHandleMapper &pmapper, | 1271 const PluginHandleMapper &pmapper, |
1236 std::string &err) { | 1272 std::string &err) { |
1237 | 1273 |
1238 Vamp::HostExt::LoadResponse resp; | 1274 Vamp::HostExt::LoadResponse resp; |
1239 if (successful(j, err) && !failed(err)) { | 1275 if (successful(j, err) && !failed(err)) { |
1240 resp = toLoadResponse(j["content"], pmapper, err); | 1276 resp = toLoadResponse(j["result"], pmapper, err); |
1241 } | 1277 } |
1242 return resp; | 1278 return resp; |
1243 } | 1279 } |
1244 | 1280 |
1245 static Vamp::HostExt::ConfigurationRequest | 1281 static Vamp::HostExt::ConfigurationRequest |
1247 const PluginHandleMapper &pmapper, | 1283 const PluginHandleMapper &pmapper, |
1248 std::string &err) { | 1284 std::string &err) { |
1249 | 1285 |
1250 checkTypeField(j, "configure", err); | 1286 checkTypeField(j, "configure", err); |
1251 if (failed(err)) return {}; | 1287 if (failed(err)) return {}; |
1252 return toConfigurationRequest(j["content"], pmapper, err); | 1288 return toConfigurationRequest(j["params"], pmapper, err); |
1253 } | 1289 } |
1254 | 1290 |
1255 static Vamp::HostExt::ConfigurationResponse | 1291 static Vamp::HostExt::ConfigurationResponse |
1256 toVampResponse_Configure(json11::Json j, | 1292 toVampResponse_Configure(json11::Json j, |
1257 const PluginHandleMapper &pmapper, | 1293 const PluginHandleMapper &pmapper, |
1258 std::string &err) { | 1294 std::string &err) { |
1259 | 1295 |
1260 Vamp::HostExt::ConfigurationResponse resp; | 1296 Vamp::HostExt::ConfigurationResponse resp; |
1261 if (successful(j, err) && !failed(err)) { | 1297 if (successful(j, err) && !failed(err)) { |
1262 resp = toConfigurationResponse(j["content"], pmapper, err); | 1298 resp = toConfigurationResponse(j["result"], pmapper, err); |
1263 } | 1299 } |
1264 return resp; | 1300 return resp; |
1265 } | 1301 } |
1266 | 1302 |
1267 static Vamp::HostExt::ProcessRequest | 1303 static Vamp::HostExt::ProcessRequest |
1268 toVampRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, | 1304 toVampRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, |
1269 BufferSerialisation &serialisation, std::string &err) { | 1305 BufferSerialisation &serialisation, std::string &err) { |
1270 | 1306 |
1271 checkTypeField(j, "process", err); | 1307 checkTypeField(j, "process", err); |
1272 if (failed(err)) return {}; | 1308 if (failed(err)) return {}; |
1273 return toProcessRequest(j["content"], pmapper, serialisation, err); | 1309 return toProcessRequest(j["params"], pmapper, serialisation, err); |
1274 } | 1310 } |
1275 | 1311 |
1276 static Vamp::HostExt::ProcessResponse | 1312 static Vamp::HostExt::ProcessResponse |
1277 toVampResponse_Process(json11::Json j, | 1313 toVampResponse_Process(json11::Json j, |
1278 const PluginHandleMapper &pmapper, | 1314 const PluginHandleMapper &pmapper, |
1279 BufferSerialisation &serialisation, std::string &err) { | 1315 BufferSerialisation &serialisation, std::string &err) { |
1280 | 1316 |
1281 Vamp::HostExt::ProcessResponse resp; | 1317 Vamp::HostExt::ProcessResponse resp; |
1282 if (successful(j, err) && !failed(err)) { | 1318 if (successful(j, err) && !failed(err)) { |
1283 auto jc = j["content"]; | 1319 auto jc = j["result"]; |
1284 auto h = jc["pluginHandle"].int_value(); | 1320 auto h = jc["pluginHandle"].int_value(); |
1285 resp.plugin = pmapper.handleToPlugin(h); | 1321 resp.plugin = pmapper.handleToPlugin(h); |
1286 resp.features = toFeatureSet(jc["features"], | 1322 resp.features = toFeatureSet(jc["features"], |
1287 *pmapper.handleToOutputIdMapper(h), | 1323 *pmapper.handleToOutputIdMapper(h), |
1288 serialisation, err); | 1324 serialisation, err); |
1296 | 1332 |
1297 checkTypeField(j, "finish", err); | 1333 checkTypeField(j, "finish", err); |
1298 if (failed(err)) return {}; | 1334 if (failed(err)) return {}; |
1299 Vamp::HostExt::FinishRequest req; | 1335 Vamp::HostExt::FinishRequest req; |
1300 req.plugin = pmapper.handleToPlugin | 1336 req.plugin = pmapper.handleToPlugin |
1301 (j["content"]["pluginHandle"].int_value()); | 1337 (j["params"]["pluginHandle"].int_value()); |
1302 return req; | 1338 return req; |
1303 } | 1339 } |
1304 | 1340 |
1305 static Vamp::HostExt::ProcessResponse | 1341 static Vamp::HostExt::ProcessResponse |
1306 toVampResponse_Finish(json11::Json j, | 1342 toVampResponse_Finish(json11::Json j, |
1307 const PluginHandleMapper &pmapper, | 1343 const PluginHandleMapper &pmapper, |
1308 BufferSerialisation &serialisation, std::string &err) { | 1344 BufferSerialisation &serialisation, std::string &err) { |
1309 | 1345 |
1310 Vamp::HostExt::ProcessResponse resp; | 1346 Vamp::HostExt::ProcessResponse resp; |
1311 if (successful(j, err) && !failed(err)) { | 1347 if (successful(j, err) && !failed(err)) { |
1312 auto jc = j["content"]; | 1348 auto jc = j["result"]; |
1313 auto h = jc["pluginHandle"].int_value(); | 1349 auto h = jc["pluginHandle"].int_value(); |
1314 resp.plugin = pmapper.handleToPlugin(h); | 1350 resp.plugin = pmapper.handleToPlugin(h); |
1315 resp.features = toFeatureSet(jc["features"], | 1351 resp.features = toFeatureSet(jc["features"], |
1316 *pmapper.handleToOutputIdMapper(h), | 1352 *pmapper.handleToOutputIdMapper(h), |
1317 serialisation, err); | 1353 serialisation, err); |