aboutsummaryrefslogtreecommitdiff
path: root/src/modules/json.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/json.js')
-rw-r--r--src/modules/json.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/modules/json.js b/src/modules/json.js
new file mode 100644
index 0000000..5c099b1
--- /dev/null
+++ b/src/modules/json.js
@@ -0,0 +1,35 @@
+const fs = require("fs");
+const repair = require("jsonrepair");
+
+function read(file) {
+ let json = false;
+
+ // make sure the file actually exists
+ if (! fs.existsSync(file)) {
+ return false;
+ }
+
+ // make sure we're actually reading a file
+ if (! fs.statSync(file).isFile()) {
+ return false;
+ }
+
+ // read the file
+ let file_content = fs.readFileSync(file, "utf8");
+
+ // attempt to parse it
+ try {
+ json = JSON.parse(file_content);
+ }catch(err) {
+ // attempt to repair then parse
+ try {
+ json = JSON.parse(repair(file_content));
+ }catch(repair_err) {
+ return false;
+ }
+ }
+
+ return json;
+}
+
+module.exports = read;