Skip to content

Commit 329857d

Browse files
authored
feat: add support for primitives to EJSON.stringify
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.
1 parent 45bc86d commit 329857d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/extended_json.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ function stringify(value, replacer, space, options) {
187187
}
188188
options = Object.assign({}, { relaxed: true, legacy: false }, options);
189189

190-
const doc = Array.isArray(value)
191-
? serializeArray(value, options)
192-
: serializeDocument(value, options);
193-
190+
const doc = serializeValue(value, options);
194191
return JSON.stringify(doc, replacer, space);
195192
}
196193

test/node/extended_json_tests.js

+10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ describe('Extended JSON', function() {
159159
expect(serialized).to.equal('{"$binary":{"base64":"AQIDBAU=","subType":"00"}}');
160160
});
161161

162+
it('should correctly serialize strings', function() {
163+
const serialized = EJSON.stringify('new string');
164+
expect(serialized).to.equal('"new string"');
165+
});
166+
167+
it('should correctly serialize numbers', function() {
168+
const serialized = EJSON.stringify(42);
169+
expect(serialized).to.equal('42');
170+
});
171+
162172
it('should correctly parse null values', function() {
163173
expect(EJSON.parse('null')).to.be.null;
164174
expect(EJSON.parse('[null]')[0]).to.be.null;

0 commit comments

Comments
 (0)