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

add 'objectProto' option to create parsed objects as {} #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var json_parse = function (options) {
var _options = {
strict: false, // not being strict means do not generate syntax errors for "duplicate key"
storeAsString: false, // toggles whether the values should be stored as BigNumber (default) or a string
objectProto: false, // toggles whether to parse objects as Objects (consistent with JSON.parse), or as null-prototype objects
alwaysParseAsBig: false, // toggles whether all numbers should be Big
useNativeBigInt: false, // toggles whether to use native BigInt instead of bignumber.js
protoAction: 'error',
Expand All @@ -98,6 +99,9 @@ var json_parse = function (options) {
if (options.storeAsString === true) {
_options.storeAsString = true;
}
if (options.objectProto === true) {
_options.objectProto = true;
}
_options.alwaysParseAsBig =
options.alwaysParseAsBig === true ? options.alwaysParseAsBig : false;
_options.useNativeBigInt =
Expand Down Expand Up @@ -328,7 +332,7 @@ var json_parse = function (options) {
// Parse an object value.

var key,
object = Object.create(null);
object = _options.objectProto ? {} : Object.create(null);

if (ch === '{') {
next('{');
Expand Down
22 changes: 22 additions & 0 deletions test/object-proto-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const makeJSON = require('../index.js');
const expect = require('chai').expect;

describe('objectProto option', function(){
it("should parse objects as Object instances if 'objectProto' set to true", function(done){
const JSONbigObjectProto = makeJSON({ objectProto: true });
var json_with_object = '{ "foo": { "bar": "baz" }}';
var result = JSONbigObjectProto.parse(json_with_object);
expect(result instanceof Object).to.be.true;
expect(result['foo'] instanceof Object).to.be.true;
done();
});

it("should parse objects as null-prototype objects if 'objectProto' set to false", function(done){
const JSONbigNullProto = makeJSON({ objectProto: false });
var json_with_object = '{ "foo": { "bar": "baz" }}';
var result = JSONbigNullProto.parse(json_with_object);
expect(result.__proto__).to.be.undefined;
expect(result['foo'].__proto__).to.be.undefined;
done();
});
});