Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/encode/json.js
1154 views
1
//
2
// Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
/**
8
* Json code from Brantlye Harris-- http://code.google.com/p/jquery-json/
9
* @namespace beef.encode.json
10
*/
11
12
beef.encode.json = {
13
/**
14
* @memberof beef.encode.json
15
* @param o
16
*/
17
stringify: function(o) {
18
if (typeof(JSON) == 'object' && JSON.stringify) {
19
// Error on stringifying cylcic structures caused polling to die
20
try {
21
s = JSON.stringify(o);
22
} catch(error) {
23
// TODO log error / handle cyclic structures?
24
}
25
return s;
26
}
27
var type = typeof(o);
28
29
if (o === null)
30
return "null";
31
32
if (type == "undefined")
33
return '\"\"';
34
35
if (type == "number" || type == "boolean")
36
return o + "";
37
38
if (type == "string")
39
return $j.quoteString(o);
40
41
if (type == 'object')
42
{
43
if (typeof o.toJSON == "function")
44
return $j.toJSON( o.toJSON() );
45
46
if (o.constructor === Date)
47
{
48
var month = o.getUTCMonth() + 1;
49
if (month < 10) month = '0' + month;
50
51
var day = o.getUTCDate();
52
if (day < 10) day = '0' + day;
53
54
var year = o.getUTCFullYear();
55
56
var hours = o.getUTCHours();
57
if (hours < 10) hours = '0' + hours;
58
59
var minutes = o.getUTCMinutes();
60
if (minutes < 10) minutes = '0' + minutes;
61
62
var seconds = o.getUTCSeconds();
63
if (seconds < 10) seconds = '0' + seconds;
64
65
var milli = o.getUTCMilliseconds();
66
if (milli < 100) milli = '0' + milli;
67
if (milli < 10) milli = '0' + milli;
68
69
return '"' + year + '-' + month + '-' + day + 'T' +
70
hours + ':' + minutes + ':' + seconds +
71
'.' + milli + 'Z"';
72
}
73
74
if (o.constructor === Array)
75
{
76
var ret = [];
77
for (var i = 0; i < o.length; i++)
78
ret.push( $j.toJSON(o[i]) || "null" );
79
80
return "[" + ret.join(",") + "]";
81
}
82
83
var pairs = [];
84
for (var k in o) {
85
var name;
86
var type = typeof k;
87
88
if (type == "number")
89
name = '"' + k + '"';
90
else if (type == "string")
91
name = $j.quoteString(k);
92
else
93
continue; //skip non-string or number keys
94
95
if (typeof o[k] == "function")
96
continue; //skip pairs where the value is a function.
97
98
var val = $j.toJSON(o[k]);
99
100
pairs.push(name + ":" + val);
101
}
102
103
return "{" + pairs.join(", ") + "}";
104
}
105
},
106
/**
107
* @memberof beef.encode.json
108
* @param string
109
*/
110
quoteString: function(string) {
111
if (string.match(this._escapeable))
112
{
113
return '"' + string.replace(this._escapeable, function (a)
114
{
115
var c = this._meta[a];
116
if (typeof c === 'string') return c;
117
c = a.charCodeAt();
118
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
119
}) + '"';
120
}
121
return '"' + string + '"';
122
},
123
124
_escapeable: /["\\\x00-\x1f\x7f-\x9f]/g,
125
126
_meta : {
127
'\b': '\\b',
128
'\t': '\\t',
129
'\n': '\\n',
130
'\f': '\\f',
131
'\r': '\\r',
132
'"' : '\\"',
133
'\\': '\\\\'
134
}
135
};
136
137
$j.toJSON = function(o) {return beef.encode.json.stringify(o);};
138
$j.quoteString = function(o) {return beef.encode.json.quoteString(o);};
139
140
beef.regCmp('beef.encode.json');
141
142