Skip to content

Commit

Permalink
Replace json-fixer with jsonrepair (#1052)
Browse files Browse the repository at this point in the history
The `fixJson` package is stale and `jsonrepair` is an active
alternative. Seams to work well.
  • Loading branch information
chompDev authored Jan 9, 2025
2 parents ae7fddb + 4b04b5d commit 7c3b10d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions project/src/utils/JsonUtil.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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<T>(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;
Expand Down

0 comments on commit 7c3b10d

Please sign in to comment.