Please turn on JavaScript. To find out how to do this visit the WebWise JavaScript guide.

Serialising and de-serialising data

Methods

decodeJson

Decodes a string JSON representation into an object.

Synopsis

glow.data.decodeJson(string, opts);

Parameters

string
Type
String

The string to be decoded. Must be valid JSON.

opts
Type
Object

Zero or more of the following as properties of an object:

safeMode

Whether the string should only be decoded if it is deemed "safe".

Type
Boolean
Default
false
Optional
Yes

The json.org regular expression checks are used.

Returns

Description

Returns a JavaScript object that mirrors the data given.

Example

var getRef = glow.data.decodeJson('{foo: "Foo", bar: ["Bar 1", "Bar2"]}');
// will return {foo: "Foo", bar: ["Bar 1", "Bar2"]}
      
var getRef = glow.data.decodeJson('foobar', {safeMode: true});
// will throw an error
decodeUrl

Decodes a query string into an object.

Synopsis

glow.data.decodeUrl(string);

Parameters

string
Type
String

The query string to be decoded.

It should not include the initial question mark.

Returns

Description

Returns an object representing the data given by the query string, with all values suitably unescaped. All keys in the query string are keys of the object. Repeated keys result in an array.

Example

var getRef = glow.data.decodeUrl("foo=Foo&bar=Bar%201&bar=Bar2");
// will return the object {foo: "Foo", bar: ["Bar 1", "Bar2"]}
encodeJson

Encodes an object into a string JSON representation.

Synopsis

glow.data.encodeJson(object);

Parameters

object
Type
Object

The object to be encoded.

This can be arbitrarily nested, but must not contain functions or cyclical structures.

Returns

Description

Returns a string representing the object as JSON.

Example

var myObj = {foo: "Foo", bar: ["Bar 1", "Bar2"]};
var getRef = glow.data.encodeJson(myObj);
// will return '{"foo": "Foo", "bar": ["Bar 1", "Bar2"]}'
encodeUrl

Encodes an object for use as a query string.

Synopsis

glow.data.encodeUrl(object);

Parameters

object
Type
Object

The object to be encoded.

This must be a hash whose values can only be primitives or arrays of primitives.

Returns

Description

Returns a string representing the object suitable for use as a query string, with all values suitably escaped. It does not include the initial question mark. Where the input field was an array, the key is repeated in the output.

Example

var getRef = glow.data.encodeUrl({foo: "Foo", bar: ["Bar 1", "Bar2"]});
// will return "foo=Foo&bar=Bar%201&bar=Bar2"
escapeHTML

Escape HTML entities.

Synopsis

glow.data.escapeHTML(string);

Parameters

string
Type
String

The string to be escaped.

Returns

Description

Returns a string with HTML entities escaped.

Example

// useful for protecting against XSS attacks:
var fieldName = '" onclick="alert(\'hacked\')" name="';
      
// but should be used in all cases like this:
glow.dom.create('<input name="' + glow.data.escapeHTML(untrustedString) + '"/>');
Documentation generated by JsDoc Toolkit 2.1.0 on Mon Jul 06 2009 11:46:35 GMT+0100 (BST)

BBC © 2014 The BBC is not responsible for the content of external sites. Read more.

This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.