Skip to content

Add 'json stringifier' option #4454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ res.json = function json(obj) {
var escape = app.get('json escape')
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = stringify(val, replacer, spaces, escape)
var stringifier = app.get('json stringifier');
var body = stringify(val, replacer, spaces, escape, stringifier)

// content-type
if (!this.get('Content-Type')) {
Expand Down Expand Up @@ -300,7 +301,8 @@ res.jsonp = function jsonp(obj) {
var escape = app.get('json escape')
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = stringify(val, replacer, spaces, escape)
var stringifier = app.get('json stringifier');
var body = stringify(val, replacer, spaces, escape, stringifier)
var callback = this.req.query[app.get('jsonp callback name')];

// content-type
Expand Down Expand Up @@ -837,8 +839,10 @@ res.cookie = function (name, value, options) {
throw new Error('cookieParser("secret") required for signed cookies');
}

var app = this.app;
var stringifier = app.get('json stringifier');
var val = typeof value === 'object'
? 'j:' + JSON.stringify(value)
? 'j:' + stringify(value, undefined, undefined, undefined, stringifier)
: String(value);

if (signed) {
Expand Down Expand Up @@ -1111,16 +1115,19 @@ function sendfile(res, file, options, callback) {
* @param {function} replaces
* @param {number} spaces
* @param {boolean} escape
* @param {function} stringifier
* @returns {string}
* @private
*/

function stringify (value, replacer, spaces, escape) {
function stringify (value, replacer, spaces, escape, stringifier) {
var jsonStringify = stringifier || JSON.stringify

// v8 checks arguments.length for optimizing simple call
// https://bugs.chromium.org/p/v8/issues/detail?id=4730
var json = replacer || spaces
? JSON.stringify(value, replacer, spaces)
: JSON.stringify(value);
? jsonStringify(value, replacer, spaces)
: jsonStringify(value);

if (escape) {
json = json.replace(/[<>&]/g, function (c) {
Expand Down
18 changes: 18 additions & 0 deletions test/res.cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ describe('res', function(){
.expect('Set-Cookie', 'user=j%3A%7B%22name%22%3A%22tobi%22%7D; Path=/')
.expect(200, done)
})

it('should generate a JSON cookie with a custom JSON stringifier', function(done){
var app = express();

app.set('json stringifier', function(value) {
value.name = 'asdf'
return JSON.stringify(value)
});

app.use(function(req, res){
res.cookie('user', { name: 'tobi' }).end();
});

request(app)
.get('/')
.expect('Set-Cookie', 'user=j%3A%7B%22name%22%3A%22asdf%22%7D; Path=/')
.expect(200, done)
})
})

describe('.cookie(name, string)', function(){
Expand Down
31 changes: 31 additions & 0 deletions test/res.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,37 @@ describe('res', function(){
.expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)
})
})

describe('"json stringifier" setting', function(){
it('should be undefined by default', function(){
var app = express();
assert(undefined === app.get('json stringifier'));
})

it('should be used instead of JSON.stringify', function(done){
var app = express();

app.set('json replacer', function(key, val){
return key[0] === '_'
? undefined
: val;
});
app.set('json spaces', 2);
app.set('json stringifier', function(value, replacer, space) {
value.name = 'asdf'
return JSON.stringify(value, replacer, space);
});

app.use(function(req, res){
res.json({ name: 'tobi', age: 2, _id: 1234 });
});

request(app)
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(200, '{\n "name": "asdf",\n "age": 2\n}', done)
})
})
})

describe('.json(status, object)', function(){
Expand Down
31 changes: 31 additions & 0 deletions test/res.jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,37 @@ describe('res', function(){
.expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)
})
})

describe('"json stringifier" setting', function(){
it('should be undefined by default', function(){
var app = express();
assert(undefined === app.get('json stringifier'));
})

it('should be used instead of JSON.stringify', function(done){
var app = express();

app.set('json replacer', function(key, val){
return key[0] === '_'
? undefined
: val;
});
app.set('json spaces', 2);
app.set('json stringifier', function(value, replacer, space) {
value.name = 'asdf'
return JSON.stringify(value, replacer, space);
});

app.use(function(req, res){
res.jsonp({ name: 'tobi', age: 2, _id: 1234 });
});

request(app)
.get('/')
.expect('Content-Type', 'application/json; charset=utf-8')
.expect(200, '{\n "name": "asdf",\n "age": 2\n}', done)
})
})
})

describe('.jsonp(status, object)', function(){
Expand Down