Last active
October 9, 2020 16:12
-
-
Save meleu/82d8fc7499fa89da0d8b9f35370ff4a5 to your computer and use it in GitHub Desktop.
write and parse json files
This file contains hidden or 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
function writeJsonFile(path, obj) { | |
if (!(typeof obj === 'object' && obj !== null)) { | |
return false; | |
} | |
try { | |
fs.writeFileSync(path, JSON.stringify(obj, null, 2)); | |
} catch (err) { | |
console.error(`---[ERRO - ${new Date()}]\n${err}`); | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Retorna o conteúdo do arquivo JSON já transformado em um objeto. | |
* @param {String} path | |
*/ | |
function parseJsonFile(path) { | |
try { | |
return JSON.parse(fs.readFileSync(path, 'utf8')); | |
} catch (err) { | |
console.error(`---[ERRO - ${new Date()}]\n${err}`); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment