Skip to content
Merged
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
42 changes: 11 additions & 31 deletions Cargo.lock

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

22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ customize this behaviour by passing options:
```ts
import { parse, parseToValue } from "@david/jsonc-morph";

// Disable specific extensions
// disable specific extensions
const root = parse(text, {
allowComments: false, // Reject // and /* */ comments
allowTrailingCommas: false, // Reject trailing commas in arrays/objects
allowSingleQuotedStrings: false, // Reject 'single quoted' strings
allowHexadecimalNumbers: false, // Reject 0xFF style numbers
allowUnaryPlusNumbers: false, // Reject +42 style numbers
allowMissingCommas: false, // Reject missing commas between elements
allowLooseObjectPropertyNames: false, // Reject unquoted property names
allowComments: false, // reject // and /* */ comments
allowTrailingCommas: false, // reject trailing commas in arrays/objects
allowSingleQuotedStrings: false, // reject 'single quoted' strings
allowHexadecimalNumbers: false, // reject 0xFF style numbers
allowUnaryPlusNumbers: false, // reject +42 style numbers
allowMissingCommas: false, // reject missing commas between elements
allowLooseObjectPropertyNames: false, // reject unquoted property names
});

// parseToValue accepts the same options
Expand All @@ -96,12 +96,12 @@ For strict JSON parsing (only allow JSON), use `parseStrict` or
```ts
import { parseStrict, parseToValueStrict } from "@david/jsonc-morph";

// All extensions disabled by default
// all extensions disabled by default
const root = parseStrict('{"name": "test"}');

// Selectively enable specific extensions
// selectively enable specific extensions
const rootWithComments = parseStrict(text, { allowComments: true });

// Same for parseToValueStrict
// same for `parseToValueStrict`
const value = parseToValueStrict('{"name": "test"}');
```
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"Cargo.lock",
"Cargo.toml",
"deno.lock",
"target",
"rust-toolchain.toml",
"**/*_test.ts"
]
},
"tasks": {
"build:npm": "deno run -A scripts/build_npm.ts",
"build": "deno run -A jsr:@deno/wasmbuild@0.19.3 --inline && deno run -A scripts/fix_types.ts"
"build": "deno run -A jsr:@deno/wasmbuild@0.21.0 --inline && deno run -A scripts/fix_types.ts"
},
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.42.3",
Expand Down
30 changes: 10 additions & 20 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
ObjectProp,
ObjectPropName,
parse,
type ParseOptions,
parseToValue,
RootNode,
StringLit,
Expand All @@ -18,29 +19,18 @@ export {
import {
type JsonValue,
parse,
type ParseOptions,
parseToValue,
type RootNode,
} from "./lib/rs_lib.js";

/** Options for strict JSON parsing (all JSONC extensions disabled by default). */
export interface ParseStrictOptions {
/** Allow comments (defaults to `false` in strict mode). */
allowComments?: boolean;
/** Allow trailing commas (defaults to `false` in strict mode). */
allowTrailingCommas?: boolean;
/** Allow loose object property names (defaults to `false` in strict mode). */
allowLooseObjectPropertyNames?: boolean;
/** Allow missing commas (defaults to `false` in strict mode). */
allowMissingCommas?: boolean;
/** Allow single-quoted strings (defaults to `false` in strict mode). */
allowSingleQuotedStrings?: boolean;
/** Allow hexadecimal numbers (defaults to `false` in strict mode). */
allowHexadecimalNumbers?: boolean;
/** Allow unary plus on numbers (defaults to `false` in strict mode). */
allowUnaryPlusNumbers?: boolean;
}
/**
* Options for strict JSON parsing (all JSONC extensions disabled by default).
* @deprecated Use `ParseOptions` instead - this is an alias for backwards compatibility.
*/
export type ParseStrictOptions = ParseOptions;

const STRICT_DEFAULTS: Required<ParseStrictOptions> = {
const STRICT_DEFAULTS: Required<ParseOptions> = {
allowComments: false,
allowTrailingCommas: false,
allowLooseObjectPropertyNames: false,
Expand All @@ -60,7 +50,7 @@ const STRICT_DEFAULTS: Required<ParseStrictOptions> = {
*/
export function parseStrict(
text: string,
options?: ParseStrictOptions,
options?: ParseOptions,
): RootNode {
return parse(text, { ...STRICT_DEFAULTS, ...options });
}
Expand All @@ -76,7 +66,7 @@ export function parseStrict(
*/
export function parseToValueStrict(
text: string,
options?: ParseStrictOptions,
options?: ParseOptions,
): JsonValue {
return parseToValue(text, { ...STRICT_DEFAULTS, ...options });
}
2 changes: 1 addition & 1 deletion rs_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
jsonc-parser = { version = "0.29.0", features = ["cst", "serde", "preserve_order", "error_unicode_width"] }
wasm-bindgen = "=0.2.102"
wasm-bindgen = "=0.2.106"
js-sys = "0.3"
serde = "1.0"
serde-wasm-bindgen = "0.6"
Expand Down
31 changes: 25 additions & 6 deletions rs_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@ fn throw_error(msg: &str) -> JsValue {

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(
typescript_type = "{ allowComments?: boolean; allowTrailingCommas?: boolean; allowLooseObjectPropertyNames?: boolean; allowMissingCommas?: boolean; allowSingleQuotedStrings?: boolean; allowHexadecimalNumbers?: boolean; allowUnaryPlusNumbers?: boolean; }"
)]
#[wasm_bindgen(typescript_type = "ParseOptions")]
pub type JsoncParseOptionsObject;

#[wasm_bindgen(
typescript_type = "string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }"
)]
#[wasm_bindgen(typescript_type = "JsonValue")]
pub type JsonValue;
}

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
/**
* Options for parsing JSONC.
*
* When used with `parse`/`parseToValue`, all options default to `true` (permissive).
* When used with `parseStrict`/`parseToValueStrict`, all options default to `false` (strict).
*/
export interface ParseOptions {
/** Allow comments. */
allowComments?: boolean;
/** Allow trailing commas. */
allowTrailingCommas?: boolean;
/** Allow loose object property names (unquoted keys). */
allowLooseObjectPropertyNames?: boolean;
/** Allow missing commas between elements. */
allowMissingCommas?: boolean;
/** Allow single-quoted strings. */
allowSingleQuotedStrings?: boolean;
/** Allow hexadecimal numbers (e.g., 0xFF). */
allowHexadecimalNumbers?: boolean;
/** Allow unary plus on numbers (e.g., +42). */
allowUnaryPlusNumbers?: boolean;
}

export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
"#;

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.89.0"
channel = "1.92.0"
components = ["clippy", "rustfmt"]