@@ -193,6 +193,25 @@ res.send = function send(body) {
193
193
return this ;
194
194
} ;
195
195
196
+ /**
197
+ * Create JSON response.
198
+ *
199
+ * Examples:
200
+ *
201
+ * res.json(null);
202
+ * res.json({ user: 'tj' });
203
+ *
204
+ * @param {string|number|boolean|object } obj
205
+ * @api public
206
+ */
207
+ res . createJSON = function createJSON ( obj ) {
208
+ var app = this . app ;
209
+ var replacer = app . get ( 'json replacer' ) ;
210
+ var spaces = app . get ( 'json spaces' ) ;
211
+ var body = JSON . stringify ( obj , replacer , spaces ) ;
212
+ return body ;
213
+ } ;
214
+
196
215
/**
197
216
* Send JSON response.
198
217
*
@@ -221,11 +240,8 @@ res.json = function json(obj) {
221
240
}
222
241
}
223
242
224
- // settings
225
- var app = this . app ;
226
- var replacer = typeof res . jsonReplacer !== 'undefined' ? res . jsonReplacer : app . get ( 'json replacer' ) ;
227
- var spaces = app . get ( 'json spaces' ) ;
228
- var body = JSON . stringify ( val , replacer , spaces ) ;
243
+ // json
244
+ var body = this . createJSON ( val ) ;
229
245
230
246
// content-type
231
247
if ( ! this . get ( 'Content-Type' ) ) {
@@ -264,14 +280,8 @@ res.jsonp = function jsonp(obj) {
264
280
}
265
281
266
282
// settings
267
- var app = this . app ;
268
- var replacer = res . get ( 'json replacer' ) ;
269
- if ( typeof replacer === 'undefined' ) {
270
- replacer = app . get ( 'json replacer' ) ;
271
- }
272
- var spaces = app . get ( 'json spaces' ) ;
273
- var body = JSON . stringify ( val , replacer , spaces ) ;
274
- var callback = this . req . query [ app . get ( 'jsonp callback name' ) ] ;
283
+ var body = this . createJSON ( val ) ;
284
+ var callback = this . req . query [ this . app . get ( 'jsonp callback name' ) ] ;
275
285
276
286
// content-type
277
287
if ( ! this . get ( 'Content-Type' ) ) {
0 commit comments