Members
(static) Urison
A class, which (re)implements the "Rison" standard of en- and decoding JSON structures to and from URL-safe strings, which can be used as parameter or hash values, while staying readable and avoiding characters, which are not meant to be used inside a URL.
This is a renamed reimplementation of ES5 Rison, which has not gotten an update for years and should be fully compatible with other available parsers for that standard.
The basic idea is this: We have some kind of complex data structure we want to serialize to a URL, to represent a current search and filter setup for example. This structure should also be retrievable easily after a reload, to be able to use that config as a starting point again for the page's search and filter widgets. A big plus here would be readability, which, for instance, gets lost, if we just were to url-encode JSON as-is.
This class provides the means to en- and decode JSON structures for usage in URLs. Additionally, it provides methods to explicitly work with objects and array, for the en- and decoding process, removing the necessity to include brackets into the result, making the string even leaner.
See class documentation below for details.
- Source:
- See:
Example
(new Urison()).encode({key1 : 'value', key2 : true, key3 : [false, 42, null]})
=> '(key1:value,key2:!t,key3:!(!f,42,!n))'
(new Urison()).decode('(key1:value,key2:!t,key3:!(!f,42,!n))')
=> {key1 : 'value', key2 : true, key3 : [false, 42, null]}
(new Urison()).encodeObject({key1 : 'value', key2 : true, key3 : [false, 42, null]})
=> 'key1:value,key2:!t,key3:!(!f,42,!n)'
(new Urison()).decodeObject('key1:value,key2:!t,key3:!(!f,42,!n)')
=> {key1 : 'value', key2 : true, key3 : [false, 42, null]}
(new Urison()).encodeArray([false, 42, null])
=> '!f,42,!n'
(new Urison()).decodeArray('!f,42,!n')
=> [false, 42, null]