-
-
Save iemejia/94f0d26be3ee1060f6c9 to your computer and use it in GitHub Desktop.
Pretty print a json skipping the BOM if present
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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