mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
Add Bun.JSONC API for parsing JSON with comments and trailing commas (#22115)
## Summary This PR implements a new `Bun.JSONC.parse()` API that allows parsing JSONC (JSON with Comments) files. It addresses the feature request from issue #16257 by providing a native API for parsing JSON with comments and trailing commas. The implementation follows the same pattern as `Bun.YAML` and `Bun.TOML`, leveraging the existing `TSConfigParser` which already handles JSONC parsing internally. ## Features - **Parse JSON with comments**: Supports both `//` single-line and `/* */` block comments - **Handle trailing commas**: Works with trailing commas in objects and arrays - **Full JavaScript object conversion**: Returns native JavaScript objects/arrays - **Error handling**: Proper error throwing for invalid JSON - **TypeScript compatibility**: Works with TypeScript config files and other JSONC formats ## Usage Example ```javascript const result = Bun.JSONC.parse(`{ // This is a comment "name": "my-app", "version": "1.0.0", // trailing comma is allowed "dependencies": { "react": "^18.0.0", }, }`); // Returns native JavaScript object ``` ## Implementation Details - Created `JSONCObject.zig` following the same pattern as `YAMLObject.zig` and `TOMLObject.zig` - Uses the existing `TSConfigParser` from `json.zig` which already handles comments and trailing commas - Added proper C++ bindings and exports following Bun's established patterns - Comprehensive test suite covering various JSONC features ## Test Plan - [x] Basic JSON parsing works - [x] Single-line comments (`//`) are handled correctly - [x] Block comments (`/* */`) are handled correctly - [x] Trailing commas in objects and arrays work - [x] Complex nested structures parse correctly - [x] Error handling for invalid JSON - [x] Empty objects and arrays work - [x] Boolean and null values work correctly 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
27
packages/bun-types/bun.d.ts
vendored
27
packages/bun-types/bun.d.ts
vendored
@@ -625,6 +625,33 @@ declare module "bun" {
|
||||
export function parse(input: string): object;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONC related APIs
|
||||
*/
|
||||
namespace JSONC {
|
||||
/**
|
||||
* Parse a JSONC (JSON with Comments) string into a JavaScript value.
|
||||
*
|
||||
* Supports both single-line (`//`) and block comments (`/* ... *\/`), as well as
|
||||
* trailing commas in objects and arrays.
|
||||
*
|
||||
* @category Utilities
|
||||
*
|
||||
* @param input The JSONC string to parse
|
||||
* @returns A JavaScript value
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* const result = Bun.JSONC.parse(`{
|
||||
* // This is a comment
|
||||
* "name": "my-app",
|
||||
* "version": "1.0.0", // trailing comma is allowed
|
||||
* }`);
|
||||
* ```
|
||||
*/
|
||||
export function parse(input: string): unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* YAML related APIs
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user