Skip to content

Commit 1879481

Browse files
committed
Revert "Revert "support serialize undefined (#54)""
This reverts commit 4dfd9e5.
1 parent 9ee6b1c commit 1879481

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ serialize({
5252
The above will produce the following string output:
5353

5454
```js
55-
'{"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}'
55+
'{"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}'
5656
```
5757

5858
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.

Diff for: index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ See the accompanying LICENSE file for terms.
88

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

1313
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1414
var IS_PURE_FUNCTION = /function.*?\(/;
@@ -44,11 +44,13 @@ module.exports = function serialize(obj, options) {
4444
var dates = [];
4545
var maps = [];
4646
var sets = [];
47+
var undefs = [];
4748

4849
// Returns placeholders for functions and regexps (identified by index)
4950
// which are later replaced by their string representation.
5051
function replacer(key, value) {
51-
if (!value) {
52+
53+
if (!value && value !== undefined) {
5254
return value;
5355
}
5456

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

84+
if (type === 'undefined') {
85+
return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@';
86+
}
87+
8288
return value;
8389
}
8490

@@ -119,6 +125,12 @@ module.exports = function serialize(obj, options) {
119125
return serializedFn;
120126
}
121127

128+
// Protects against `JSON.stringify()` returning `undefined`, by serializing
129+
// to the literal string: "undefined".
130+
if (obj === undefined) {
131+
return String(obj);
132+
}
133+
122134
var str;
123135

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

145-
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0) {
157+
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0) {
146158
return str;
147159
}
148160

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

181+
if (type === 'U') {
182+
return 'undefined'
183+
}
184+
169185
var fn = functions[valueIndex];
170186

171187
return serializeFunc(fn);

Diff for: test/unit/serialize.js

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ describe('serialize( obj )', function () {
5757
var ws = String.fromCharCode(8232);
5858
expect(eval(serialize(ws))).to.equal(ws);
5959
});
60+
61+
it('should serialize undefined correctly', function () {
62+
var obj;
63+
var str = '{"undef":undefined,"nest":{"undef":undefined}}';
64+
eval('obj = ' + str);
65+
expect(serialize(obj)).to.equal(str);
66+
});
6067
});
6168

6269
describe('functions', function () {

0 commit comments

Comments
 (0)