diff options
author | Richard Knight <q@1bpm.net> | 2022-09-22 15:17:08 +0100 |
---|---|---|
committer | Richard Knight <q@1bpm.net> | 2022-09-22 15:17:08 +0100 |
commit | b60bcda728f7bce9e5753aa20e59b29a4479aab8 (patch) | |
tree | b5a7050604254ca8f77591acf7884081c3fe7881 | |
parent | 6258a82af556285658a542876175b04039cd2707 (diff) | |
download | csound-json-b60bcda728f7bce9e5753aa20e59b29a4479aab8.tar.gz csound-json-b60bcda728f7bce9e5753aa20e59b29a4479aab8.tar.bz2 csound-json-b60bcda728f7bce9e5753aa20e59b29a4479aab8.zip |
-rw-r--r-- | README.md | 41 | ||||
-rw-r--r-- | src/opcodes.cpp | 118 |
2 files changed, 157 insertions, 2 deletions
@@ -224,7 +224,7 @@ Get a numeric or string value from an object by string key or numeric index. * **index** index for accessing an array -## jsongetvalk +### jsongetvalk Get a numeric or string value from an object by string key or numeric index, at k-rate. Svalue jsongetvalk iJson, Skey @@ -278,6 +278,45 @@ Perform a JSON Pointer query and obtain the resulting JSON object handle. * **Spointer** JSON Pointer expression +### jsonptrval +Use a JSON Pointer query to obtain a string/numeric value, or an array of string/numeric. + + ivalue jsonptrval iJson, Spointer + Svalue jsonptrval iJson, Spointer + ivalues[] jsonptrval iJson, Spointer + Svalues[] jsonptrval iJson, Spointer +* **ivalue** numeric output value +* **Svalue** string output value +* **ivalues[]** numeric array output values +* **Svalues[]** string array output values +* **iJson** JSON object handle to evaluate +* **Spointer** JSON Pointer expression + + +### jsonptrvalk +Use a JSON Pointer query to obtain a string/numeric value, or an array of string/numeric, at k-rate. + + kvalue jsonptrvalk iJson, Spointer + Svalue jsonptrvalk iJson, Spointer + kvalues[] jsonptrvalk iJson, Spointer + Svalues[] jsonptrvalk iJson, Spointer +* **kvalue** numeric output value +* **Svalue** string output value +* **kvalues[]** numeric array output values +* **Svalues[]** string array output values +* **iJson** JSON object handle to evaluate +* **Spointer** JSON Pointer expression + + +### jsonptrarr +Get an array of JSON object handles from a JSON Pointer query. + + iJsonObjects[] jsonptrarr iJson, Spointer +* **iJsonObjects[]** array of JSON object handles +* **iJson** JSON object handle to evaluate +* **Spointer** JSON Pointer expression + + ### jsonptrhas Check if a JSON Pointer query results in a valid existing object. diff --git a/src/opcodes.cpp b/src/opcodes.cpp index a241a74..8f76317 100644 --- a/src/opcodes.cpp +++ b/src/opcodes.cpp @@ -753,6 +753,110 @@ struct jsonptr : plugin<1, 2> { } }; + +/* + Get string value by JSON Pointer + */ +struct jsonptrvalStringBase : plugin<1, 2> { + void run() { + STRINGDAT &output = outargs.str_data(0); + jsoncons::json queried = jsoncons::jsonpointer::get( + jsonSession->data, std::string(inargs.str_data(1).data) + ); + std::string value = queried.as<std::string>(); + output.size = value.size(); + output.data = csound->strdup((char*) value.c_str()); + } +}; +struct jsonptrvalString : jsonptrvalStringBase { + PLUGINCHILD("S", "iS", true) +}; +struct jsonptrvalStringK : jsonptrvalStringBase { + PLUGINCHILDK("S", "iS", true) +}; + + +/* + Get string array value by JSON Pointer + */ +struct jsonptrvalStringArrayBase : plugin<1, 2> { + void run() { + jsoncons::json queried = jsoncons::jsonpointer::get( + jsonSession->data, std::string(inargs.str_data(1).data) + ); + jsonArrayToCSArray(csound, &queried, (ARRAYDAT*) outargs(0), true); + } +}; +struct jsonptrvalStringArray : jsonptrvalStringArrayBase { + PLUGINCHILD("S[]", "iS", true) +}; +struct jsonptrvalStringArrayK : jsonptrvalStringArrayBase { + PLUGINCHILDK("S[]", "iS", true) +}; + + +/* + Get numeric value by JSON Pointer + */ +struct jsonptrvalNumericBase : plugin<1, 2> { + void run() { + jsoncons::json queried = jsoncons::jsonpointer::get( + jsonSession->data, std::string(inargs.str_data(1).data) + ); + outargs[0] = queried.as<MYFLT>(); + } +}; +struct jsonptrvalNumeric : jsonptrvalNumericBase { + PLUGINCHILD("i", "iS", true) +}; +struct jsonptrvalNumericK : jsonptrvalNumericBase { + PLUGINCHILDK("k", "iS", true) +}; + + +/* + Get numeric array value by JSON Pointer + */ +struct jsonptrvalNumericArrayBase : plugin<1, 2> { + void run() { + jsoncons::json queried = jsoncons::jsonpointer::get( + jsonSession->data, std::string(inargs.str_data(1).data) + ); + jsonArrayToCSArray(csound, &queried, (ARRAYDAT*) outargs(0), false); + } +}; +struct jsonptrvalNumericArray : jsonptrvalNumericArrayBase { + PLUGINCHILD("i[]", "iS", true) +}; +struct jsonptrvalNumericArrayK : jsonptrvalNumericArrayBase { + PLUGINCHILDK("k[]", "iS", true) +}; + + +/* + Get numeric array value by JSON Pointer + */ +struct jsonptrarr : plugin<1, 2> { + PLUGINIT("i[]", "iS", true) + void irun() { + JSONSession* jsonSession2; + jsoncons::json queried = jsoncons::jsonpointer::get( + jsonSession->data, std::string(inargs.str_data(1).data) + ); + std::vector<jsoncons::json> vals = queried.as<std::vector<jsoncons::json>>(); + ARRAYDAT* array = (ARRAYDAT*) outargs(0); + arrayInit(csound, array, vals.size(), 1); + MYFLT handle; + for (std::size_t index = 0; index < vals.size(); index++) { + handle = createHandle<JSONSession>(csound, &jsonSession2, handleName); + jsonSession2->active = true; + jsonSession2->data = vals[index]; + array->data[index] = handle; + } + } +}; + + /* Check for existence by JSON Pointer */ @@ -820,7 +924,7 @@ struct jsonptradd : inplug<3> { INPLUGINIT("iSi") void irun() { JSONSession* jsonSession2; - getSession(args[1], &jsonSession2); + getSession(args[2], &jsonSession2); jsoncons::jsonpointer::add( jsonSession->data, @@ -1090,6 +1194,18 @@ void csnd::on_load(csnd::Csound *csound) { // csnd::plugin<jsonpathrpl>(csound, "jsonpathrpl", csnd::thread::i); csnd::plugin<jsonptr>(csound, "jsonptr", csnd::thread::i); + + csnd::plugin<jsonptrarr>(csound, "jsonptrarr", csnd::thread::i); + + csnd::plugin<jsonptrvalStringArray>(csound, "jsonptrval.Sa", csnd::thread::i); + csnd::plugin<jsonptrvalStringArrayK>(csound, "jsonptrval.Sak", csnd::thread::i); + csnd::plugin<jsonptrvalString>(csound, "jsonptrval.S", csnd::thread::i); + csnd::plugin<jsonptrvalStringK>(csound, "jsonptrval.Sk", csnd::thread::i); + csnd::plugin<jsonptrvalNumeric>(csound, "jsonptrval.i", csnd::thread::i); + csnd::plugin<jsonptrvalNumericK>(csound, "jsonptrval.k", csnd::thread::i); + csnd::plugin<jsonptrvalNumericArray>(csound, "jsonptrval.ia", csnd::thread::i); + csnd::plugin<jsonptrvalNumericArrayK>(csound, "jsonptrval.ka", csnd::thread::i); + csnd::plugin<jsonptrhas>(csound, "jsonptrhas", csnd::thread::i); csnd::plugin<jsonptrhasK>(csound, "jsonptrhask", csnd::thread::ik); csnd::plugin<jsonptraddvalString>(csound, "jsonptraddval.S", csnd::thread::i); |