Page tree
Skip to end of metadata
Go to start of metadata

This guide explains how to use JSON.stringify() with Smartface controls.

What is JSON.stringify() ?

JSON.stringify() converts a value to JSON notation representing it:

  • Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.
  • Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.
  • If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify can also just return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
  • All symbol-keyed properties will be completely ignored, even when using the replacer function.
  • Non-enumerable properties will be ignored
JSON.stringify({});                  // '{}'
JSON.stringify(true);                // 'true'
JSON.stringify('foo');               // '"foo"'
JSON.stringify([1, 'false', false]); // '[1,"false",false]'
JSON.stringify({ x: 5 });            // '{"x":5}'

JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) 
// '"2006-01-02T15:04:05.000Z"'

JSON.stringify({ x: 5, y: 6 });
// '{"x":5,"y":6}' or '{"y":6,"x":5}'
JSON.stringify([new Number(1), new String('false'), new Boolean(false)]);
// '[1,"false",false]'

// Symbols:
JSON.stringify({ x: undefined, y: Object, z: Symbol('') });
// '{}'
JSON.stringify({ [Symbol('foo')]: 'foo' });
// '{}'
JSON.stringify({ [Symbol.for('foo')]: 'foo' }, [Symbol.for('foo')]);
// '{}'
JSON.stringify({ [Symbol.for('foo')]: 'foo' }, function(k, v) {
  if (typeof k === 'symbol') {
    return 'a symbol';
  }
});
// '{}'

// Non-enumerable properties:
JSON.stringify( Object.create(null, { x: { value: 'x', enumerable: false }, y: { value: 'y', enumerable: true } }) );
// '{"y":"y"}'

How to use with Smartface Controls ?

Smartface Controls have cyclic structure. If you directly use JSON.stringify() with Smartface controls, it will give exception.

For example, open a new workspace and write the code lines below into the page1_btn_onPressed function.

alert(JSON.stringify(lbl));

If you run the app and press the button you will see the error below;

 

So, you can use JSON.stringify() with eliminating parent objects with the code below;

var jst = JSON.stringify(lbl, function(key, value) {
	if (key == "parent")
		return undefined;
	return value;
});
alert(jst);

Here is the screenshot below ;

As you can see above, it lists all the properties of only the given control object.

JSON.prune for Cyclic Structure objects

Cyclic structure objects are common problem for JSON.stringify() . There are some open source libraries to solve that problem. JSON.prune is a open source library which helps listing object properties for cyclic structures.

Download the "JSON.prune.js" from the GitHub repository. Create a lib folder and add this .js file into it. After that, you can use it by including this file into your application.

Sample code and screenshots are shown below;

		include('lib/JSON.prune.js');
		alert(JSON.prune(lbl));

Also, you can find more usage details of JSON.prune library link below;

https://github.com/Canop/JSON.prune/blob/master/README.md