From 1317313c7f4e28b1b42c82e091c14dcda025cc50 Mon Sep 17 00:00:00 2001 From: "Hyunsoo, Chung" Date: Sun, 17 Oct 2021 03:11:40 +0900 Subject: [PATCH] fix json parsing --- package-lock.json | 36 ++++++++++++++++++++++++++++++------ package.json | 3 +++ utils.js | 22 ++++++++++++++-------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 54dcfd5..c12cac4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,16 @@ { "name": "level-file-checker", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.0.0", + "name": "level-file-checker", + "version": "1.0.1", "license": "AGPL-3.0-or-later", + "dependencies": { + "json5": "^2.2.0" + }, "devDependencies": { "pkg": "^5.1.0" } @@ -796,6 +800,20 @@ "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", "dev": true }, + "node_modules/json5": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "dependencies": { + "minimist": "^1.2.5" + }, + "bin": { + "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/jsonfile": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", @@ -876,8 +894,7 @@ "node_modules/minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "node_modules/mkdirp-classic": { "version": "0.5.3", @@ -2278,6 +2295,14 @@ "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=", "dev": true }, + "json5": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "requires": { + "minimist": "^1.2.5" + } + }, "jsonfile": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", @@ -2338,8 +2363,7 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "mkdirp-classic": { "version": "0.5.3", diff --git a/package.json b/package.json index 04f9aca..7fd187d 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "homepage": "https://github.com/wjdgustn/level-file-checker#readme", "devDependencies": { "pkg": "^5.1.0" + }, + "dependencies": { + "json5": "^2.2.0" } } diff --git a/utils.js b/utils.js index c895dd9..63e4ca2 100644 --- a/utils.js +++ b/utils.js @@ -1,14 +1,20 @@ +const JSON5 = require('json5'); + String.prototype.replaceAll = function(org, dest) { return this.split(org).join(dest); } module.exports.ADOFAIParser = level => { - return JSON.parse(String(level).trim() - .replaceAll(', ,', ',') - .replaceAll('}\n', '},\n') - .replaceAll('},\n\t]', '}\n\t]') - .replaceAll(', },', ' },') - .replaceAll(', }', ' }') - .replaceAll('\n', '') - .replaceAll('}\n', '},\n')); + try { + return JSON5.parse(String(level).trim()); + } catch (e) { + return JSON5.parse(String(level).trim() + .replaceAll(', ,', ',') + .replaceAll('}\n', '},\n') + .replaceAll('},\n\t]', '}\n\t]') + .replaceAll(', },', ' },') + .replaceAll(', }', ' }') + .replaceAll('\n', '') + .replaceAll('}\n', '},\n')); + } } \ No newline at end of file