-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_encode.js
200 lines (184 loc) · 3.64 KB
/
json_encode.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const JSON_HEX_QUOT = 8;
const JSON_HEX_TAG = 1;
const JSON_HEX_AMP = 2;
const JSON_HEX_APOS = 4;
const JSON_NUMERIC_CHECK = 32;
const JSON_UNESCAPED_SLASHES = 64;
const JSON_UNESCAPED_UNICODE = 256;
const JSON_FORCE_OBJECT = 16;
function json_encode(val,options = 0){
var gettype=Object.prototype.toString;
switch( gettype.call(val) ){
case '[object String]':
return json_escape_string(val,options);
break;
case '[object Number]':
if(val % 1 === 0){
return val;
}else{
return isFinite(val)?val:0;
}
break;
case '[object Boolean]':
return val?true:false;
break;
case '[object Undefined]':
return null;
break;
case '[object Null]':
return null;
break;
case '[object Object]':
return json_encode_array(val,options);
break;
case '[object Array]':
return json_encode_array(val,options);
break;
case '[object Function]':
return null;
break;
default:
return null;
break;
}
}
function json_escape_string(val,options){
var pos = 0;
var len = val.length;
if(len == 0){
return '""';
}
if(options & JSON_NUMERIC_CHECK){
if (!isNaN(parseInt(val))) {
if(val % 1 === 0){
return val;
}else{
return isFinite(val)?val:0;
}
}
}
if(len == null){
return null;
}else if(len == 0){
return '""';
}
var result = '"';
while(pos < len){
us = val.charAt(pos);
switch(us){
case '"':
if(options & JSON_HEX_QUOT){
result += "\\u0022";
}else{
result += "\\\"";
}
break;
case '\\':
result += "\\\\";
break;
case '/':
if (options & JSON_UNESCAPED_SLASHES ) {
result += us;
} else {
result += "\\/";
}
break;
case '\b':
result += "\\b";
break;
case '\f':
result += "\\f";
break;
case '\n':
result += "\\n";
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '<':
if (options & JSON_HEX_TAG) {
result += "\\u003C";
} else {
result += '<';
}
break;
case '>':
if (options & JSON_HEX_TAG) {
result += "\\u003E";
} else {
result += '>';
}
break;
case '&':
if (options & JSON_HEX_AMP) {
result += "\\u0026";
} else {
result += '&';
}
break;
case '\'':
if (options & JSON_HEX_APOS) {
result += "\\u0027";
} else {
result += '\'';
}
break;
default:
if ((us >= ' ' && (us & 127) == us) || IsDigit(us) || IsAlpha(us) || (options & JSON_UNESCAPED_UNICODE)) {
result += us;
}else{
result += "\\u"+parseInt(us.charCodeAt(0),10).toString(16);
}
break;
}
pos++;
}
result += '"';
return result;
}
function IsDigit(cCheck) { return (('0'<=cCheck) && (cCheck<='9')); }
function IsAlpha(cCheck) { return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) }
function is_index_array(val,options){
if(options & JSON_FORCE_OBJECT){
return false;
}
var index = 0;
for(var item in val){
if(item == index){
index++;
}else{
return false;
}
}
return true;
}
function json_encode_array(val,options){
if(!is_index_array(val,options)){
var result = '{';
}else{
var result = '[';
}
if(!is_index_array(val,options)){
for(var item in val){
result += json_encode(item,options)+':'+json_encode(val[item],options);
result += ',';
}
}else{
for(var item in val){
result += json_encode(val[item]);
result += ',';
}
}
if(result.length > 1){
result = result.substring(0,result.length-1)
}
if(!is_index_array(val,options)){
result += '}';
}else{
result += ']';
}
return result;
}