Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
17 changes: 6 additions & 11 deletions src/jsonc.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +21,11 @@ export function parseJSONC<T = unknown>(
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;
}
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions test/bench.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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", {
Expand Down
Loading