Skip to content

Commit

Permalink
Issue kof#20: Fix parsing JSON files with BOM characters
Browse files Browse the repository at this point in the history
  • Loading branch information
adivinaelnombre authored and mmukarram committed Jan 13, 2016
1 parent 79f345f commit d82f632
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
*.log
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ exports.load = function load(path, options) {

data = fs.readFileSync(path, 'utf-8');

// replace BOM Character
data = data.replace(/\ufeff/g, '');

if (options.replace) {
data = exports.replace(data, options.replace);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cjson",
"description": "cjson - Commented JavaScript Object Notation. It is a json loader, which parses only valid json files, but with comments enabled. Useful for loading configs.",
"version": "0.3.2",
"version": "0.3.3",
"repository": "git://github.com/kof/node-cjson.git",
"keywords": [
"json",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/conf10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// First character of this file is BOM
"test": "valid JSON, except for the the hidden BOM character"
}
5 changes: 4 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var data = {
conf5: {"'key/*test*/'": "'value//test'"},
conf6: {"key\"/*test*/": "value\"//test"},
conf7: {"key": "{{root}}/src"},
conf8: {}
conf8: {},
conf10: {"test":"valid JSON, except for the the hidden BOM character"},
};

a.doesNotThrow(function() {
Expand All @@ -38,6 +39,8 @@ a.deepEqual(cjson.load(fixtures + '/conf7.json', {replace: {root: '/usr'}}), {"k

a.deepEqual(cjson.load(fixtures + '/conf8.json'), data.conf8, 'string-like comment');

a.deepEqual(cjson.load(fixtures + '/conf10.json'), data.conf10, 'BOM character');

var data1 = {
conf1: {key: 'value'},
conf6: data.conf6
Expand Down

0 comments on commit d82f632

Please sign in to comment.