Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent c3c0fb9 · Browse files · 3 files·+67 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+67 added−5 removed
- tests:+56 added−1 removed
- other:+11 added−4 removed
Provenance
- Commit
ff92afa- Tree
ccef375- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
c3c0fb9
3 files changed+67 added−5 removed
modified:lib/application.js+5 added-1 removed
107 unmodified lines108 109 // default locals110 this.locals.settings = this.settings;111 112- // default production configuration112+ // default configuration113+ this.configure('development', function(){114+ this.set('json spaces', 2);115+ });116+117 this.configure('production', function(){118 this.enable('view cache');119 });120 };409 unmodified linesmodified:lib/response.js+6 added-3 removed
154 unmodified lines155 this.statusCode = obj;156 obj = arguments[1];157 }158 159- var body = JSON.stringify(obj)160- , callback = this.req.query.callback161- , jsonp = this.app.enabled('jsonp callback');159+ var settings = this.app.settings160+ , jsonp = settings['jsonp callback']161+ , replacer = settings['json replacer']162+ , spaces = settings['json spaces']163+ , body = JSON.stringify(obj, replacer, spaces)164+ , callback = this.req.query.callback;165 166 this.charset = this.charset || 'utf-8';167 this.header('Content-Type', 'application/json');168 411 unmodified linesmodified:test/res.json.js+56 added-1 removed
12334567830 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910017 unmodified lines
var express = require('../') , request = require('./support/http'); , request = require('./support/http') , assert = require('assert');
describe('res', function(){ describe('.json(object)', function(){ describe('when given primitives', function(){30 unmodified lines done(); }) }) })
describe('"json replacer" setting', function(){ it('should be passed to JSON.stringify()', function(done){ var app = express();
app.set('json replacer', function(key, val){ return '_' == key[0] ? undefined : val; });
app.use(function(req, res){ res.json({ name: 'tobi', _id: 12345 }); });
request(app) .get('/') .end(function(res){ res.body.should.equal('{"name":"tobi"}'); done(); }); }) })
describe('"json spaces" setting', function(){ it('should default to 2 in development', function(){ process.env.NODE_ENV = 'development'; var app = express(); app.get('json spaces').should.equal(2); process.env.NODE_ENV = 'test'; })
it('should be undefined otherwise', function(){ var app = express(); assert(undefined === app.get('json spaces')); })
it('should be passed to JSON.stringify()', function(done){ var app = express();
app.set('json spaces', 2);
app.use(function(req, res){ res.json({ name: 'tobi', age: 2 }); });
request(app) .get('/') .end(function(res){ res.body.should.equal('{\n "name": "tobi",\n "age": 2\n}'); done(); }); }) }) }) describe('.json(status, object)', function(){ it('should respond with json and set the .statusCode', function(done){17 unmodified lines