beatbox-puredata/purest_json/json-help.pd

209 lines
8.1 KiB
Plaintext
Raw Permalink Normal View History

2024-09-18 21:11:11 -03:00
#N canvas 79 63 1064 678 10;
#X declare -path purest_json;
#X obj 565 357 json-encode;
#X obj 565 459 json-decode;
#X obj 6 161 json-encode;
#X obj 6 182 json-decode;
#X text 81 163 - encodes data into a JSON string.;
#X text 81 181 - decodes a JSON string to lists.;
#X msg 543 25 add id 1;
#X text 543 6 Will add key id and value of 1 to object;
#X text 585 57 This will add a string with the spaces intact to the JSON object, f 69;
#X msg 634 207 add id 2;
#X text 637 186 adding a different value will overwrite the previously stored one, f 68;
#X msg 488 284 clear;
#X text 390 260 This will clear the object;
#X text 694 222 output the value;
#X msg 694 241 bang;
#N canvas 85 61 536 476 nested-objects 0;
#X obj 76 244 json-encode;
#X obj 76 352 json-decode;
#X msg 76 197 bang;
#X obj 142 153 json-encode;
#X msg 142 199 add newest \$1;
#X obj 76 26 bng 15 250 50 0 empty empty Click_to_create_a_nested_object 17 7 0 10 #fcfcfc #000000 #000000;
#X obj 76 56 t b b b;
#X obj 76 270 t a a;
#X obj 161 352 print JSON_string;
#X obj 76 432 print decoding_done;
#X obj 207 431 print decoded_pair;
#X obj 161 327 spigot;
#X obj 194 301 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X msg 254 199 add name Aphex Twin;
#X msg 142 119 add album Syro \, add year 2014 \, bang \,;
#X obj 207 407 spigot;
#X obj 240 381 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X connect 0 0 7 0;
#X connect 1 0 9 0;
#X connect 1 1 15 0;
#X connect 2 0 0 0;
#X connect 3 0 4 0;
#X connect 4 0 0 0;
#X connect 5 0 6 0;
#X connect 6 0 2 0;
#X connect 6 1 14 0;
#X connect 6 2 13 0;
#X connect 7 0 1 0;
#X connect 7 1 11 0;
#X connect 11 0 8 0;
#X connect 12 0 11 1;
#X connect 13 0 0 0;
#X connect 14 0 3 0;
#X connect 15 0 10 0;
#X connect 16 0 15 1;
#X restore 9 579 pd nested-objects;
#X obj 565 378 t a a;
#N canvas 312 61 801 648 about-decoded-json-strings 0;
#X obj 388 205 json-encode;
#X obj 164 334 json-decode;
#X msg 388 158 bang;
#X obj 388 8 bng 15 250 50 0 empty empty Click_to_create_an_object 17 7 0 10 #fcfcfc #000000 #000000;
#X obj 388 38 t b b b b;
#X msg 439 74 add duration 3000;
#X obj 388 231 t a a a;
#X obj 407 333 json-decode;
#X obj 438 477 line;
#X floatatom 438 510 5 0 0 0 - - - 0;
#X msg 438 457 \$1 \, \$2 \$3;
#X obj 438 392 route start end duration;
#X text 419 312 wrong;
#X obj 227 492 line;
#X floatatom 227 525 5 0 0 0 - - - 0;
#X obj 227 391 route start end duration;
#X obj 227 362 list trim;
#X obj 438 363 list trim;
#X obj 438 435 pack 0 0 0;
#X msg 422 104 add start 100;
#X msg 405 135 add end 500;
#X obj 212 421 f;
#X obj 227 450 pack 0 0 0;
#X msg 227 472 \$1 \, \$2 \$3;
#X text 11 111 This will give you a chance to wait for other data from the JSON object.;
#X obj 473 303 print JSON_string;
#X text 175 299 correct;
#X text 11 13 You should not assume too much about the order of keys and values in a JSON object. In this example \, a JSON string is decoded to drive a [line] object from start to stop using [route] and [pack]. When using [pack] you should not feed the left-most inlet with data immediately \, but store a value \, e.g. in a [f] and bang the store \, with the left outlet of [json-decode].;
#X obj 473 278 spigot;
#X obj 506 252 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X connect 0 0 6 0;
#X connect 1 0 21 0;
#X connect 1 1 16 0;
#X connect 2 0 0 0;
#X connect 3 0 4 0;
#X connect 4 0 2 0;
#X connect 4 1 20 0;
#X connect 4 2 19 0;
#X connect 4 3 5 0;
#X connect 5 0 0 0;
#X connect 6 0 1 0;
#X connect 6 1 7 0;
#X connect 6 2 28 0;
#X connect 7 1 17 0;
#X connect 8 0 9 0;
#X connect 10 0 8 0;
#X connect 11 0 18 0;
#X connect 11 1 18 1;
#X connect 11 2 18 2;
#X connect 13 0 14 0;
#X connect 15 0 21 1;
#X connect 15 1 22 1;
#X connect 15 2 22 2;
#X connect 16 0 15 0;
#X connect 17 0 11 0;
#X connect 18 0 10 0;
#X connect 19 0 0 0;
#X connect 20 0 0 0;
#X connect 21 0 22 0;
#X connect 22 0 23 0;
#X connect 23 0 13 0;
#X connect 28 0 25 0;
#X connect 29 0 28 1;
#X restore 9 600 pd about-decoded-json-strings;
#N canvas 44 61 450 465 json-arrays 0;
#X obj 42 73 json-encode;
#X msg 42 33 clear \, array storage textfile \, array storage couchdb \, array storage array \, bang;
#X obj 42 179 json-decode;
#X obj 42 112 t a a;
#X text 49 11 This will construct a json array.;
#X obj 73 216 list trim;
#X obj 73 243 route storage;
#X obj 73 334 json-decode;
#X obj 73 271 t a a;
#X obj 132 180 print JSON_object_string;
#X obj 158 334 print JSON_array;
#X obj 136 370 print decoded;
#X obj 73 395 print array_member_done;
#X obj 158 304 spigot;
#X obj 191 278 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X obj 132 155 spigot;
#X obj 165 129 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X connect 0 0 3 0;
#X connect 1 0 0 0;
#X connect 2 1 5 0;
#X connect 3 0 2 0;
#X connect 3 1 15 0;
#X connect 5 0 6 0;
#X connect 6 0 8 0;
#X connect 7 0 12 0;
#X connect 7 1 11 0;
#X connect 8 0 7 0;
#X connect 8 1 13 0;
#X connect 13 0 10 0;
#X connect 14 0 13 1;
#X connect 15 0 9 0;
#X connect 16 0 15 1;
#X restore 9 622 pd json-arrays;
#X text 6 559 [json-decode] treats json arrays as a series of objects.;
#X obj 669 477 print JSON_string;
#X msg 699 284 write /tmp/test.json;
#X msg 704 325 read test.json;
#X text 698 267 write the content as JSON to a file;
#X text 703 308 read data from a JSON file;
#X text 6 202 [json-encode] has six methods: add \, array \, clear \, bang \, write and read.;
#X text 6 277 array adds a new value to an array.;
#X text 6 295 clear clears the internally stored object.;
#X text 6 313 bang outputs the stored object as a JSON string.;
#X text 6 233 add adds a new key/value pair to the internally stored object. If you call add with a value that is already a JSON string \, then the object will be added as a nested object.;
#X text 6 331 read reads JSON data from a file. This file must contain exactly one JSON object \, as [json-encode] only stores one object internally.;
#X text 6 375 write writes the internally stored data as JSON to a file.;
#X obj 264 394 qlist;
#X text 6 393 add \, read and write methods work analog to;
#X obj 468 587 print decoding_done;
#X obj 596 587 print decoded_data;
#X obj 719 587 print not-a-json-object;
#X msg 449 417 no json;
#X text 6 411 [json-decode] will accept a JSON string at its input and will output the decoded object as a series of lists of key/value pairs on the middle outlet. Boolean values of TRUE/FALSE will be translated to 1 or 0 \, float and integer values will be output as floats. Nested objects and arrays are output as strings \, that can be parsed. After the object is fully parsed \, the left outlet will issue a bang \, so that you can differenciate between subsequent objects. The messages will come as list key value. If the string cannot be decoded as JSON \, then the right outlet will output a bang.;
#X obj 9 648 declare -path purest_json;
#X obj 596 564 spigot;
#X obj 629 542 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X obj 669 456 spigot;
#X obj 702 433 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X text 6 6 WARNING: [Print]ing JSON values to console may lead to undesired behaviour \, because it usually contains special characters such as curly braces \, backslashes \, or quotation marks that may be interpreted as tcl commands and therefore break the console or even crash Pd.;
#X text 6 76 To mitigate that \, set the [spigot]s only to 1 \, if running Pd with the command line switch -stderr \, that will direct the output to the command line instead of the Pd console.;
#X obj 409 641 text define \$0-example;
#X obj 659 409 text insert \$0-example;
#X text 6 117 Another strategy can be to use [text] for storing data for inspection.;
#X msg 588 77 add name Aphex Twin;
#X msg 614 107 add album drukqs;
#X msg 634 155 add year 2001;
#X connect 0 0 16 0;
#X connect 1 0 34 0;
#X connect 1 1 40 0;
#X connect 1 2 36 0;
#X connect 6 0 0 0;
#X connect 9 0 0 0;
#X connect 11 0 0 0;
#X connect 14 0 0 0;
#X connect 16 0 1 0;
#X connect 16 1 42 0;
#X connect 16 1 47 0;
#X connect 21 0 0 0;
#X connect 22 0 0 0;
#X connect 37 0 1 0;
#X connect 40 0 35 0;
#X connect 41 0 40 1;
#X connect 42 0 20 0;
#X connect 43 0 42 1;
#X connect 49 0 0 0;
#X connect 50 0 0 0;
#X connect 51 0 0 0;