[ANN] JSURL a small module to help with JSON in URL parameters.

1,114 views
Skip to first unread message

Bruno Jouhier

unread,
Nov 4, 2011, 7:24:51 PM11/4/11
to nodejs
I find it rather clumsy to pass complex values (things that mix
objects and arrays for ex) via URL query parameters. Splitting into
several parameters quickly becomes hairy and very verbose. JSON + URL
encoding is also pretty verbose and hard to read because of all the
percent escaping. Needless to say JSON + base64 is even harder to
read.

So, I wrote a little module that tries to do things the JSON way but
with the very few characters that URL encoding leaves intact.

https://github.com/Sage/jsurl

Gotcha: this is non standard encoding and I only provided a JS
implementation so don't use it for public service APIs that may be
consumed by clients written in other languages (unless you are ready
to provide support for these languages).

Bruno



alFReD the GODZilla

unread,
Nov 5, 2011, 3:18:32 AM11/5/11
to nodejs
Nice library. How fast is it?
Have you done any benchmarks comparing it with native JSON?

Peter Lejeck

unread,
Nov 5, 2011, 10:42:17 PM11/5/11
to nodejs
What about something like Rison? http://mjtemplate.org/examples/rison.html
URL-safe and represents the same stuff as JSON?

On Nov 5, 12:18 am, alFReD the GODZilla <farid...@googlemail.com>
wrote:

Bruno Jouhier

unread,
Nov 6, 2011, 9:10:49 AM11/6/11
to nodejs
@Alfred,

I wrote a small bench: https://gist.github.com/1342872

Results MacBook Pro + Lion + node 0.6.0:

JSURL.stringify: 1617
JSON.stringify: 152
JSON.stringify + URL encode: 1731
JSURL.parse: 985
JSON.parse: 134
URL decode + JSON.parse: 579

JSON is obviously much faster (not a big surprise).

But JSON + URI encode/decode is a fairer comparison. Here JSURL does a
bit better on stringify but is slower on parse.


@Peter

Thanks for the pointer. I looked at Rison (did not know about it). It
addresses the same problem. It find it a bit complex, though. A few
remarks:

1) Rison must be combined with a percent encoding/decoding pass. JSURL
does not because its output only contains characters that don't need
URL escaping.

2) Rison uses comma and colon. These characters are encoded by
encodeURIComponent. So you only get the advertised compression/
readability if you encode with encodeURI. Seems problematic because
the typical use case is to encode query parameter values and the
recommended method in this case is encodeURIComponent.

3) The idea of sorting keys to normalize and improve caching is
interesting (I hadn't thought about that). But it introduces overhead
and, as most URLs will be generated by programs, there is a good
chance that keys will come in a fixed order most of the time.

Bruno

On Nov 6, 3:42 am, Peter Lejeck <peter.lej...@gmail.com> wrote:
> What about something like Rison?http://mjtemplate.org/examples/rison.html
Reply all
Reply to author
Forward
0 new messages