new Urison(autoEscapeopt)
Creates a new Urison en- and decoder.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
autoEscape |
Boolean |
<optional> |
true | if true, all keys and values are automatically uri-encoded and decoded if necessary, set this to false to keep values as is |
Methods
decode(risonString) → {Object|Array|String|Number|Boolean|null}
Decodes a Rison string to a JSON value.
Parameters:
Name | Type | Description |
---|---|---|
risonString |
String | the Rison string to decode |
Throws:
error if decoding fails
Returns:
- Type
- Object | Array | String | Number | Boolean | null
Example
(new Urison()).decode('(key1:value,key2:!t,key3:!(!f,42,!n))')
=> {key1 : 'value', key2 : true, key3 : [false, 42, null]}
decodeArray(risonString) → {Array}
Decodes a shortened Rison array string to a JSON array.
Parameters:
Name | Type | Description |
---|---|---|
risonString |
String | the Rison array string to decode |
Throws:
error if decoding fails
Returns:
- Type
- Array
Example
(new Urison()).decodeArray('!f,42,!n')
=> [false, 42, null]
decodeObject(risonString) → {Object}
Decodes a shortened Rison object string to a JSON object.
Parameters:
Name | Type | Description |
---|---|---|
risonString |
String | the Rison object string to decode |
Throws:
error if decoding fails
Returns:
- Type
- Object
Example
(new Urison()).decodeObject('key1:value,key2:!t,key3:!(!f,42,!n)')
=> {key1 : 'value', key2 : true, key3 : [false, 42, null]}
encode(value) → {String|undefined}
Encodes a JSON value to a Rison string.
Parameters:
Name | Type | Description |
---|---|---|
value |
Array | Object | String | Number | Boolean | null | the value to encode |
Throws:
error if encoding fails or value is not usable JSON
Returns:
- Type
- String | undefined
Example
(new Urison()).encode({key1 : 'value', key2 : true, key3 : [false, 42, null]})
=> '(key1:value,key2:!t,key3:!(!f,42,!n))'
encodeArray(value) → {String|undefined}
Encodes a JSON array to a Rison string.
Parameters:
Name | Type | Description |
---|---|---|
value |
Array | the array to encode |
Throws:
error if value is not an array
Returns:
- Type
- String | undefined
Example
(new Urison()).encodeArray([false, 42, null])
=> '!f,42,!n'
encodeObject(value) → {String|undefined}
Encodes a JSON value to a Rison string.
Parameters:
Name | Type | Description |
---|---|---|
value |
Object | the object to encode |
Throws:
error if value is not an object
Returns:
- Type
- String | undefined
Example
(new Urison()).encodeObject({key1 : 'value', key2 : true, key3 : [false, 42, null]})
=> 'key1:value,key2:!t,key3:!(!f,42,!n)'
escape(value) → {String}
URI-Escapes a value, if necessary, according to the rules of Rison, which is a little bit more lax than native uri encoding (allows [,:@$/+]).
This method has one difference to the reference implementation:
We do not encode whitespace as "+", but as "%20". This is done, because "+"-encoding is not
compatible with decodeURIComponent
and makes working with URL-encoded values manually painful.
So here, "+" is just a normal, allowed URL-safe character and whitespace becomes "%20".
Since encode_uri
was never automatically applied in Rison, this should not break anything.
Parameters:
Name | Type | Description |
---|---|---|
value |
String | the value to escape problematic chars in |
Returns:
- Type
- String
Example
(new Urison()).escape('abc,:@')
=> 'abc%2C%3A%40'