Skip to content

Commit

Permalink
support serialize undefined (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
nqdy666 authored and okuryu committed Aug 29, 2019
1 parent c812a45 commit 356987f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ serialize({
The above will produce the following string output:

```js
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,date:new Date("2016-04-28T22:02:17.156Z"),new Map([["hello", "world"]]),new Set([123,456]),"fn":function echo(arg) { return arg; },"re":/([^\\s]+)/g}'
'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"undef":undefined,"date":new Date("2016-04-28T22:02:17.000Z"),"map":new Map([["hello","world"]]),"set":new Set([123,456]),"fn":function echo(arg) { return arg; },"re":/([^\s]+)/g}'
```

Note: to produced a beautified string, you can pass an optional second argument to `serialize()` to define the number of spaces to be used for the indentation.
Expand Down
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ See the accompanying LICENSE file for terms.

// Generate an internal UID to make the regexp pattern harder to guess.
var UID = Math.floor(Math.random() * 0x10000000000).toString(16);
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S)-' + UID + '-(\\d+)__@"', 'g');
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U)-' + UID + '-(\\d+)__@"', 'g');

var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
var IS_PURE_FUNCTION = /function.*?\(/;
Expand Down Expand Up @@ -44,11 +44,13 @@ module.exports = function serialize(obj, options) {
var dates = [];
var maps = [];
var sets = [];
var undefs = [];

// Returns placeholders for functions and regexps (identified by index)
// which are later replaced by their string representation.
function replacer(key, value) {
if (!value) {

if (!value && value !== undefined) {
return value;
}

Expand Down Expand Up @@ -79,6 +81,10 @@ module.exports = function serialize(obj, options) {
return '@__F-' + UID + '-' + (functions.push(origValue) - 1) + '__@';
}

if (type === 'undefined') {
return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@';
}

return value;
}

Expand Down Expand Up @@ -119,6 +125,12 @@ module.exports = function serialize(obj, options) {
return serializedFn;
}

// Protects against `JSON.stringify()` returning `undefined`, by serializing
// to the literal string: "undefined".
if (obj === undefined) {
return String(obj);
}

var str;

// Creates a JSON string representation of the value.
Expand All @@ -142,7 +154,7 @@ module.exports = function serialize(obj, options) {
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
}

if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0) {
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0) {
return str;
}

Expand All @@ -166,6 +178,10 @@ module.exports = function serialize(obj, options) {
return "new Set(" + serialize(Array.from(sets[valueIndex].values()), options) + ")";
}

if (type === 'U') {
return 'undefined'
}

var fn = functions[valueIndex];

return serializeFunc(fn);
Expand Down
7 changes: 7 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ describe('serialize( obj )', function () {
var ws = String.fromCharCode(8232);
expect(eval(serialize(ws))).to.equal(ws);
});

it('should serialize undefined correctly', function () {
var obj;
var str = '{"undef":undefined,"nest":{"undef":undefined}}';
eval('obj = ' + str);
expect(serialize(obj)).to.equal(str);
});
});

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

0 comments on commit 356987f

Please sign in to comment.