1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Piper C++
Centre for Digital Music, Queen Mary, University of London.
Copyright 2015-2017 QMUL.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the Centre for
Digital Music; Queen Mary, University of London; and Chris Cannam
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization.
*/
#ifndef PIPER_VAMP_JSON_H
#define PIPER_VAMP_JSON_H
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <cmath>
#include <json11/json11.hpp>
#include <base-n/include/basen.hpp>
#include <vamp-hostsdk/Plugin.h>
#include <vamp-hostsdk/PluginLoader.h>
#include "vamp-support/StaticOutputDescriptor.h"
#include "vamp-support/PluginStaticData.h"
#include "vamp-support/PluginConfiguration.h"
#include "vamp-support/RequestResponse.h"
#include "vamp-support/PluginHandleMapper.h"
#include "vamp-support/PluginOutputIdMapper.h"
#include "vamp-support/RequestResponseType.h"
namespace piper_vamp {
/**
* Convert the structures laid out in the Vamp SDK classes into JSON
* (and back again) following the schema in the vamp-json-schema
* project repo.
*
* Functions with names starting "from" convert from a Vamp SDK object
* to JSON output. Most of them return a json11::Json object, with a
* few exceptions for low-level utilities that return a string. These
* functions succeed all of the time.
*
* Functions with names starting "to" convert to a Vamp SDK object
* from JSON input. These functions all accept a json11::Json object
* as first argument, with a few exceptions for low-level utilities
* that accept a string. These functions all accept a string reference
* as a final argument and return an error string through it if the
* conversion fails. If conversion fails the return value is
* undefined, and any returned object may be incomplete or
* invalid. Callers should check for an empty error string (indicating
* success) before using the returned value.
*/
class VampJson
{
public:
/** Serialisation format for arrays of floats (process input and
* feature values). Wherever such an array appears, it may
* alternatively be replaced by a single string containing a
* base-64 encoding of the IEEE float buffer. When parsing, if a
* string is found instead of an array in this case, it will be
* interpreted as a base-64 encoded buffer. Only array or base-64
* encoding may be provided, not both.
*/
enum class BufferSerialisation {
/** Default JSON serialisation of values in array form. This
* is relatively slow to parse and serialise, and can take a
* lot of space.
*/
Array,
/** Base64-encoded string of the raw data as packed
* little-endian IEEE 32-bit floats. Faster and more compact
* than the text encoding but more complicated to
* provide. Note that Base64 serialisations produced by this
* library do not including padding characters and so are not
* necessarily multiples of 4 characters long. You will need
* to pad them yourself if concatenating them or supplying to
* a consumer that expects padding.
*/
Base64
};
static bool failed(const std::string &err) {
return err != "";
}
template <typename T>
static json11::Json
fromBasicDescriptor(const T &t) {
return json11::Json::object {
{ "identifier", t.identifier },
{ "name", t.name },
{ "description", t.description }
};
}
template <typename T>
static void
toBasicDescriptor(json11::Json j, T &t, std::string &err) {
if (!j.is_object()) {
err = "object expected for basic descriptor content";
return;
}
if (!j["identifier"].is_string()) {
err = "string expected for identifier";
return;
}
t.identifier = j["identifier"].string_value();
t.name = j["name"].string_value();
t.description = j["description"].string_value();
}
template <typename T>
static json11::Json
fromValueExtents(const T &t) {
return json11::Json::object {
{ "min", t.minValue },
{ "max", t.maxValue }
};
}
template <typename T>
static bool
toValueExtents(json11::Json j, T &t, std::string &err) {
if (j["extents"].is_null()) {
return false;
} else if (j["extents"].is_object()) {
if (j["extents"]["min"].is_number() &&
j["extents"]["max"].is_number()) {
t.minValue = float(j["extents"]["min"].number_value());
t.maxValue = float(j["extents"]["max"].number_value());
return true;
} else {
err = "numbers expected for min and max";
return false;
}
} else {
err = "object expected for extents (if present)";
return false;
}
}
static json11::Json
fromRealTime(const Vamp::RealTime &r) {
return json11::Json::object {
{ "s", r.sec },
{ "n", r.nsec }
};
}
static Vamp::RealTime
toRealTime(json11::Json j, std::string &err) {
json11::Json sec = j["s"];
json11::Json nsec = j["n"];
if (!sec.is_number() || !nsec.is_number()) {
err = "invalid Vamp::RealTime object " + j.dump();
return {};
}
return Vamp::RealTime(sec.int_value(), nsec.int_value());
}
static std::string
fromSampleType(Vamp::Plugin::OutputDescriptor::SampleType type) {
switch (type) {
case Vamp::Plugin::OutputDescriptor::OneSamplePerStep:
return "OneSamplePerStep";
case Vamp::Plugin::OutputDescriptor::FixedSampleRate:
return "FixedSampleRate";
case Vamp::Plugin::OutputDescriptor::VariableSampleRate:
return "VariableSampleRate";
}
return "";
}
static Vamp::Plugin::OutputDescriptor::SampleType
toSampleType(std::string text, std::string &err) {
if (text == "OneSamplePerStep") {
return Vamp::Plugin::OutputDescriptor::OneSamplePerStep;
} else if (text == "FixedSampleRate") {
return Vamp::Plugin::OutputDescriptor::FixedSampleRate;
} else if (text == "VariableSampleRate") {
return Vamp::Plugin::OutputDescriptor::VariableSampleRate;
} else {
err = "invalid sample type string: " + text;
return Vamp::Plugin::OutputDescriptor::OneSamplePerStep;
}
}
static json11::Json
fromStaticOutputDescriptor(const StaticOutputDescriptor &sd) {
json11::Json::object jo;
if (sd.typeURI != "") {
jo["typeURI"] = sd.typeURI;
}
return json11::Json(jo);
}
static json11::Json
fromConfiguredOutputDescriptor(const Vamp::Plugin::OutputDescriptor &desc) {
json11::Json::object jo {
{ "unit", desc.unit },
{ "sampleType", fromSampleType(desc.sampleType) },
{ "sampleRate", desc.sampleRate },
{ "hasDuration", desc.hasDuration }
};
if (desc.hasFixedBinCount) {
jo["binCount"] = int(desc.binCount);
jo["binNames"] = json11::Json::array
(desc.binNames.begin(), desc.binNames.end());
}
if (desc.hasKnownExtents) {
jo["extents"] = fromValueExtents(desc);
}
if (desc.isQuantized) {
jo["quantizeStep"] = desc.quantizeStep;
}
return json11::Json(jo);
}
static json11::Json
fromOutputDescriptor(const Vamp::Plugin::OutputDescriptor &desc,
const StaticOutputDescriptor &sd) {
json11::Json::object jo {
{ "basic", fromBasicDescriptor(desc) },
{ "static", fromStaticOutputDescriptor(sd) },
{ "configured", fromConfiguredOutputDescriptor(desc) }
};
return json11::Json(jo);
}
static StaticOutputDescriptor
toStaticOutputDescriptor(json11::Json j, std::string &err) {
StaticOutputDescriptor sd;
if (!j.is_object()) {
err = "object expected for static output descriptor";
return sd;
}
sd.typeURI = j["typeURI"].string_value();
return sd;
}
static Vamp::Plugin::OutputDescriptor
toConfiguredOutputDescriptor(json11::Json j, std::string &err) {
Vamp::Plugin::OutputDescriptor od;
if (!j.is_object()) {
err = "object expected for output descriptor";
return {};
}
od.unit = j["unit"].string_value();
od.sampleType = toSampleType(j["sampleType"].string_value(), err);
if (failed(err)) return {};
if (!j["sampleRate"].is_number()) {
err = "number expected for sample rate";
return {};
}
od.sampleRate = float(j["sampleRate"].number_value());
od.hasDuration = j["hasDuration"].bool_value();
if (j["binCount"].is_number() && j["binCount"].int_value() > 0) {
od.hasFixedBinCount = true;
od.binCount = j["binCount"].int_value();
for (auto &n: j["binNames"].array_items()) {
if (!n.is_string()) {
err = "string expected for bin name";
return {};
}
od.binNames.push_back(n.string_value());
}
} else {
od.hasFixedBinCount = false;
}
bool extentsPresent = toValueExtents(j, od, err);
if (failed(err)) return {};
od.hasKnownExtents = extentsPresent;
if (j["quantizeStep"].is_number()) {
od.isQuantized = true;
od.quantizeStep = float(j["quantizeStep"].number_value());
} else {
od.isQuantized = false;
}
return od;
}
static std::pair<Vamp::Plugin::OutputDescriptor, StaticOutputDescriptor>
toOutputDescriptor(json11::Json j, std::string &err) {
Vamp::Plugin::OutputDescriptor od;
if (!j.is_object()) {
err = "object expected for output descriptor";
return {};
}
od = toConfiguredOutputDescriptor(j["configured"], err);
if (failed(err)) return {};
toBasicDescriptor(j["basic"], od, err);
if (failed(err)) return {};
StaticOutputDescriptor sd;
if (j["static"] != json11::Json()) {
sd = toStaticOutputDescriptor(j["static"], err);
if (failed(err)) return {};
}
return { od, sd };
}
static json11::Json
fromParameterDescriptor(const Vamp::PluginBase::ParameterDescriptor &desc) {
json11::Json::object jo {
{ "basic", fromBasicDescriptor(desc) },
{ "unit", desc.unit },
{ "extents", fromValueExtents(desc) },
{ "defaultValue", desc.defaultValue },
{ "valueNames", json11::Json::array
(desc.valueNames.begin(), desc.valueNames.end()) }
};
if (desc.isQuantized) {
jo["quantizeStep"] = desc.quantizeStep;
}
return json11::Json(jo);
}
static Vamp::PluginBase::ParameterDescriptor
toParameterDescriptor(json11::Json j, std::string &err) {
Vamp::PluginBase::ParameterDescriptor pd;
if (!j.is_object()) {
err = "object expected for parameter descriptor";
return {};
}
toBasicDescriptor(j["basic"], pd, err);
if (failed(err)) return {};
pd.unit = j["unit"].string_value();
bool extentsPresent = toValueExtents(j, pd, err);
if (failed(err)) return {};
if (!extentsPresent) {
err = "extents must be present in parameter descriptor";
return {};
}
if (!j["defaultValue"].is_number()) {
err = "number expected for default value";
return {};
}
pd.defaultValue = float(j["defaultValue"].number_value());
pd.valueNames.clear();
for (auto &n: j["valueNames"].array_items()) {
if (!n.is_string()) {
err = "string expected for value name";
return {};
}
pd.valueNames.push_back(n.string_value());
}
if (j["quantizeStep"].is_number()) {
pd.isQuantized = true;
pd.quantizeStep = float(j["quantizeStep"].number_value());
} else {
pd.isQuantized = false;
}
return pd;
}
static std::string
fromFloatBuffer(const float *buffer, size_t nfloats) {
// must use char pointers, otherwise the converter will only
// encode every 4th byte (as it will count up in float* steps)
const char *start = reinterpret_cast<const char *>(buffer);
const char *end = reinterpret_cast<const char *>(buffer + nfloats);
std::string encoded;
bn::encode_b64(start, end, back_inserter(encoded));
return encoded;
}
static std::vector<float>
toFloatBuffer(std::string encoded, std::string & /* err */) {
std::string decoded;
bn::decode_b64(encoded.begin(), encoded.end(), back_inserter(decoded));
const float *buffer = reinterpret_cast<const float *>(decoded.c_str());
size_t n = decoded.size() / sizeof(float);
return std::vector<float>(buffer, buffer + n);
}
static json11::Json
fromFeature(const Vamp::Plugin::Feature &f,
BufferSerialisation serialisation) {
json11::Json::object jo;
if (f.values.size() > 0) {
if (serialisation == BufferSerialisation::Array) {
jo["featureValues"] = json11::Json::array(f.values.begin(),
f.values.end());
} else {
jo["featureValues"] = fromFloatBuffer(f.values.data(),
f.values.size());
}
}
if (f.label != "") {
jo["label"] = f.label;
}
if (f.hasTimestamp) {
jo["timestamp"] = fromRealTime(f.timestamp);
}
if (f.hasDuration) {
jo["duration"] = fromRealTime(f.duration);
}
return json11::Json(jo);
}
static Vamp::Plugin::Feature
toFeature(json11::Json j, BufferSerialisation &serialisation, std::string &err) {
Vamp::Plugin::Feature f;
if (!j.is_object()) {
err = "object expected for feature";
return {};
}
if (j["timestamp"].is_object()) {
f.timestamp = toRealTime(j["timestamp"], err);
if (failed(err)) return {};
f.hasTimestamp = true;
}
if (j["duration"].is_object()) {
f.duration = toRealTime(j["duration"], err);
if (failed(err)) return {};
f.hasDuration = true;
}
if (j["featureValues"].is_string()) {
f.values = toFloatBuffer(j["featureValues"].string_value(), err);
if (failed(err)) return {};
serialisation = BufferSerialisation::Base64;
} else if (j["featureValues"].is_array()) {
for (auto v : j["featureValues"].array_items()) {
f.values.push_back(float(v.number_value()));
}
serialisation = BufferSerialisation::Array;
}
f.label = j["label"].string_value();
return f;
}
static json11::Json
fromFeatureSet(const Vamp::Plugin::FeatureSet &fs,
const PluginOutputIdMapper &omapper,
BufferSerialisation serialisation) {
json11::Json::object jo;
for (const auto &fsi : fs) {
std::vector<json11::Json> fj;
for (const Vamp::Plugin::Feature &f: fsi.second) {
fj.push_back(fromFeature(f, serialisation));
}
jo[omapper.indexToId(fsi.first)] = fj;
}
return json11::Json(jo);
}
static Vamp::Plugin::FeatureList
toFeatureList(json11::Json j,
BufferSerialisation &serialisation, std::string &err) {
Vamp::Plugin::FeatureList fl;
if (!j.is_array()) {
err = "array expected for feature list";
return fl;
}
for (const json11::Json &fj : j.array_items()) {
fl.push_back(toFeature(fj, serialisation, err));
if (failed(err)) return fl;
}
return fl;
}
static Vamp::Plugin::FeatureSet
toFeatureSet(json11::Json j,
const PluginOutputIdMapper &omapper,
BufferSerialisation &serialisation,
std::string &err) {
Vamp::Plugin::FeatureSet fs;
if (!j.is_object()) {
err = "object expected for feature set";
return fs;
}
for (auto &entry : j.object_items()) {
int n = omapper.idToIndex(entry.first);
if (fs.find(n) != fs.end()) {
err = "duplicate numerical index for output";
return fs;
}
fs[n] = toFeatureList(entry.second, serialisation, err);
if (failed(err)) return fs;
}
return fs;
}
static std::string
fromInputDomain(Vamp::Plugin::InputDomain domain) {
switch (domain) {
case Vamp::Plugin::TimeDomain:
return "TimeDomain";
case Vamp::Plugin::FrequencyDomain:
return "FrequencyDomain";
}
return "";
}
static Vamp::Plugin::InputDomain
toInputDomain(std::string text, std::string &err) {
if (text == "TimeDomain") {
return Vamp::Plugin::TimeDomain;
} else if (text == "FrequencyDomain") {
return Vamp::Plugin::FrequencyDomain;
} else {
err = "invalid input domain string: " + text;
return {};
}
}
static json11::Json
fromPluginStaticData(const PluginStaticData &d) {
json11::Json::object jo;
jo["key"] = d.pluginKey;
jo["basic"] = fromBasicDescriptor(d.basic);
jo["maker"] = d.maker;
jo["rights"] = d.copyright;
jo["version"] = d.pluginVersion;
json11::Json::array cat;
for (const std::string &c: d.category) cat.push_back(c);
jo["category"] = cat;
jo["minChannelCount"] = int(d.minChannelCount);
jo["maxChannelCount"] = int(d.maxChannelCount);
json11::Json::array params;
Vamp::PluginBase::ParameterList vparams = d.parameters;
for (auto &p: vparams) params.push_back(fromParameterDescriptor(p));
jo["parameters"] = params;
json11::Json::array progs;
Vamp::PluginBase::ProgramList vprogs = d.programs;
for (auto &p: vprogs) progs.push_back(p);
jo["programs"] = progs;
jo["inputDomain"] = fromInputDomain(d.inputDomain);
json11::Json::array outinfo;
auto vouts = d.basicOutputInfo;
for (auto &o: vouts) outinfo.push_back(fromBasicDescriptor(o));
jo["basicOutputInfo"] = outinfo;
json11::Json::object statinfo;
auto souts = d.staticOutputInfo;
for (auto &s: souts) {
statinfo[s.first] = fromStaticOutputDescriptor(s.second);
}
jo["staticOutputInfo"] = statinfo;
return json11::Json(jo);
}
static StaticOutputInfo
toStaticOutputInfo(json11::Json j, std::string &err) {
if (j == json11::Json()) return {};
if (!j.is_object()) {
err = "object expected for static output info";
return {};
}
StaticOutputInfo sinfo;
auto items = j.object_items();
for (auto i: items) {
sinfo[i.first] = toStaticOutputDescriptor(i.second, err);
if (failed(err)) return {};
}
return sinfo;
}
static PluginStaticData
toPluginStaticData(json11::Json j, std::string &err) {
if (!j.has_shape({
{ "key", json11::Json::STRING },
{ "version", json11::Json::NUMBER },
{ "minChannelCount", json11::Json::NUMBER },
{ "maxChannelCount", json11::Json::NUMBER },
{ "inputDomain", json11::Json::STRING }}, err)) {
err = "malformed plugin static data: " + err;
} else if (!j["basicOutputInfo"].is_array()) {
err = "array expected for basic output info";
} else if (!j["maker"].is_null() &&
!j["maker"].is_string()) {
err = "string expected for maker";
} else if (!j["rights"].is_null() &&
!j["rights"].is_string()) {
err = "string expected for rights";
} else if (!j["category"].is_null() &&
!j["category"].is_array()) {
err = "array expected for category";
} else if (!j["parameters"].is_null() &&
!j["parameters"].is_array()) {
err = "array expected for parameters";
} else if (!j["programs"].is_null() &&
!j["programs"].is_array()) {
err = "array expected for programs";
} else if (!j["inputDomain"].is_null() &&
!j["inputDomain"].is_string()) {
err = "string expected for inputDomain";
} else if (!j["staticOutputInfo"].is_null() &&
!j["staticOutputInfo"].is_object()) {
err = "object expected for staticOutputInfo";
} else {
PluginStaticData psd;
psd.pluginKey = j["key"].string_value();
toBasicDescriptor(j["basic"], psd.basic, err);
if (failed(err)) return {};
psd.maker = j["maker"].string_value();
psd.copyright = j["rights"].string_value();
psd.pluginVersion = j["version"].int_value();
for (const auto &c : j["category"].array_items()) {
if (!c.is_string()) {
err = "strings expected in category array";
return {};
}
psd.category.push_back(c.string_value());
}
psd.minChannelCount = j["minChannelCount"].int_value();
psd.maxChannelCount = j["maxChannelCount"].int_value();
for (const auto &p : j["parameters"].array_items()) {
auto pd = toParameterDescriptor(p, err);
if (failed(err)) return {};
psd.parameters.push_back(pd);
}
for (const auto &p : j["programs"].array_items()) {
if (!p.is_string()) {
err = "strings expected in programs array";
return {};
}
psd.programs.push_back(p.string_value());
}
psd.inputDomain = toInputDomain(j["inputDomain"].string_value(), err);
if (failed(err)) return {};
for (const auto &bo : j["basicOutputInfo"].array_items()) {
PluginStaticData::Basic b;
toBasicDescriptor(bo, b, err);
if (failed(err)) return {};
psd.basicOutputInfo.push_back(b);
}
StaticOutputInfo sinfo =
toStaticOutputInfo(j["staticOutputInfo"], err);
if (failed(err)) return {};
psd.staticOutputInfo = sinfo;
return psd;
}
// fallthrough error case
return {};
}
static json11::Json
fromPluginConfiguration(const PluginConfiguration &c) {
json11::Json::object jo;
json11::Json::object paramValues;
for (auto &vp: c.parameterValues) {
paramValues[vp.first] = vp.second;
}
jo["parameterValues"] = paramValues;
if (c.currentProgram != "") {
jo["currentProgram"] = c.currentProgram;
}
jo["channelCount"] = c.channelCount;
json11::Json::object framing;
framing["stepSize"] = c.framing.stepSize;
framing["blockSize"] = c.framing.blockSize;
jo["framing"] = framing;
return json11::Json(jo);
}
static PluginConfiguration
toPluginConfiguration(json11::Json j, std::string &err) {
if (!j.has_shape({
{ "channelCount", json11::Json::NUMBER } }, err)) {
err = "malformed plugin configuration: " + err;
return {};
}
if (!j["framing"].has_shape({
{ "stepSize", json11::Json::NUMBER },
{ "blockSize", json11::Json::NUMBER } }, err)) {
err = "malformed framing: " + err;
return {};
}
if (!j["parameterValues"].is_null() &&
!j["parameterValues"].is_object()) {
err = "object expected for parameter values";
return {};
}
for (auto &pv : j["parameterValues"].object_items()) {
if (!pv.second.is_number()) {
err = "number expected for parameter value";
return {};
}
}
if (!j["currentProgram"].is_null() &&
!j["currentProgram"].is_string()) {
err = "string expected for program name";
return {};
}
PluginConfiguration config;
config.channelCount = int(round(j["channelCount"].number_value()));
config.framing.stepSize = int(round(j["framing"]["stepSize"].number_value()));
config.framing.blockSize = int(round(j["framing"]["blockSize"].number_value()));
for (auto &pv : j["parameterValues"].object_items()) {
config.parameterValues[pv.first] = float(pv.second.number_value());
}
if (j["currentProgram"].is_string()) {
config.currentProgram = j["currentProgram"].string_value();
}
return config;
}
static json11::Json
fromAdapterFlags(int flags) {
json11::Json::array arr;
if (flags & Vamp::HostExt::PluginLoader::ADAPT_INPUT_DOMAIN) {
arr.push_back("AdaptInputDomain");
}
if (flags & Vamp::HostExt::PluginLoader::ADAPT_CHANNEL_COUNT) {
arr.push_back("AdaptChannelCount");
}
if (flags & Vamp::HostExt::PluginLoader::ADAPT_BUFFER_SIZE) {
arr.push_back("AdaptBufferSize");
}
return json11::Json(arr);
}
static Vamp::HostExt::PluginLoader::AdapterFlags
toAdapterFlags(json11::Json j, std::string &err) {
int flags = 0x0;
if (!j.is_array()) {
err = "array expected for adapter flags";
} else {
for (auto &jj: j.array_items()) {
if (!jj.is_string()) {
err = "string expected for adapter flag";
break;
}
std::string text = jj.string_value();
if (text == "AdaptInputDomain") {
flags |= Vamp::HostExt::PluginLoader::ADAPT_INPUT_DOMAIN;
} else if (text == "AdaptChannelCount") {
flags |= Vamp::HostExt::PluginLoader::ADAPT_CHANNEL_COUNT;
} else if (text == "AdaptBufferSize") {
flags |= Vamp::HostExt::PluginLoader::ADAPT_BUFFER_SIZE;
} else if (text == "AdaptAllSafe") {
flags |= Vamp::HostExt::PluginLoader::ADAPT_ALL_SAFE;
} else if (text == "AdaptAll") {
flags |= Vamp::HostExt::PluginLoader::ADAPT_ALL;
} else {
err = "invalid adapter flag string: " + text;
break;
}
}
}
return Vamp::HostExt::PluginLoader::AdapterFlags(flags);
}
static json11::Json
fromProgramParameters(const PluginProgramParameters &programParameters) {
json11::Json::object jo;
for (const auto &pp: programParameters.programParameters) {
auto program = pp.first;
json11::Json::object po;
for (const auto &pv: pp.second) {
po[pv.first] = pv.second;
}
jo[program] = po;
}
return json11::Json(jo);
}
static PluginProgramParameters
toProgramParameters(json11::Json j, std::string &err) {
if (!j.is_object()) {
err = "object expected for program parameters";
return {};
}
PluginProgramParameters params;
auto pp = j.object_items();
for (auto program: pp) {
std::string name = program.first;
if (!program.second.is_object()) {
err = std::string("object expected for program parameter map ") +
"(in program \"" + name + "\")";
return {};
}
for (auto p: program.second.object_items()) {
if (!p.second.is_number()) {
err = std::string("number expected for program parameter value ") +
"(in program \"" + name + "\")";
return {};
}
params.programParameters[name][p.first] = p.second.number_value();
}
}
return params;
}
static json11::Json
fromListRequest(const ListRequest &req) {
json11::Json::object jo;
json11::Json::array arr;
for (const auto &f: req.from) {
arr.push_back(f);
}
jo["from"] = arr;
return json11::Json(jo);
}
static ListRequest
toListRequest(json11::Json j, std::string &err) {
ListRequest req;
if (!j["from"].is_null() &&
!j["from"].is_array()) {
err = "array expected for from field";
return {};
}
for (const auto &a: j["from"].array_items()) {
if (!a.is_string()) {
err = "string expected for element in from array";
return {};
}
req.from.push_back(a.string_value());
}
return req;
}
static json11::Json
fromListResponse(const ListResponse &resp) {
json11::Json::array arr;
for (const auto &a: resp.available) {
arr.push_back(fromPluginStaticData(a));
}
json11::Json::object jo;
jo["available"] = arr;
return json11::Json(jo);
}
static ListResponse
toListResponse(json11::Json j, std::string &err) {
ListResponse resp;
for (const auto &a: j["available"].array_items()) {
resp.available.push_back(toPluginStaticData(a, err));
if (failed(err)) return {};
}
return resp;
}
static json11::Json
fromLoadRequest(const LoadRequest &req) {
json11::Json::object jo;
jo["key"] = req.pluginKey;
jo["inputSampleRate"] = req.inputSampleRate;
jo["adapterFlags"] = fromAdapterFlags(req.adapterFlags);
return json11::Json(jo);
}
static LoadRequest
toLoadRequest(json11::Json j, std::string &err) {
if (!j.has_shape({
{ "key", json11::Json::STRING },
{ "inputSampleRate", json11::Json::NUMBER } }, err)) {
err = "malformed load request: " + err;
return {};
}
LoadRequest req;
req.pluginKey = j["key"].string_value();
req.inputSampleRate = float(j["inputSampleRate"].number_value());
if (!j["adapterFlags"].is_null()) {
req.adapterFlags = toAdapterFlags(j["adapterFlags"], err);
if (failed(err)) return {};
}
return req;
}
static json11::Json
fromLoadResponse(const LoadResponse &resp,
const PluginHandleMapper &pmapper) {
json11::Json::object jo;
jo["handle"] = double(pmapper.pluginToHandle(resp.plugin));
jo["staticData"] = fromPluginStaticData(resp.staticData);
jo["defaultConfiguration"] =
fromPluginConfiguration(resp.defaultConfiguration);
jo["programParameters"] =
fromProgramParameters(resp.programParameters);
return json11::Json(jo);
}
static LoadResponse
toLoadResponse(json11::Json j,
const PluginHandleMapper &pmapper, std::string &err) {
if (!j.has_shape({
{ "handle", json11::Json::NUMBER },
{ "staticData", json11::Json::OBJECT },
{ "defaultConfiguration", json11::Json::OBJECT } }, err)) {
err = "malformed load response: " + err;
return {};
}
LoadResponse resp;
auto h = j["handle"].int_value();
resp.plugin = pmapper.handleToPlugin(h);
resp.staticData = toPluginStaticData(j["staticData"], err);
if (failed(err)) return {};
resp.defaultConfiguration =
toPluginConfiguration(j["defaultConfiguration"], err);
if (failed(err)) return {};
if (j.object_items().find("programParameters") !=
j.object_items().end()) {
resp.programParameters =
toProgramParameters(j["programParameters"], err);
if (failed(err)) return {};
}
return resp;
}
static json11::Json
fromConfigurationRequest(const ConfigurationRequest &cr,
const PluginHandleMapper &pmapper) {
json11::Json::object jo;
jo["handle"] = double(pmapper.pluginToHandle(cr.plugin));
jo["configuration"] = fromPluginConfiguration(cr.configuration);
return json11::Json(jo);
}
static ConfigurationRequest
toConfigurationRequest(json11::Json j,
const PluginHandleMapper &pmapper, std::string &err) {
if (!j.has_shape({
{ "handle", json11::Json::NUMBER },
{ "configuration", json11::Json::OBJECT } }, err)) {
err = "malformed configuration request: " + err;
return {};
}
ConfigurationRequest cr;
auto h = j["handle"].int_value();
cr.plugin = pmapper.handleToPlugin(h);
cr.configuration = toPluginConfiguration(j["configuration"], err);
if (failed(err)) return {};
return cr;
}
static json11::Json
fromConfigurationResponse(const ConfigurationResponse &cr,
const PluginHandleMapper &pmapper) {
json11::Json::object jo;
jo["handle"] = double(pmapper.pluginToHandle(cr.plugin));
json11::Json::array outs;
for (auto &d: cr.outputs) {
auto id = d.identifier;
StaticOutputDescriptor sd;
if (cr.staticOutputInfo.find(id) != cr.staticOutputInfo.end()) {
sd = cr.staticOutputInfo.at(id);
}
outs.push_back(fromOutputDescriptor(d, sd));
}
jo["outputList"] = outs;
json11::Json::object framing;
framing["stepSize"] = cr.framing.stepSize;
framing["blockSize"] = cr.framing.blockSize;
jo["framing"] = framing;
return json11::Json(jo);
}
static ConfigurationResponse
toConfigurationResponse(json11::Json j,
const PluginHandleMapper &pmapper, std::string &err) {
ConfigurationResponse cr;
if (!j["framing"].has_shape({
{ "stepSize", json11::Json::NUMBER },
{ "blockSize", json11::Json::NUMBER } }, err)) {
err = "malformed framing: " + err;
return {};
}
if (!j["outputList"].is_array()) {
err = "array expected for output list";
return {};
}
auto h = j["handle"].int_value();
cr.plugin = pmapper.handleToPlugin(h);
for (const auto &o: j["outputList"].array_items()) {
auto odpair = toOutputDescriptor(o, err);
if (failed(err)) return {};
cr.outputs.push_back(odpair.first);
cr.staticOutputInfo[odpair.first.identifier] = odpair.second;
}
cr.framing.stepSize = int(round(j["framing"]["stepSize"].number_value()));
cr.framing.blockSize = int(round(j["framing"]["blockSize"].number_value()));
return cr;
}
static json11::Json
fromProcessRequest(const ProcessRequest &r,
const PluginHandleMapper &pmapper,
BufferSerialisation serialisation) {
json11::Json::object jo;
jo["handle"] = double(pmapper.pluginToHandle(r.plugin));
json11::Json::object io;
io["timestamp"] = fromRealTime(r.timestamp);
json11::Json::array chans;
for (size_t i = 0; i < r.inputBuffers.size(); ++i) {
if (serialisation == BufferSerialisation::Array) {
chans.push_back(json11::Json::array(r.inputBuffers[i].begin(),
r.inputBuffers[i].end()));
} else {
chans.push_back(fromFloatBuffer(r.inputBuffers[i].data(),
r.inputBuffers[i].size()));
}
}
io["inputBuffers"] = chans;
jo["processInput"] = io;
return json11::Json(jo);
}
static ProcessRequest
toProcessRequest(json11::Json j,
const PluginHandleMapper &pmapper,
BufferSerialisation &serialisation, std::string &err) {
if (!j.has_shape({
{ "handle", json11::Json::NUMBER },
{ "processInput", json11::Json::OBJECT } }, err)) {
err = "malformed process request: " + err;
return {};
}
auto input = j["processInput"];
if (!input.has_shape({
{ "timestamp", json11::Json::OBJECT },
{ "inputBuffers", json11::Json::ARRAY } }, err)) {
err = "malformed process request: " + err;
return {};
}
ProcessRequest r;
auto h = j["handle"].int_value();
r.plugin = pmapper.handleToPlugin(h);
r.timestamp = toRealTime(input["timestamp"], err);
if (failed(err)) return {};
for (const auto &a: input["inputBuffers"].array_items()) {
if (a.is_string()) {
std::vector<float> buf = toFloatBuffer(a.string_value(),
err);
if (failed(err)) return {};
r.inputBuffers.push_back(buf);
serialisation = BufferSerialisation::Base64;
} else if (a.is_array()) {
std::vector<float> buf;
for (auto v : a.array_items()) {
buf.push_back(float(v.number_value()));
}
r.inputBuffers.push_back(buf);
serialisation = BufferSerialisation::Array;
} else {
err = "expected arrays or strings in inputBuffers array";
return {};
}
}
return r;
}
private: // go private briefly for a couple of helper functions
static void
checkRpcRequestType(json11::Json j, std::string expected, std::string &err) {
if (!j["method"].is_string()) {
err = "string expected for method";
return;
}
if (j["method"].string_value() != expected) {
err = "expected value \"" + expected + "\" for type";
return;
}
if (!j["params"].is_null() &&
!j["params"].is_object()) {
err = "object expected for params";
return;
}
if (!j["id"].is_null() &&
!j["id"].is_number() &&
!j["id"].is_string()) {
err = "number or string expected for id";
return;
}
if (!j["jsonrpc"].is_null() &&
!j["jsonrpc"].is_string()) {
err = "string expected for jsonrpc";
return;
}
for (const auto &kv: j.object_items()) {
if (kv.first != "method" &&
kv.first != "params" &&
kv.first != "id" &&
kv.first != "jsonrpc") {
err = "unexpected field \"" + kv.first + "\" in rpc request object";
return;
}
}
}
static bool
successful(json11::Json j, std::string &err) {
const bool hasResult = j["result"].is_object();
const bool hasError = j["error"].is_object();
if (hasResult && hasError) {
err = "valid response may contain only one of result and error objects";
return false;
} else if (hasError) {
return false;
} else if (!hasResult) {
err = "either a result or an error object is required for a valid response";
return false;
} else {
return true;
}
}
static void
markRPC(json11::Json::object &jo) {
jo["jsonrpc"] = "2.0";
}
static void
addId(json11::Json::object &jo, const json11::Json &id) {
if (!id.is_null()) {
jo["id"] = id;
}
}
public:
static json11::Json
fromRpcRequest_List(const ListRequest &req,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "list";
jo["params"] = fromListRequest(req);
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcResponse_List(const ListResponse &resp,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "list";
jo["result"] = fromListResponse(resp);
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcRequest_Load(const LoadRequest &req,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "load";
jo["params"] = fromLoadRequest(req);
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcResponse_Load(const LoadResponse &resp,
const PluginHandleMapper &pmapper,
const json11::Json &id) {
if (resp.plugin) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "load";
jo["result"] = fromLoadResponse(resp, pmapper);
addId(jo, id);
return json11::Json(jo);
} else {
return fromError("Failed to load plugin", RRType::Load, id);
}
}
static json11::Json
fromRpcRequest_Configure(const ConfigurationRequest &req,
const PluginHandleMapper &pmapper,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "configure";
jo["params"] = fromConfigurationRequest(req, pmapper);
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcResponse_Configure(const ConfigurationResponse &resp,
const PluginHandleMapper &pmapper,
const json11::Json &id) {
if (!resp.outputs.empty()) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "configure";
jo["result"] = fromConfigurationResponse(resp, pmapper);
addId(jo, id);
return json11::Json(jo);
} else {
return fromError("Failed to configure plugin", RRType::Configure, id);
}
}
static json11::Json
fromRpcRequest_Process(const ProcessRequest &req,
const PluginHandleMapper &pmapper,
BufferSerialisation serialisation,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
jo["method"] = "process";
jo["params"] = fromProcessRequest(req, pmapper, serialisation);
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcResponse_Process(const ProcessResponse &resp,
const PluginHandleMapper &pmapper,
BufferSerialisation serialisation,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
json11::Json::object po;
po["handle"] = double(pmapper.pluginToHandle(resp.plugin));
po["features"] = fromFeatureSet(resp.features,
*pmapper.pluginToOutputIdMapper(resp.plugin),
serialisation);
jo["method"] = "process";
jo["result"] = po;
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcRequest_Finish(const FinishRequest &req,
const PluginHandleMapper &pmapper,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
json11::Json::object fo;
fo["handle"] = double(pmapper.pluginToHandle(req.plugin));
jo["method"] = "finish";
jo["params"] = fo;
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromRpcResponse_Finish(const FinishResponse &resp,
const PluginHandleMapper &pmapper,
BufferSerialisation serialisation,
const json11::Json &id) {
json11::Json::object jo;
markRPC(jo);
json11::Json::object po;
po["handle"] = double(pmapper.pluginToHandle(resp.plugin));
po["features"] = fromFeatureSet(resp.features,
*pmapper.pluginToOutputIdMapper(resp.plugin),
serialisation);
jo["method"] = "finish";
jo["result"] = po;
addId(jo, id);
return json11::Json(jo);
}
static json11::Json
fromError(std::string errorText,
RRType responseType,
const json11::Json &id,
bool writeVerbatimError = false) {
json11::Json::object jo;
markRPC(jo);
std::string type;
if (responseType == RRType::List) type = "list";
else if (responseType == RRType::Load) type = "load";
else if (responseType == RRType::Configure) type = "configure";
else if (responseType == RRType::Process) type = "process";
else if (responseType == RRType::Finish) type = "finish";
else type = "invalid";
json11::Json::object eo;
eo["code"] = 0;
if (responseType == RRType::NotValid || writeVerbatimError) {
eo["message"] = errorText;
} else {
eo["message"] =
std::string("error in ") + type + " request: " + errorText;
}
jo["method"] = type;
jo["error"] = eo;
addId(jo, id);
return json11::Json(jo);
}
static RRType
getRequestResponseType(json11::Json j, std::string &err) {
if (!j["method"].is_string()) {
err = "string expected for method";
return RRType::NotValid;
}
std::string type = j["method"].string_value();
if (type == "list") return RRType::List;
else if (type == "load") return RRType::Load;
else if (type == "configure") return RRType::Configure;
else if (type == "process") return RRType::Process;
else if (type == "finish") return RRType::Finish;
else if (type == "invalid") return RRType::NotValid;
else {
err = "unknown or unexpected request/response type \"" + type + "\"";
return RRType::NotValid;
}
}
static ListRequest
toRpcRequest_List(json11::Json j, std::string &err) {
checkRpcRequestType(j, "list", err);
if (failed(err)) return {};
return toListRequest(j["params"], err);
}
static ListResponse
toRpcResponse_List(json11::Json j, std::string &err) {
ListResponse resp;
if (successful(j, err) && !failed(err)) {
resp = toListResponse(j["result"], err);
}
return resp;
}
static LoadRequest
toRpcRequest_Load(json11::Json j, std::string &err) {
checkRpcRequestType(j, "load", err);
if (failed(err)) return {};
return toLoadRequest(j["params"], err);
}
static LoadResponse
toRpcResponse_Load(json11::Json j,
const PluginHandleMapper &pmapper,
std::string &err) {
LoadResponse resp;
if (successful(j, err) && !failed(err)) {
resp = toLoadResponse(j["result"], pmapper, err);
}
return resp;
}
static ConfigurationRequest
toRpcRequest_Configure(json11::Json j,
const PluginHandleMapper &pmapper,
std::string &err) {
checkRpcRequestType(j, "configure", err);
if (failed(err)) return {};
return toConfigurationRequest(j["params"], pmapper, err);
}
static ConfigurationResponse
toRpcResponse_Configure(json11::Json j,
const PluginHandleMapper &pmapper,
std::string &err) {
ConfigurationResponse resp;
if (successful(j, err) && !failed(err)) {
resp = toConfigurationResponse(j["result"], pmapper, err);
}
return resp;
}
static ProcessRequest
toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper,
BufferSerialisation &serialisation, std::string &err) {
checkRpcRequestType(j, "process", err);
if (failed(err)) return {};
return toProcessRequest(j["params"], pmapper, serialisation, err);
}
static ProcessResponse
toRpcResponse_Process(json11::Json j,
const PluginHandleMapper &pmapper,
BufferSerialisation &serialisation, std::string &err) {
ProcessResponse resp;
if (successful(j, err) && !failed(err)) {
auto jc = j["result"];
auto h = jc["handle"].int_value();
resp.plugin = pmapper.handleToPlugin(h);
resp.features = toFeatureSet(jc["features"],
*pmapper.handleToOutputIdMapper(h),
serialisation, err);
}
return resp;
}
static FinishRequest
toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper,
std::string &err) {
checkRpcRequestType(j, "finish", err);
if (failed(err)) return {};
FinishRequest req;
auto h = j["params"]["handle"].int_value();
req.plugin = pmapper.handleToPlugin(h);
return req;
}
static FinishResponse
toRpcResponse_Finish(json11::Json j,
const PluginHandleMapper &pmapper,
BufferSerialisation &serialisation, std::string &err) {
FinishResponse resp;
if (successful(j, err) && !failed(err)) {
auto jc = j["result"];
auto h = jc["handle"].int_value();
resp.plugin = pmapper.handleToPlugin(h);
resp.features = toFeatureSet(jc["features"],
*pmapper.handleToOutputIdMapper(h),
serialisation, err);
}
return resp;
}
};
}
#endif
|