Mercurial > hg > piper-cpp
comparison json/VampJson.h @ 24:533ca5ca3404
More on reading and writing json messages
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 23 May 2016 16:09:25 +0100 |
parents | 071c55f52c7d |
children | 5b9690d18241 |
comparison
equal
deleted
inserted
replaced
23:d678cd00e593 | 24:533ca5ca3404 |
---|---|
868 static json11::Json | 868 static json11::Json |
869 fromVampResponse_List(std::string errorText, | 869 fromVampResponse_List(std::string errorText, |
870 const std::vector<Vamp::HostExt::PluginStaticData> &d) { | 870 const std::vector<Vamp::HostExt::PluginStaticData> &d) { |
871 | 871 |
872 json11::Json::object jo; | 872 json11::Json::object jo; |
873 jo["type"] = "list"; | |
873 jo["success"] = (errorText == ""); | 874 jo["success"] = (errorText == ""); |
874 jo["errorText"] = errorText; | 875 jo["errorText"] = errorText; |
875 | 876 |
876 json11::Json::array arr; | 877 json11::Json::array arr; |
877 for (const auto &a: d) { | 878 for (const auto &a: d) { |
878 arr.push_back(fromPluginStaticData(a)); | 879 arr.push_back(fromPluginStaticData(a)); |
879 } | 880 } |
880 jo["response"] = arr; | 881 json11::Json::object po; |
882 po["plugins"] = arr; | |
883 | |
884 jo["content"] = po; | |
881 return json11::Json(jo); | 885 return json11::Json(jo); |
882 } | 886 } |
883 | 887 |
884 static json11::Json | 888 static json11::Json |
885 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { | 889 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { |
893 static json11::Json | 897 static json11::Json |
894 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, | 898 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, |
895 PluginHandleMapper &mapper) { | 899 PluginHandleMapper &mapper) { |
896 | 900 |
897 json11::Json::object jo; | 901 json11::Json::object jo; |
902 jo["type"] = "load"; | |
898 jo["success"] = (resp.plugin != 0); | 903 jo["success"] = (resp.plugin != 0); |
899 jo["errorText"] = ""; | 904 jo["errorText"] = ""; |
900 jo["response"] = fromLoadResponse(resp, mapper); | 905 jo["content"] = fromLoadResponse(resp, mapper); |
901 return json11::Json(jo); | 906 return json11::Json(jo); |
902 } | 907 } |
903 | 908 |
904 static json11::Json | 909 static json11::Json |
905 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, | 910 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, |
913 | 918 |
914 static json11::Json | 919 static json11::Json |
915 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { | 920 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp) { |
916 | 921 |
917 json11::Json::object jo; | 922 json11::Json::object jo; |
923 jo["type"] = "configure"; | |
918 jo["success"] = (!resp.outputs.empty()); | 924 jo["success"] = (!resp.outputs.empty()); |
919 jo["errorText"] = ""; | 925 jo["errorText"] = ""; |
920 jo["response"] = fromConfigurationResponse(resp); | 926 jo["content"] = fromConfigurationResponse(resp); |
921 return json11::Json(jo); | 927 return json11::Json(jo); |
922 } | 928 } |
923 | 929 |
924 static json11::Json | 930 static json11::Json |
925 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, | 931 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, |
933 | 939 |
934 static json11::Json | 940 static json11::Json |
935 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp) { | 941 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp) { |
936 | 942 |
937 json11::Json::object jo; | 943 json11::Json::object jo; |
944 jo["type"] = "process"; | |
938 jo["success"] = true; | 945 jo["success"] = true; |
939 jo["errorText"] = ""; | 946 jo["errorText"] = ""; |
940 jo["response"] = fromFeatureSet(resp.features); | 947 jo["content"] = fromFeatureSet(resp.features); |
941 return json11::Json(jo); | 948 return json11::Json(jo); |
942 } | 949 } |
943 | 950 |
944 static json11::Json | 951 static json11::Json |
945 fromVampRequest_Finish() { | 952 fromVampRequest_Finish(Vamp::Plugin *p, |
953 PluginHandleMapper &mapper) { | |
946 | 954 |
947 json11::Json::object jo; | 955 json11::Json::object jo; |
948 jo["type"] = "finish"; | 956 jo["type"] = "finish"; |
957 json11::Json::object fo; | |
958 fo["pluginHandle"] = mapper.pluginToHandle(p); | |
959 jo["content"] = fo; | |
949 return json11::Json(jo); | 960 return json11::Json(jo); |
950 } | 961 } |
951 | 962 |
952 static json11::Json | 963 static json11::Json |
953 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp) { | 964 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp) { |
954 | 965 |
955 json11::Json::object jo; | 966 json11::Json::object jo; |
967 jo["type"] = "finish"; | |
956 jo["success"] = true; | 968 jo["success"] = true; |
957 jo["errorText"] = ""; | 969 jo["errorText"] = ""; |
958 jo["response"] = fromFeatureSet(resp.features); | 970 jo["content"] = fromFeatureSet(resp.features); |
959 return json11::Json(jo); | 971 return json11::Json(jo); |
972 } | |
973 | |
974 private: // go private briefly for a couple of helper functions | |
975 | |
976 static void | |
977 checkTypeField(json11::Json j, std::string expected) { | |
978 if (!j["type"].is_string()) { | |
979 throw Failure("string expected for type"); | |
980 } | |
981 if (j["type"].string_value() != expected) { | |
982 throw Failure("expected value \"" + expected + "\" for type"); | |
983 } | |
984 } | |
985 | |
986 static bool | |
987 successful(json11::Json j) { | |
988 if (!j["success"].is_bool()) { | |
989 throw Failure("bool expected for success"); | |
990 } | |
991 return j["success"].bool_value(); | |
992 } | |
993 | |
994 public: | |
995 static void | |
996 toVampRequest_List(json11::Json j) { | |
997 | |
998 checkTypeField(j, "list"); | |
999 } | |
1000 | |
1001 static std::vector<Vamp::HostExt::PluginStaticData> | |
1002 toVampResponse_List(json11::Json j) { | |
1003 | |
1004 std::vector<Vamp::HostExt::PluginStaticData> arr; | |
1005 if (successful(j)) { | |
1006 for (const auto &a: j["content"]["plugins"].array_items()) { | |
1007 arr.push_back(toPluginStaticData(a)); | |
1008 } | |
1009 } | |
1010 return arr; | |
1011 } | |
1012 | |
1013 static Vamp::HostExt::LoadRequest | |
1014 toVampRequest_Load(json11::Json j) { | |
1015 | |
1016 checkTypeField(j, "load"); | |
1017 return toLoadRequest(j["content"]); | |
1018 } | |
1019 | |
1020 static Vamp::HostExt::LoadResponse | |
1021 toVampResponse_Load(json11::Json j, PluginHandleMapper &mapper) { | |
1022 | |
1023 Vamp::HostExt::LoadResponse resp; | |
1024 if (successful(j)) { | |
1025 resp = toLoadResponse(j["content"], mapper); | |
1026 } | |
1027 return resp; | |
1028 } | |
1029 | |
1030 static Vamp::HostExt::ConfigurationRequest | |
1031 toVampRequest_Configure(json11::Json j, PluginHandleMapper &mapper) { | |
1032 | |
1033 checkTypeField(j, "configure"); | |
1034 return toConfigurationRequest(j["content"], mapper); | |
1035 } | |
1036 | |
1037 static Vamp::HostExt::ConfigurationResponse | |
1038 toVampResponse_Configure(json11::Json j) { | |
1039 | |
1040 Vamp::HostExt::ConfigurationResponse resp; | |
1041 if (successful(j)) { | |
1042 resp = toConfigurationResponse(j["content"]); | |
1043 } | |
1044 return resp; | |
1045 } | |
1046 | |
1047 static Vamp::HostExt::ProcessRequest | |
1048 toVampRequest_Process(json11::Json j, PluginHandleMapper &mapper) { | |
1049 | |
1050 checkTypeField(j, "process"); | |
1051 return toProcessRequest(j["content"], mapper); | |
1052 } | |
1053 | |
1054 static Vamp::HostExt::ProcessResponse | |
1055 toVampResponse_Process(json11::Json j) { | |
1056 | |
1057 Vamp::HostExt::ProcessResponse resp; | |
1058 if (successful(j)) { | |
1059 resp.features = toFeatureSet(j["content"]); | |
1060 } | |
1061 return resp; | |
1062 } | |
1063 | |
1064 static Vamp::Plugin * | |
1065 toVampRequest_Finish(json11::Json j, PluginHandleMapper &mapper) { | |
1066 | |
1067 checkTypeField(j, "finish"); | |
1068 return mapper.handleToPlugin(j["content"]["pluginHandle"].int_value()); | |
1069 } | |
1070 | |
1071 static Vamp::HostExt::ProcessResponse | |
1072 toVampResponse_Finish(json11::Json j) { | |
1073 | |
1074 Vamp::HostExt::ProcessResponse resp; | |
1075 if (successful(j)) { | |
1076 resp.features = toFeatureSet(j["content"]); | |
1077 } | |
1078 return resp; | |
960 } | 1079 } |
961 }; | 1080 }; |
962 | 1081 |
963 } | 1082 } |
964 | 1083 |