-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZZISJSON.xml
58 lines (57 loc) · 2.55 KB
/
ZZISJSON.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Routine name="ZZISJSON" type="INC" timestamp="65581,67786.904074"><![CDATA[
ZZISJSON(jsn) ; check JSON string
new ok,er
try {do Do^ZPretty(.jsn) set ok=1}
catch er {set ok=0 }
if ok ;set $TEST
quit ok
]]></Routine>
<Routine name="ZPretty" type="MAC" languagemode="0" timestamp="65578,59596.661274"><![CDATA[
ZPretty ; make JSON readable
#; input= JSON string | stream | %DynamicAbstractObject
#; indent filler ; "" => no indent
#define fill " "
#; New line code ; "" => no Lines
#define crlf $c(13,10)
Do(input,fil,nl) public {
S fil=$g(fil,$$$fill),nl=$g(nl,$$$crlf), sc=1,res="",indt=""
I $iso(input),input.%IsA("%Library.DynamicAbstractObject") S obj=input
E S obj=##class(%Library.DynamicAbstractObject).%FromJSON(input)
S stc=0,ito=obj.%GetIterator(),sep=""
,jtyp=$s(obj.%IsA("%Library.DynamicObject"):"object",1:"array")
,ist(stc)=ito, cst(stc)=obj, tst(stc)=jtyp
I $i(stc)
S res=res_$s(jtyp="object":"{",1:"["), indt=indt_fil
F { S isito=ito.%IsA("%Iterator.Object")
I ito.%GetNext(.key,.val) {
S jtyp=$CASE(obj.%GetTypeCodeOf(key),0:"null"
,1:"boolean",2:"boolean",3:"number",4:"number"
,6:"array",7:"object",8:"string",9:"number"
,10:"illegalvalue"
,11:"number",12:"oref",13:"unassigned"
,14:"number",15:"number",16:"string"
,18:"number",19:"number",20:"string"
,31:"unassigned",:"UNKNOWN")
S res=res_sep_nl_indt, sep="," }
Else {
S indt=$e(indt,1,*-$l(nl)), res=res_nl_indt, res=res_$s(isito:"}",1:"]")
Q:'$i(stc,-1)
S ito=ist(stc), obj=cst(stc), jtyp=tst(stc), sep=","
Continue }
I isito S res=res_""""_key_""":"
I (jtyp="object") || (jtyp="array") {
S ist(stc)=ito, cst(stc)=obj, tst(stc)=jtyp, sep=""
I $i(stc)
S ito=val.%GetIterator(),obj=val, indt=indt_fil
,res=res_$s(jtyp="object":"{",1:"[")
} ElseIf jtyp="string" { S res=res_""""_$zcvt(val,"O","JSON")_""""
} ElseIf jtyp="boolean" { S res=res_$s(val:"true",1:"false")
} ElseIf jtyp="null" { S res=res_"null"
} Else { I $e(val)="." S val="0"_val
I $e(val,1,2)="-." S val="-0."_$e(val,3,*)
S res=res_val } }
Q res }
]]></Routine>
</Export>