aboutsummaryrefslogtreecommitdiff
path: root/site/udo/json.udo
blob: b0bdcd1233e16105426b6e221a0cf8bcf89f6bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#ifndef UDO_JSON
#define UDO_JSON ##
/*
	JSON formatting and parsing
	
	This file is part of the SONICS UDO collection by Richard Knight 2022
		License: GPL-2.0-or-later
		http://1bpm.net

*/

#include "/string_tools.udo"
#include "/array_tools.udo"


/*
	Create a new empty JSON string

	Sjson json_init

	Sout	empty JSON object/string
*/
opcode json_init, S, 0
	xout "{}"
endop


/*
	Append raw string to JSON string

	Sout json_append Sjson, StoAppend

	Sout		string with appendage
	Sjson		input string to append to
	StoAppend	string to append
*/
opcode json_append, S, SS
	Sjson, StoAppend xin
	ilen strlen Sjson

	Sjson = (ilen < 2) ? "{" : strsub(Sjson, 0, ilen-1)
	Sout = sprintf("%s%s%s}", Sjson, (strlen(Sjson) == 1) ? "" : ",", StoAppend)

	xout Sout
endop


/*
	Append JSON object string to JSON object string with key

	Sout json_appendobject Sjson, Skey, Svalue

	Sout		string with appendage
	Sjson		input string to append to
	Skey		key for appending
	Svalue		value for appending
*/
opcode json_appendobject, S, SSS
	Sjson, Skey, Svalue xin
	Sout = json_append(Sjson, sprintf("\"%s\":%s", Skey, Svalue))
	xout Sout
endop


/*
	Append string to JSON object string with key

	Sout json_appendstring Sjson, Skey, Svalue

	Sout		string with quoted appendage
	Sjson		input string to append to
	Skey		key for appending
	Svalue		value for appending
*/
opcode json_appendstring, S, SSS
	Sjson, Skey, Svalue xin
	Sout = json_append(Sjson, sprintf("\"%s\":\"%s\"", Skey, Svalue))
	xout Sout
endop


/*
	Append numeric value to JSON object string with key

	Sout json_appendvalue Sjson, Skey, ivalue

	Sout		string with appendage
	Sjson		input string to append to
	Skey		key for appending
	ivalue		value for appending
*/
opcode json_appendvalue, S, SSi
	Sjson, Skey, ivalue xin
	Sformat = (frac(ivalue) == 0) ? "\"%s\":%d" : "\"%s\":%f"
	Sout = json_append(Sjson, sprintf(Sformat, Skey, ivalue))
	xout Sout
endop


/*
	Append numeric array to JSON object string with key

	Sout json_appendarray Sjson, Skey, iarray[]

	Sout		string with appendage
	Sjson		input string to append to
	Skey		key for appending
	iarray[]	array for appending
*/
opcode json_appendarray, S, SSi[]o
	Sjson, Skey, iarray[] xin
	Sformatted = ""
	ilen = lenarray(iarray)
	index = 0
	while (index < ilen) do
		ivalue = iarray[index]
		Sformat = (frac(ivalue) == 0) ? "%d" : "%f"
		Sformatted strcat Sformatted, sprintf(Sformat, ivalue)
		if (index != ilen - 1) then
			Sformatted strcat Sformatted, ","
		endif
		index += 1
	od
	Sout = json_appendobject(Sjson, Skey, sprintf("[%s]", Sformatted))
	xout Sout
endop



/*
	Append string array to JSON object string with key

	Sout json_appendarray Sjson, Skey, Sarray[]

	Sout		string with appendage
	Sjson		input string to append to
	Skey		key for appending
	Sarray[]	array for appending
	iomitempty	leave out empty strings
*/
opcode json_appendarray, S, SSS[]o
	Sjson, Skey, Sarray[], iomitempty xin
	Sformatted = ""
	ilen = lenarray(Sarray)
	index = 0
	while (index < ilen) do
		isempty = (strcmp(Sarray[index], "") == 0) ? 1 : 0
		if ((iomitempty == 0 && isempty == 1) || isempty == 0) then
			Sformatted strcat Sformatted, sprintf("\"%s\",", Sarray[index])
		endif
		index += 1
	od
	
	Sout = json_appendobject(Sjson, Skey, sprintf("[%s]", strsub(Sformatted, 0, strlen(Sformatted) - 1)))
	xout Sout
endop


/*
	Append f-table to JSON object string with key

	Sout json_appendtable Sjson, Skey, ifn

	Sout		string with appendage
	Sjson		input string to append to
	Skey		key for appending
	ifn			f-table number
*/
opcode json_appendtable, S, SSi
	Sjson, Skey, ifn xin
	Sformatted = ""
	ilen = ftlen(ifn)
	index = 0
	while (index < ilen) do
		ivalue = table:i(index, ifn)
		Sformat = (frac(ivalue) == 0) ? "%d" : "%f"
		Sformatted strcat Sformatted, sprintf(Sformat, ivalue)
		if (index != ilen - 1) then
			Sformatted strcat Sformatted, ","
		endif
		index += 1
	od
	Sout = json_appendobject(Sjson, Skey, sprintf("[%s]", Sformatted))
	xout Sout
endop




/*
	Obtain an object, array or value from a json object given a string key

	itype, Stringvalue, inumericvalue json_parse Sjson, Skey

	itype			type of returned value: 0=string, 1=object, 2=array, 3=numeric (1 and 2 are returned as strings for further parsing)
	Stringvalue		the requested value as a string
	inumericvalue	the numeric value if itype is 3 , otherwise -1

	Sjson			the json to parse
	Skey			the key to lookup
*/
opcode json_parse, iSi, SS
	Sjson, Skey xin
	itype = -1
	index = 0
	idepth = 0
	idepthrequest = -1
	iobjectstart = -1
	inval = 0
	instring = 0
	inrequested = 0
	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)
		if (strcmp(Schar, "[") == 0 && instring == 0)  then
			idepth += 1
			if (inrequested == 1 && iobjectstart == -1) then
				iobjectstart = index
			endif

		elseif (strcmp(Schar, "]") == 0 && instring == 0) then
			idepth -= 1
			if (idepthrequest == idepth) then
				itype = 2
				goto complete
			endif

		elseif (strcmp(Schar, "{") == 0 && instring == 0) then
			idepth += 1
			if (inrequested == 1 && iobjectstart == -1) then
				iobjectstart = index
			endif

		elseif (strcmp(Schar, "}") == 0 && instring == 0) then
			idepth -= 1
			if (idepthrequest == idepth) then
				itype = 1
				goto complete
			endif

		elseif (strcmp(Schar, ":") == 0 && instring == 0) then
			inval = 1
			iobjectstart = index + 1
		elseif (strcmp(Schar, ",") == 0 && instring == 0) then
			if (inval == 1) then
				inval = 0
				if (inrequested == 1  && idepthrequest == idepth) then
					index -= 1
					itype = 3
					goto complete
				endif
			endif
		elseif (strcmp(Schar, "\"") == 0) then
			instring = 1 - instring
			if (instring == 1) then
				istringstart = index + 1
			else
				String = strsub(Sjson, istringstart, index)
				if (inrequested == 1 && idepthrequest == idepth) then
					iobjectstart = istringstart
					index -= 1
					itype = 0
					goto complete
				elseif (strcmp(String, Skey) == 0) then
					idepthrequest = idepth
					inrequested = 1
				endif
			endif
		endif
		index += 1
	od

complete:

	Stringvalue = strsub(Sjson, iobjectstart, index+1)  ; +1 required?
	if (itype == 3) then
		ivalid, inumericvalue try_strtod strstrip(Stringvalue)
		itype = (ivalid == 1) ? 3 : 0
	endif
	

	xout itype, Stringvalue, inumericvalue
endop


; itype: 0=S, 1=i
opcode _json_getarray, S[]i[], Si
	Sjson, itype xin
	index = 0
	instring = 0
	iobjectstart = -1
	iobjectcount = 0
	iwriteindex = 0

	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)
		if (strcmp(Schar, ",") == 0 && instring == 0) then
			iobjectcount += 1
		elseif (strcmp(Schar, "\"") == 0) then
			instring = 1 - instring
		endif
		index += 1
	od

	if (itype == 0) then
		Soutput[] init iobjectcount + 1
		ioutput[] init 1
	else
		Soutput[] init 1
		ioutput[] init iobjectcount + 1
	endif
	index = 0
	instring = 0
	
	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)
		if (strcmp(Schar, ",") == 0 && instring == 0) then			
			if (itype == 1) then
				ioutput[iwriteindex] = strtod(strsub(Sjson, iobjectstart, index))
				iobjectstart = index + 1
			endif
			iwriteindex += 1
		elseif (strcmp(Schar, "[") == 0 && instring == 0) then
			iobjectstart = index + 1
		elseif (strcmp(Schar, "]") == 0 && instring == 0) then
			if (itype == 1) then
				ioutput[iwriteindex] = strtod(strsub(Sjson, iobjectstart, index))
			endif
		elseif (strcmp(Schar, "\"") == 0) then
			if (instring == 0) then
				iobjectstart = index + 1
			elseif (itype == 0) then
				Svalue = strsub(Sjson, iobjectstart, index)
				Soutput[iwriteindex] = Svalue
			endif
			instring = 1 - instring
		endif
		index += 1
	od
	xout Soutput, ioutput
endop

opcode json_getstringarray, S[], S
	Sjson xin
	Soutput[], i_[] _json_getarray Sjson, 0
	xout Soutput
endop

opcode json_getnumericarray, i[], S
	Sjson xin
	S_[], ioutput[] _json_getarray Sjson, 1
	xout ioutput
endop

/*
	Obtain an object, array or value from a json array given an index

	itype, Stringvalue, inumericvalue json_parsearray Sjson, irequestindex

	itype			type of returned value: 0=string, 1=object, 2=array, 3=numeric (1 and 2 are returned as strings for further parsing)
	Stringvalue		the requested value as a string
	inumericvalue	the numeric value if itype is 3 , otherwise -1

	Sjson			the json to parse
	irequestindex	index in the array to obtain
*/
opcode json_parsearray, iSi, Si
	Sjson, irequestindex xin
	itype = -1
	inmainarray = 0
	index = 0
	iobjectstart = 0
	iscanindex = 0
	instring = 0
	idepth = 0
	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)

		if (strcmp(Schar, "[") == 0 && instring == 0) then
			if (inmainarray == 0) then
				inmainarray = 1
				iobjectstart = index + 1
			else
				idepth += 1 
			endif

		elseif (strcmp(Schar, "]") == 0 && instring == 0) then
			if (inmainarray == 1 && idepth == 0) then
				itype = 0
				goto complete
			else
				idepth -= 1
				itype = 2
			endif

		elseif (strcmp(Schar, "{") == 0 && inmainarray == 1 && instring == 0) then
			idepth += 1

		elseif (strcmp(Schar, "}") == 0 && inmainarray == 1 && instring == 0) then
			idepth -= 1
			itype = 1
	
		elseif (strcmp(Schar, ",") == 0 && idepth == 0 && inmainarray == 1 && instring == 0) then
			if (iscanindex == irequestindex) then
				itype = 0
				goto complete
			endif
			iobjectstart = index + 1
			iscanindex += 1

		elseif (strcmp(Schar, "\"") == 0 && idepth == 0 && inmainarray == 1 && iscanindex == irequestindex) then
			instring = 1 - instring
			if (instring == 1) then
				iobjectstart = index + 1
			else
				itype = 0
				goto complete
			endif
		endif

		index += 1
	od

complete:

	Stringvalue = strsub(Sjson, iobjectstart, index)
	if (itype == 0) then
		ivalid, inumericvalue try_strtod strstrip(Stringvalue)
		itype = (ivalid == 1) ? 3 : 0
	endif
	

	xout itype, Stringvalue, inumericvalue
endop



/*
	Get the length of a json array

	ilength json_arraylength Sjson

	ilength		length determined
	Sjson		input array
*/
opcode json_arraylength, i, S
	Sjson xin
	idepth = -1
	idepthmax = -1
	ihasitems = 0
	instring = 0
	index = 0
	itemnum = 0

	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)
		if (strcmp(Schar, "[") == 0 && instring == 0)  then
			idepth += 1
			idepthmax += 1

		elseif (strcmp(Schar, "]") == 0 && instring == 0) then
			idepth -= 1
			if (idepth < 0) then
				goto complete
			endif

		elseif (strcmp(Schar, "{") == 0 && instring == 0 && idepth >= 0) then
			idepth += 1
			idepthmax += 1

		elseif (strcmp(Schar, "}") == 0 && instring == 0 && idepth >= 0) then
			idepth -= 1

		elseif (strcmp(Schar, ",") == 0 && instring == 0 && idepth == 0) then
			itemnum += 1

		elseif (strcmp(Schar, "\"") == 0) then
			if (idepth == 0) then
				ihasitems = 1
			endif
			instring = 1 - instring
		endif
		index += 1
	od

complete:
		if (idepthmax != 0 || ihasitems == 1) then
			itemnum += 1
		endif
		xout itemnum

endop




/*
	Get the keys from an object

	Skeys[] json_getkeys Sjson

	Skeys[]		the keys found
	Sjson		input json
*/
opcode json_getkeys, S[], S
	Sjson xin
	Stemp[] init 999

	itempindex init 0
	idepth = -1
	instring = 0
	index = 0
	inmainobject = 0
	iskey = 1

	while (index < strlen(Sjson)) do
		Schar = strsub(Sjson, index, index + 1)
		if (strcmp(Schar, "[") == 0 && instring == 0 && idepth >= 0)  then
			idepth += 1

		elseif (strcmp(Schar, "]") == 0 && instring == 0 && idepth >= 0) then
			idepth -= 1

		elseif (strcmp(Schar, "{") == 0 && instring == 0) then
			idepth += 1

		elseif (strcmp(Schar, "}") == 0 && instring == 0) then
			idepth -= 1
			if (idepth < 0) then
				goto complete
			endif

		elseif (strcmp(Schar, ":") == 0 && instring == 0 && idepth == 0) then
			iskey = 0

		elseif (strcmp(Schar, ",") == 0 && instring == 0 && idepth == 0) then
			iskey = 1

		elseif (strcmp(Schar, "\"") == 0 && idepth == 0) then
			instring = 1 - instring
			if (instring == 1) then
				istringstart = index + 1
			elseif (iskey == 1) then
				Stemp[itempindex] = strsub(Sjson, istringstart, index)
				itempindex += 1
			endif
		endif
		index += 1
	od

complete:
	Skeys[] init itempindex ;+ 1
	index = 0
	while (index < itempindex) do
		Skeys[index] = Stemp[index]
		index += 1
	od

	xout Skeys
endop


#end