From 56c13e9d3f689595f07cb3eaae5bb123f0bf8c65 Mon Sep 17 00:00:00 2001 From: Refringe Date: Thu, 9 Jan 2025 09:58:31 -0500 Subject: [PATCH] Replace json-fixer with jsonrepair for improved JSON validation --- project/package.json | 2 +- project/src/utils/JsonUtil.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/project/package.json b/project/package.json index 78650772..30148481 100644 --- a/project/package.json +++ b/project/package.json @@ -41,9 +41,9 @@ "date-fns-tz": "~3.1", "fs-extra": "11.2.0", "i18n": "~0.15", - "json-fixer": "~1.6", "json5": "~2.2", "jsonc": "~2.0", + "jsonrepair": "3.11.2", "logform": "~2.6", "mongoid-js": "~1.3", "reflect-metadata": "~0.2", diff --git a/project/src/utils/JsonUtil.ts b/project/src/utils/JsonUtil.ts index a50f1b24..ad71a7fc 100644 --- a/project/src/utils/JsonUtil.ts +++ b/project/src/utils/JsonUtil.ts @@ -1,10 +1,10 @@ import type { ILogger } from "@spt/models/spt/utils/ILogger"; import { FileSystemSync } from "@spt/utils/FileSystemSync"; import { HashUtil } from "@spt/utils/HashUtil"; -import fixJson from "json-fixer"; import { parse, stringify } from "json5"; import { jsonc } from "jsonc"; import { IParseOptions, IStringifyOptions, Reviver } from "jsonc/lib/interfaces"; +import { jsonrepair } from "jsonrepair"; import { inject, injectable } from "tsyringe"; @injectable() @@ -153,10 +153,13 @@ export class JsonUtil { let savedHash = this.fileHashes[filePath]; if (!savedHash || savedHash !== generatedHash) { try { - const { data, changed } = fixJson(jsonString); + const fixedJsonString = jsonrepair(jsonString); + const data = this.deserialize(fixedJsonString); + const changed = jsonString !== fixedJsonString; + if (changed) { // data invalid, return it - this.logger.error(`${filePath} - Detected faulty json, please fix your json file using VSCodium`); + this.logger.error(`${filePath} - Detected faulty JSON, please fix using a validator`); } else { // data valid, save hash and call function again this.fileHashes[filePath] = generatedHash;