Skip to content

Commit

Permalink
feat: add support for primitives to EJSON.stringify
Browse files Browse the repository at this point in the history
Since EJSON.stringify is often used as a replacement for built-in JSON.stringify, it
would make sense that it would handle the same arguments as JSON.stringify,
including primitive values.
  • Loading branch information
bradvogel authored Aug 5, 2020
1 parent 45bc86d commit 329857d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 1 addition & 4 deletions lib/extended_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ function stringify(value, replacer, space, options) {
}
options = Object.assign({}, { relaxed: true, legacy: false }, options);

const doc = Array.isArray(value)
? serializeArray(value, options)
: serializeDocument(value, options);

const doc = serializeValue(value, options);
return JSON.stringify(doc, replacer, space);
}

Expand Down
10 changes: 10 additions & 0 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ describe('Extended JSON', function() {
expect(serialized).to.equal('{"$binary":{"base64":"AQIDBAU=","subType":"00"}}');
});

it('should correctly serialize strings', function() {
const serialized = EJSON.stringify('new string');
expect(serialized).to.equal('"new string"');
});

it('should correctly serialize numbers', function() {
const serialized = EJSON.stringify(42);
expect(serialized).to.equal('42');
});

it('should correctly parse null values', function() {
expect(EJSON.parse('null')).to.be.null;
expect(EJSON.parse('[null]')[0]).to.be.null;
Expand Down

0 comments on commit 329857d

Please sign in to comment.