Skip to content

Commit

Permalink
Add json.k to support older versions of q
Browse files Browse the repository at this point in the history
If running on an old version of q, load json.k from kx to provide JSON decoding
support. With this change, reQ should be fully compatible with older versions
of kdb+; tested working with 2.8 & 3.3.

.Q.hg was only added in 3.4 - reQ will now work on prior versions that may
still be in use in some places.
  • Loading branch information
jonathonmcmurray committed May 17, 2018
1 parent 1323435 commit 01ed1d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions json.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/downloaded from https://raw.githubusercontent.com/KxSystems/kdb/master/e/json.k

\d .j

/[]{} Cbg*xhijefcspmdznuvt
q:"\"";s:{q,x,q};J:(($`0`1)!$`false`true;s;{$[#x;x;"null"]};s;{s@[x;&"."=8#x;:;"-"]};s)1 2 5 11 12 16h bin
j:{$[10=abs t:@x;s@,/{$[x in r:"\t\n\r\"\\";"\\","tnr\"\\"r?x;x]}'x;99=t;"{",(","/:(j'!x),'":",'j'. x),"}";-1<t;"[",($[98=t;",\n ";","]/:.Q.fc[j']x),"]";J[-t]@$x]}

/enclose
e:{(*x),(","/:y),*|x};a:"\t\n\r\"\\";f:{$[x in a;"\\","tnr\"\\"a?x;x]}
j:{$[10=abs t:@x;s$[|/x in a;,/f'x;x];99=t;e["{}"](j'!x),'":",'j'. x;-1<t;e["[]"].Q.fc[j']x;J[-t]@$x]}

/disclose
v:{=\~("\\"=-1_q,x)<q=x};d:{$[1<n:(s:+\v[x]*1 -1 1 -1"{}[]"?x)?0;1_'(0,&(v[x]&","=x)&1=n#s)_x:n#x;()]}
c:{$["{"=*x;(`$c'n#'x)!c'(1+n:x?'":")_'x:d x;"["=*x;.Q.fc[c']d x;q=*x;$[1<+/v x;'`err;"",. x];"a">*x;"F"$x;"n"=*x;0n;"t"=*x]}
k:{c x@&~v[x]&x in" \t\n\r"};

\

k j x:([]C:$`as`;b:01b;j:0N 2;z:0Nz,.z.z)
k j x:"\"a \\"
k"{},2]"


4 changes: 3 additions & 1 deletion req.q
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
\d .req

if[.z.K<=3.1;@[system;"l json.k";{-2"Failed to load json.k: ",x}]]; //add JSON support for older q versions

cookiejar:()!() //storage for cookies

sturl:{(":"=first x)_x:$[-11=type x;string;]x} //convert URL to string
Expand Down Expand Up @@ -112,7 +114,7 @@ send:{[m;u;hd;p] /m-method,u-url,hd-headers,p-payload
parseresp:{[r]
/* detect JSON reponse & parse into KDB data structure */
/ TODO - add handling for other data types? /
:$[r[0][`$"Content-Type"]like .h.ty[`json],"*";.j.k;] r[1]; //check for JSON, parse if so
:$[(`j in key`)&r[0][`$"Content-Type"]like .h.ty[`json],"*";.j.k;] r[1]; //check for JSON, parse if so
}

.req.get:{parseresp okstatus send[`GET;x;y;()]} //get - projection with no payload & GET method
Expand Down

0 comments on commit 01ed1d6

Please sign in to comment.