forked from ConfirmitASA/TextAnalyticsTemplate_CodeLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.js
More file actions
71 lines (68 loc) · 2.22 KB
/
JSON.js
File metadata and controls
71 lines (68 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @class JSON
* @classdesc Class with functions Helping working with reportal objects like JSON objects
*/
class JSON
{
/**
* @memberof JSON
* @function stringify
* @description implement JSON.stringify serialization
* @param {Object} obj
* @returns {String}
*/
static function stringify(obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+ _escapeEntities(obj) +'"';
else if(t=="number") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string"){
v = '"'+ _escapeEntities(v) +'"';
}
else if (t == "object" && v !== null) v = stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
/**
* @memberof JSON
* @private
* @function _escapeEntities
* @description function to replace some chars
* @param {String} str
* @returns {String}
*/
private static function _escapeEntities(str) {
var entitiesMap = {
'<': '<',
'>': '>',
'&': '&',
'\"': '\\"',
'\'':'&apos;'
};
return str.replace(/[&<>\"\']/g, function(key) {
return entitiesMap[key];
});
}
/**
* @memberof JSON
* @function print
* @description function to create js variable from reportal object
* @param {Object} config - reportal object
* @param {String} configName - name for the js variable ('config' by degault)
* @returns {String}
*/
static function print(config, configName){ // JSON.print prints JSON `config` to page as JavaScript variable with a specified `configName`
var varName = configName || 'config';
return '<script type="text/javascript">var '+ varName + '=' + stringify(config) +'</script>';
}
}