Skip to content

Commit d46e95d

Browse files
committed
Use separate 'createJSON' method that can be overriden
1 parent 2910bc2 commit d46e95d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/response.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ res.send = function send(body) {
193193
return this;
194194
};
195195

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+
196215
/**
197216
* Send JSON response.
198217
*
@@ -221,11 +240,8 @@ res.json = function json(obj) {
221240
}
222241
}
223242

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);
229245

230246
// content-type
231247
if (!this.get('Content-Type')) {
@@ -264,14 +280,8 @@ res.jsonp = function jsonp(obj) {
264280
}
265281

266282
// 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')];
275285

276286
// content-type
277287
if (!this.get('Content-Type')) {

0 commit comments

Comments
 (0)