aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Knight <q@1bpm.net>2022-09-18 21:56:26 +0100
committerRichard Knight <q@1bpm.net>2022-09-18 21:56:26 +0100
commit6258a82af556285658a542876175b04039cd2707 (patch)
treee0afa33dfa1b9c7a6ab19090297dbf41e1d686d1
parent05a9a94ba5af5f7efa1fbc7acc2f4045ad13f5f6 (diff)
downloadcsound-json-6258a82af556285658a542876175b04039cd2707.tar.gz
csound-json-6258a82af556285658a542876175b04039cd2707.tar.bz2
csound-json-6258a82af556285658a542876175b04039cd2707.zip
jsongetval added; deinit incorporated but not used as counterproductive for globals etcv1.2
-rw-r--r--README.md28
-rw-r--r--include/handling.h9
-rw-r--r--src/opcodes.cpp111
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<JSONSession>(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<JSONSession>(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<JSONSession>(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);
}
-
}
};
@@ -536,6 +556,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<std::string>();
+ 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<MYFLT>();
+ }
+};
+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<std::string>();
+ 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<MYFLT>();
+ }
+};
+struct jsongetvalNumericNumeric : jsongetvalNumericNumericBase {
+ PLUGINCHILD("i", "ii", true)
+};
+struct jsongetvalNumericNumericK : jsongetvalNumericNumericBase {
+ PLUGINCHILDK("k", "ik", true)
+};
+
+
+
+/*
Get object from an object by key name
*/
struct jsongetString : plugin<1, 2> {
@@ -957,6 +1054,16 @@ void csnd::on_load(csnd::Csound *csound) {
csnd::plugin<jsonkeysK>(csound, "jsonkeysk", csnd::thread::ik);
csnd::plugin<jsongetString>(csound, "jsonget.S", csnd::thread::i);
csnd::plugin<jsongetNumeric>(csound, "jsonget.i", csnd::thread::i);
+
+ csnd::plugin<jsongetvalStringString>(csound, "jsongetval.SS", csnd::thread::i);
+ csnd::plugin<jsongetvalNumericString>(csound, "jsongetval.iS", csnd::thread::i);
+ csnd::plugin<jsongetvalStringNumeric>(csound, "jsongetval.Si", csnd::thread::i);
+ csnd::plugin<jsongetvalNumericNumeric>(csound, "jsongetval.ii", csnd::thread::i);
+ csnd::plugin<jsongetvalStringStringK>(csound, "jsongetvalk.SS", csnd::thread::ik);
+ csnd::plugin<jsongetvalNumericStringK>(csound, "jsongetvalk.kS", csnd::thread::ik);
+ csnd::plugin<jsongetvalStringNumericK>(csound, "jsongetvalk.Sk", csnd::thread::ik);
+ csnd::plugin<jsongetvalNumericNumericK>(csound, "jsongetvalk.kk", csnd::thread::ik);
+
csnd::plugin<jsonsize>(csound, "jsonsize", csnd::thread::i);
csnd::plugin<jsonsizeK>(csound, "jsonsizek", csnd::thread::ik);