Skip to content
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

fix improper character escaping #21

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion lib/serialize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var util = require('util');
var json2 = require('JSON2');

module.exports = serialize;

Expand All @@ -24,7 +25,7 @@ function serialize(obj) {
// Creates a JSON string representation of the object and uses placeholders
// for functions and regexps (identified by index) which are later
// replaced.
str = JSON.stringify(obj, function (key, value) {
str = json2.stringify(obj, function (key, value) {
if (typeof value === 'function') {
return '@__FUNCTION_' + (functions.push(value) - 1) + '__@';
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"peerDependencies": {
"express": "3.x"
},
"dependencies": {
"JSON2": "0.1.0"
},
"devDependencies": {
"chai": "*",
"express": "3.x",
Expand Down
5 changes: 5 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ describe('serialize( obj )', function () {
it('should deserialize a JSON string to a JSON object', function () {
expect(JSON.parse(serialize(data))).to.deep.equal(data);
});

it('should serialize weird whitespace characters correctly', function () {
var ws = String.fromCharCode(8232);
expect(eval(serialize(ws))).to.equal(ws);
});
});

describe('functions', function () {
Expand Down