json.k 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. T:1_4\0xe74717
  2. ws:~^"\t\n\r "?
  3. / make sure that all the brackets match
  4. chkb:{$[(,2)~?(2!!#:)#-':,/x@@[y;]#'=z;;`err@"mismatched brackets"]}
  5. / make sure that keys to dictionaries are strings
  6. chkds:{$[~&/1,"\""=x@-1+&":"=x;`err@"keys are strings";]} / ")]} emacs :(
  7. / pairs have objects as parents
  8. / children of objects are pairs
  9. chko:{$[(~&/1,"{"=x@y@&":"=x)&&/1,":"=x@y@(~:)_&"{"=x@y;`err@"malformed object";]}
  10. pr:{(g;p):0 2\<((2*#x)#!2)+{2}#x;f:&~p;(g@(|\p*!#p)@f)@<g@f};dp:{+/~~,/(x@)\,!#x}
  11. prn:{p:pr@z@y;@[p;;:;].+.{$[~#c:&":"=x[y]@z;(();())
  12. ~1~#?3!c;`err@"malformed object"
  13. z@(c+\:1 2;c)]}[x;y]'=p}
  14. trm:{x:"[",x;(-/1(ws@x@)\)/y}
  15. tree:{ $[~#x:trm[x;#x]#x;:(!0;!0);]
  16. m:{$[~^x?3;`err@"stray escape";x>0]}@0{T[x;y]}\2^"\\\""?x
  17. $[~#s:(+/1(ws@x@)\)/&|':m<|/w:"{[]},:"=\:x;:(0;(,0)!trm[x;#x]);]
  18. d:(-|/2#w)++\+/1 1 -1 -1*w:4#w>\:m
  19. chkb[x;|/w;d]
  20. chkds[x s]
  21. end:!/|+1_,':s,#x
  22. s:@[s;|w;:;s@w:-1 0+\:&":"=x@s]
  23. s@:&~|/",]}"=\:x@s
  24. chko[x s;p:prn[x;s;d]]
  25. (p;trm[x]'s#end) }
  26. hx:16/+/1(-39*~:10>)\-48+_: / to hex
  27. u8:{`c$(0x00c0e0f0[c],c#128)+(0,64+&c:1+128 2048 65536'x)\x} /utf8 encode
  28. L:("true";"false";"null");E:`c$"\"\\/bfnrt"!(34 92 47 8 12 10 13 9)
  29. de:{,/{i:&<\"\\"=s:" ",x;s:@[s;ii@j;:;w@j:&~^w:E@s@ii:1+i];,/1_'(0,i)_s}'{$["\\u"~2#x;(u8@hx@2_6#x),6_x;x]}'_[;x]0,@/1(&"u"=x@1+)\&<\"\\"=x}
  30. tkn:{x@+/(!--)\(!y;.y)@\:z}
  31. leaf:{$["\""~*t:tkn[x;y;z];de@1_-1_t;~&/^(`c$"-","0"+!10)?*t;._t;~^L?t;`$t;t]} / "]} emacs :(
  32. prs:{t:tree[x];{$[~^":["?t:x[!y[1]]@z; o[x;y]'((y[0][z])^z)^0N
  33. "{"~t; !/+o[x;y]'((y[0][z])^z)^0N
  34. leaf[x;y[1];z]]}[x;(=t[0];t[1]);0]}
  35. dmp:{$[~^`c`C?t:@x;`k@x;~~/1_:\t;(","/o'x)/"[]";`m~t;(","/":"/'+o''(!x;.x))/"{}";~^`i`f?t;$x;|/(`$L)~\:x;$x]}