Skip to content

Instantly share code, notes, and snippets.

@iemejia
Forked from rgamez/pp_json_bom.js
Last active October 19, 2015 19:24
Show Gist options
  • Save iemejia/94f0d26be3ee1060f6c9 to your computer and use it in GitHub Desktop.
Save iemejia/94f0d26be3ee1060f6c9 to your computer and use it in GitHub Desktop.
Pretty print a json skipping the BOM if present
#!/usr/bin/env node
var fs = require('fs');
var prettyPrint = function(json) {
return JSON.stringify(JSON.parse(json), null, ' ');
};
var args = process.argv.slice(2);
if (args.length == 0 || args.length > 1) {
console.log("Invalid args number: pp_json_bon FILE");
process.exit(1);
}
filename = args[0];
fs.readFile(filename, function(error, buffer) {
if (error) {
console.error(error);
return;
}
var bom = new Buffer(3);
buffer.copy(bom, 0);
if (bom.equals(new Buffer([0xef, 0xbb, 0xbf]))) {
console.log(prettyPrint(buffer.slice(3)));
}
else {
console.log(prettyPrint(buffer));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment