Skip to content
Cabbage
Install

Opcodes

cabbageJsonType

This opcode reports the JSON type of the value at a dot-notation path, so the orchestra can branch before querying. It is the typesafe companion to cabbageJsonGet: use it when an empty string or zero is ambiguous (missing vs. present-but-empty, "123" vs. 123).

type:S = cabbageJsonType(json:S, path:S)
  • json:S – a string holding a valid JSON document
  • path:S – dot-notation path into the document, where numeric segments index arrays
  • type:S – one of "string", "number", "boolean", "array", "object", "null", or "missing" (invalid documents report "missing" for every path)
  • Works at k-rate; the document is re-parsed only when the input text changes
instr 1
doc:S = "{\"freq\":\"440\"}"
type:S = cabbageJsonType(doc, "freq")
if strcmp(type, "string") == 0 then
prints("frequency arrived as text, parsing...\n")
endif
freq:k = cabbageJsonGet(doc, "freq")
endin

The following instrument exercises the JSON opcodes against a patcher-style state document:

cabbageJson.csd
<Cabbage>
{
"pluginId": "jsonUtils",
"widgets": [
{"type":"form","caption":"JSON Utilities","size":{"width":500,"height":200}}
]
}
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
; A patcher-style state document: voices with params plus a mix list.
json:S = "{\"voices\":[{\"id\":\"vco1\",\"params\":{\"freq\":440,\"amp\":0.5,\"wave\":\"sine\"}},{\"id\":\"lfo1\",\"params\":{\"rate\":2.5}}],\"mix\":[0.8,0.6]}"
; --- typed queries: the output variable selects the overload ---
wave:S = cabbageJsonGet(json, "voices.0.params.wave")
freq:k = cabbageJsonGet(json, "voices.0.params.freq")
amp:k = cabbageJsonGet(json, "voices.0.params.amp")
mix:k[] = cabbageJsonGet(json, "mix")
has:i = cabbageJsonHas(json, "voices.1.params.rate")
n:i = cabbageJsonLen(json, "voices")
type:S = cabbageJsonType(json, "voices.0.params.freq")
prints("wave=%s type=%s voices=%d has(rate)=%d\n", wave, type, n, has)
; --- play voice 0, scaled by the mix list ---
sig:a = oscili(amp, freq)
outs(sig*mix[0], sig*mix[1])
; --- trigger variant: fires when the result changes ---
id:S, changed:k = cabbageJsonGet(json, "voices.0.id")
printks("active voice: %s\n", changed, id)
; --- builders: patch the document, read it back ---
patched:S = cabbageJsonSet(json, "voices.0.params.freq", 880)
patched = cabbageJsonSet(patched, "voices.0.params.wave", "saw")
newfreq:i = cabbageJsonGet(patched, "voices.0.params.freq")
newwave:S = cabbageJsonGet(patched, "voices.0.params.wave")
prints("patched voice: %s at %f Hz\n", newwave, newfreq)
endin
</CsInstruments>
<CsScore>
i1 0 z
</CsScore>
</CsoundSynthesizer>

Download cabbageJson.csd