From 6258a82af556285658a542876175b04039cd2707 Mon Sep 17 00:00:00 2001 From: Richard Knight Date: Sun, 18 Sep 2022 21:56:26 +0100 Subject: jsongetval added; deinit incorporated but not used as counterproductive for globals etc --- README.md | 28 ++++++++++++++ include/handling.h | 9 +++++ src/opcodes.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4780e09..a6a5742 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,34 @@ Get a JSON object handle of the object contained in the specified key or index. * **index** index for accessing an array +### jsongetval +Get a numeric or string value from an object by string key or numeric index. + + Svalue jsongetval iJson, Skey + ivalue jsongetval iJson, Skey + Svalue jsongetval iJson, index + ivalue jsongetval iJson, index +* **Svalue** string value returned +* **ivalue** numeric value returned +* **iJson** JSON object handle to evaluate +* **Skey** key for accessing an object +* **index** index for accessing an array + + +## jsongetvalk +Get a numeric or string value from an object by string key or numeric index, at k-rate. + + Svalue jsongetvalk iJson, Skey + kvalue jsongetvalk iJson, Skey + Svalue jsongetvalk iJson, kindex + kvalue jsongetvalk iJson, kindex +* **Svalue** string value returned +* **kvalue** numeric value returned +* **iJson** JSON object handle to evaluate +* **Skey** key for accessing an object +* **kindex** index for accessing an array + + ### jsonpath Perform a JSONPath query and obtain the resulting JSON object handle. diff --git a/include/handling.h b/include/handling.h index c5e0691..2eed1f6 100644 --- a/include/handling.h +++ b/include/handling.h @@ -50,4 +50,13 @@ MYFLT createHandle(csnd::Csound* csound, T** data, const char* name) { return FL(handle); } +/* + * Destroy global object + */ +void destroyHandle(csnd::Csound* csound, MYFLT handle, const char* name) { + char buffer[32]; + snprintf(buffer, 32, handleIdentifier, name, (int)handle); + csound->destroy_global_variable(buffer); +} + diff --git a/src/opcodes.cpp b/src/opcodes.cpp index f3eccd6..a241a74 100644 --- a/src/opcodes.cpp +++ b/src/opcodes.cpp @@ -130,6 +130,23 @@ int getJsonType(JSONSession* jsonSession) { #define _PLUGINSESSIONBASE(idInArgs)\ JSONSession* jsonSession;\ + JSONSession* jsonDeinitSession;\ + MYFLT handleDeinit;\ + int deinit() {\ + if (jsonDeinitSession != nullptr && jsonDeinitSession->active) {\ + jsonSession->data.~basic_json();\ + jsonSession->active = false;\ + }\ + if (handleDeinit != -1) {\ + destroyHandle(csound, handleDeinit, handleName);\ + }\ + return OK;\ + }\ + void registerDeinit(JSONSession* jsonDeinitSession, MYFLT handleDeinit) {\ + this->jsonDeinitSession = jsonDeinitSession;\ + this->handleDeinit = handleDeinit;\ + csound->plugin_deinit(this);\ + }\ void getSession() {\ if (!(jsonSession = getHandle(csound, idInArgs[0], handleName))) {\ throw std::runtime_error(badHandle);\ @@ -147,6 +164,8 @@ int getJsonType(JSONSession* jsonSession) { ARGT* otypes = votypes;\ ARGT* itypes = vitypes;\ int init() {\ + jsonDeinitSession = nullptr;\ + handleDeinit = -1;\ try {\ if (doGetSession) getSession();\ irun();\ @@ -230,6 +249,7 @@ struct jsonloads : plugin<1, 1> { outargs[0] = createHandle(csound, &jsonSession, handleName); jsonSession->active = true; jsonSession->data = jsoncons::json::parse(std::string(inargs.str_data(0).data)); + //registerDeinit(jsonSession, outargs[0]); } }; @@ -243,6 +263,7 @@ struct jsoninit : plugin<1, 0> { outargs[0] = createHandle(csound, &jsonSession, handleName); jsonSession->active = true; jsonSession->data = jsoncons::json::parse("{}"); + //registerDeinit(jsonSession, outargs[0]); } }; @@ -252,7 +273,7 @@ struct jsoninit : plugin<1, 0> { */ struct jsonmerge : inplug<3> { INPLUGINIT("iio") - int irun() { + void irun() { JSONSession* jsonSession2; getSession(args[1], &jsonSession2); if (args[2] == 1) { @@ -260,7 +281,6 @@ struct jsonmerge : inplug<3> { } else { jsonSession->data.merge(jsonSession2->data); } - } }; @@ -535,6 +555,83 @@ struct jsonsizeK : jsonsizeBase { }; +/* + Get string value by string key + */ +struct jsongetvalStringStringBase : plugin<1, 2> { + void run() { + STRINGDAT &input = inargs.str_data(1); + STRINGDAT &output = outargs.str_data(0); + jsoncons::json selected = jsonSession->data[std::string(input.data)]; + std::string value = selected.as(); + output.size = value.size(); + output.data = csound->strdup((char*) value.c_str()); + } +}; +struct jsongetvalStringString : jsongetvalStringStringBase { + PLUGINCHILD("S", "iS", true) +}; +struct jsongetvalStringStringK : jsongetvalStringStringBase { + PLUGINCHILDK("S", "iS", true) +}; + + +/* + Get numeric value by string key + */ +struct jsongetvalNumericStringBase : plugin<1, 2> { + void run() { + STRINGDAT &input = inargs.str_data(1); + jsoncons::json selected = jsonSession->data[std::string(input.data)]; + outargs[0] = selected.as(); + } +}; +struct jsongetvalNumericString : jsongetvalNumericStringBase { + PLUGINCHILD("i", "iS", true) +}; +struct jsongetvalNumericStringK : jsongetvalNumericStringBase { + PLUGINCHILDK("k", "iS", true) +}; + + +/* + Get string value by numeric index + */ +struct jsongetvalStringNumericBase : plugin<1, 2> { + void run() { + STRINGDAT &output = outargs.str_data(0); + jsoncons::json selected = jsonSession->data[(int) inargs[1]]; + std::string value = selected.as(); + output.size = value.size(); + output.data = csound->strdup((char*) value.c_str()); + } +}; +struct jsongetvalStringNumeric : jsongetvalStringNumericBase { + PLUGINCHILD("S", "ii", true) +}; +struct jsongetvalStringNumericK : jsongetvalStringNumericBase { + PLUGINCHILDK("S", "ik", true) +}; + + +/* + Get numeric value by numeric index + */ +struct jsongetvalNumericNumericBase : plugin<1, 2> { + void run() { + jsoncons::json selected = jsonSession->data[(int) inargs[1]]; + outargs[0] = selected.as(); + } +}; +struct jsongetvalNumericNumeric : jsongetvalNumericNumericBase { + PLUGINCHILD("i", "ii", true) +}; +struct jsongetvalNumericNumericK : jsongetvalNumericNumericBase { + PLUGINCHILDK("k", "ik", true) +}; + + + /* Get object from an object by key name */ @@ -957,6 +1054,16 @@ void csnd::on_load(csnd::Csound *csound) { csnd::plugin(csound, "jsonkeysk", csnd::thread::ik); csnd::plugin(csound, "jsonget.S", csnd::thread::i); csnd::plugin(csound, "jsonget.i", csnd::thread::i); + + csnd::plugin(csound, "jsongetval.SS", csnd::thread::i); + csnd::plugin(csound, "jsongetval.iS", csnd::thread::i); + csnd::plugin(csound, "jsongetval.Si", csnd::thread::i); + csnd::plugin(csound, "jsongetval.ii", csnd::thread::i); + csnd::plugin(csound, "jsongetvalk.SS", csnd::thread::ik); + csnd::plugin(csound, "jsongetvalk.kS", csnd::thread::ik); + csnd::plugin(csound, "jsongetvalk.Sk", csnd::thread::ik); + csnd::plugin(csound, "jsongetvalk.kk", csnd::thread::ik); + csnd::plugin(csound, "jsonsize", csnd::thread::i); csnd::plugin(csound, "jsonsizek", csnd::thread::ik); -- cgit v1.2.3