diff --git a/package.json b/package.json index 6456939..17ce7a8 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "mitata": "^1.0.34", "prettier": "^3.5.2", "smol-toml": "^1.3.1", + "strip-json-comments": "^5.0.1", "toml": "^3.0.0", "typescript": "^5.8.2", "unbuild": "^3.5.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c1a2a30..22ede2f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ importers: smol-toml: specifier: ^1.3.1 version: 1.3.1 + strip-json-comments: + specifier: ^5.0.1 + version: 5.0.1 toml: specifier: ^3.0.0 version: 3.0.0 @@ -2302,6 +2305,10 @@ packages: resolution: {integrity: sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==} engines: {node: '>=8'} + strip-json-comments@5.0.1: + resolution: {integrity: sha512-0fk9zBqO67Nq5M/m45qHCJxylV/DhBlIOVExqgOMiCCrzrhU6tCibRXNqE3jwJLftzE9SNuZtYbpzcO+i9FiKw==} + engines: {node: '>=14.16'} + stylehacks@7.0.4: resolution: {integrity: sha512-i4zfNrGMt9SB4xRK9L83rlsFCgdGANfeDAYacO1pkqcE7cRHPdWHwnKZVz7WY17Veq/FvyYsRAU++Ga+qDFIww==} engines: {node: ^18.12.0 || ^20.9.0 || >=22.0} @@ -4611,6 +4618,8 @@ snapshots: strip-json-comments@3.1.1: {} + strip-json-comments@5.0.1: {} + stylehacks@7.0.4(postcss@8.5.3): dependencies: browserslist: 4.24.4 diff --git a/src/index.ts b/src/index.ts index add28cb..4cc2b03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ export { parseJSON5, stringifyJSON5 } from "./json5"; export type { JSON5ParseOptions, JSON5StringifyOptions } from "./json5"; export { parseJSONC, stringifyJSONC } from "./jsonc"; -export type { JSONCParseError, JSONCParseOptions } from "./jsonc"; +export type { JSONCParseOptions } from "./jsonc"; export { parseYAML, stringifyYAML } from "./yaml"; export type { YAMLParseOptions, YAMLStringifyOptions } from "./yaml"; diff --git a/src/jsonc.ts b/src/jsonc.ts index e240d64..1843606 100644 --- a/src/jsonc.ts +++ b/src/jsonc.ts @@ -1,5 +1,5 @@ import { storeFormat, type FormatOptions } from "./_format"; -import { parse } from "jsonc-parser"; +import stripeJSONComments from "strip-json-comments"; import { stringifyJSON } from "./json"; // Source: https://github.com/microsoft/node-jsonc-parser @@ -21,7 +21,11 @@ export function parseJSONC( text: string, options?: JSONCParseOptions, ): T { - const obj = parse(text, options?.errors, options); + const obj = JSON.parse( + stripeJSONComments(text, { + trailingCommas: options?.allowTrailingComma, + }), + ); storeFormat(text, obj, options); return obj as T; } @@ -45,16 +49,7 @@ export function stringifyJSONC( // --- Types --- export interface JSONCParseOptions extends FormatOptions { - disallowComments?: boolean; allowTrailingComma?: boolean; - allowEmptyContent?: boolean; - errors?: JSONCParseError[]; } export interface JSONCStringifyOptions extends FormatOptions {} - -export interface JSONCParseError { - error: number; - offset: number; - length: number; -} diff --git a/test/bench.mjs b/test/bench.mjs index 3e3bcc3..2649118 100644 --- a/test/bench.mjs +++ b/test/bench.mjs @@ -7,6 +7,7 @@ import jsYaml from "js-yaml"; import yaml from "yaml"; import * as json5 from "json5"; import * as jsoncParser from "jsonc-parser"; +import stripeJSONComments from "strip-json-comments"; import * as confbox from "../dist/index.mjs"; import * as fixtures from "./fixtures.mjs"; @@ -64,6 +65,9 @@ defineBench("jsonc", { "microsoft/node-jsonc-parser": () => { jsoncParser.parse(fixtures.jsonc); }, + "strip-json-comments/parseJSONC": () => { + JSON.parse(stripeJSONComments(fixtures.jsonc)); + }, }); defineBench("json", {