From b60bcda728f7bce9e5753aa20e59b29a4479aab8 Mon Sep 17 00:00:00 2001 From: Richard Knight Date: Thu, 22 Sep 2022 15:17:08 +0100 Subject: added jsonptrval and jsonptrarr opcodes; fix of jsonptradd --- README.md | 41 +++++++++++++++++++- src/opcodes.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6a5742..646ea06 100644 --- a/README.md +++ b/README.md @@ -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(); + 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(); + } +}; +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 vals = queried.as>(); + 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(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(csound, "jsonpathrpl", csnd::thread::i); csnd::plugin(csound, "jsonptr", csnd::thread::i); + + csnd::plugin(csound, "jsonptrarr", csnd::thread::i); + + csnd::plugin(csound, "jsonptrval.Sa", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.Sak", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.S", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.Sk", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.i", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.k", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.ia", csnd::thread::i); + csnd::plugin(csound, "jsonptrval.ka", csnd::thread::i); + csnd::plugin(csound, "jsonptrhas", csnd::thread::i); csnd::plugin(csound, "jsonptrhask", csnd::thread::ik); csnd::plugin(csound, "jsonptraddval.S", csnd::thread::i); -- cgit v1.2.3