{"_id":"@bakes/dastardly-json","name":"@bakes/dastardly-json","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@bakes/dastardly-json","version":"1.0.0","description":"JSON parser and serializer for dASTardly","type":"module","main":"dist/index.js","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","default":"./dist/index.js"}},"keywords":["json","parser","serializer","ast","tree-sitter"],"author":{"name":"The Software Bakery"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/thesoftwarebakery/dastardly.git","directory":"packages/json"},"homepage":"https://github.com/thesoftwarebakery/dastardly#readme","bugs":{"url":"https://github.com/thesoftwarebakery/dastardly/issues"},"publishConfig":{"access":"public"},"dependencies":{"tree-sitter":"^0.21.1","tree-sitter-json":"^0.24.5","@bakes/dastardly-tree-sitter-runtime":"^1.0.0","@bakes/dastardly-core":"^1.0.0"},"devDependencies":{"@types/benchmark":"^2.1.5","@types/node":"^24.10.0","@vitest/ui":"^1.6.1","benchmark":"^2.1.4","typescript":"^5.3.0","vitest":"^1.6.1"},"scripts":{"build":"tsc","typecheck":"tsc --noEmit","test":"vitest run","test:watch":"vitest","benchmark":"pnpm build && npx tsx benchmarks/run.ts"},"_id":"@bakes/dastardly-json@1.0.0","_integrity":"sha512-sDV2P9VVO6CVqUR6jgGULIfykJXtIFEHVOkrF3Oh5akdXE4VYlde4uy16U6K7NcCzS7P+YOu9V8TfbAfxJraWA==","_resolved":"/tmp/9df6b15973ff0d610f5280ae54cd1aa1/bakes-dastardly-json-1.0.0.tgz","_from":"file:bakes-dastardly-json-1.0.0.tgz","_nodeVersion":"20.19.5","_npmVersion":"10.8.2","dist":{"integrity":"sha512-sDV2P9VVO6CVqUR6jgGULIfykJXtIFEHVOkrF3Oh5akdXE4VYlde4uy16U6K7NcCzS7P+YOu9V8TfbAfxJraWA==","shasum":"cba66dc61e3868fc2fb952314dcc1ec025bc07af","tarball":"https://registry.npmjs.org/@bakes/dastardly-json/-/dastardly-json-1.0.0.tgz","fileCount":19,"unpackedSize":48793,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@bakes%2fdastardly-json@1.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIFYGYeJjs5xioMZ3rQjwWyujgWI/QmiJFJxXvcHz6hZFAiEAw77faeSutBHzQoyl/NK7hZRDEhaQip2eVnrY8Iw5CTg="}]},"_npmUser":{"name":"georgewaters","email":"george@bakes.software"},"directories":{},"maintainers":[{"name":"georgewaters","email":"george@bakes.software"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/dastardly-json_1.0.0_1763161269134_0.39794914109226"},"_hasShrinkwrap":false}},"time":{"created":"2025-11-14T23:01:08.975Z","1.0.0":"2025-11-14T23:01:09.332Z","modified":"2025-11-14T23:01:09.987Z"},"maintainers":[{"name":"georgewaters","email":"george@bakes.software"}],"description":"JSON parser and serializer for dASTardly","homepage":"https://github.com/thesoftwarebakery/dastardly#readme","keywords":["json","parser","serializer","ast","tree-sitter"],"repository":{"type":"git","url":"git+https://github.com/thesoftwarebakery/dastardly.git","directory":"packages/json"},"author":{"name":"The Software Bakery"},"bugs":{"url":"https://github.com/thesoftwarebakery/dastardly/issues"},"license":"MIT","readme":"# @bakes/dastardly-json\n\nHigh-performance JSON parser and serializer for dASTardly, built with Tree-sitter.\n\n## Installation\n\n```bash\nnpm install @bakes/dastardly-json @bakes/dastardly-core\n```\n\n```bash\npnpm add @bakes/dastardly-json @bakes/dastardly-core\n```\n\n## Overview\n\n`@bakes/dastardly-json` provides a blazing-fast JSON parser and serializer that converts JSON to dASTardly's format-agnostic AST. Built on tree-sitter for real-time editor performance with full position tracking for precise error reporting.\n\n**Key Features:**\n- **High performance** - Tree-sitter-based parsing (36-52x faster than traditional parsers)\n- **Position tracking** - Every node tracks source location (line, column, offset)\n- **Roundtrip support** - Parse and serialize while preserving formatting\n- **Type-safe** - Full TypeScript support with strict mode\n- **Comprehensive** - Handles all JSON types and edge cases\n- **Format-agnostic AST** - Convert to/from other formats (YAML, CSV, etc.)\n\n## Quick Start\n\n### Parsing\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\n\n// Parse to DocumentNode (includes document wrapper)\nconst doc = parse('{\"name\": \"Alice\", \"age\": 30}');\nconsole.log(doc.type); // 'Document'\nconsole.log(doc.body.type); // 'Object'\n\n// Access the data directly\nconst obj = doc.body;\nconsole.log(obj.properties[0].key.value); // 'name'\n```\n\n### Serializing\n\n```typescript\nimport { serialize } from '@bakes/dastardly-json';\nimport { parse } from '@bakes/dastardly-json';\n\nconst doc = parse('{\"name\": \"Alice\"}');\n\n// Compact mode (no whitespace)\nconst compact = serialize(doc);\n// {\"name\":\"Alice\"}\n\n// Pretty-print with custom indentation\nconst pretty = serialize(doc, { indent: 2 });\n// {\n//   \"name\": \"Alice\"\n// }\n```\n\n### Roundtrip\n\n```typescript\nimport { parse, serialize } from '@bakes/dastardly-json';\n\nconst source = '{\"name\": \"Alice\", \"age\": 30}';\nconst doc = parse(source);\nconst output = serialize(doc, { indent: 2 });\n// Preserves data structure, reformats with specified style\n```\n\n## API Reference\n\n### Package Object\n\nThe package exports a `json` object implementing the `FormatPackage` interface:\n\n```typescript\nimport { json } from '@bakes/dastardly-json';\n\nconst doc = json.parse('{\"a\": 1}');\nconst output = json.serialize(doc);\n```\n\n### Convenience Functions\n\nFor convenience, `parse` and `serialize` are also exported as standalone functions (destructured from the `json` object):\n\n#### `parse(source)`\n\nParse JSON string into a DocumentNode:\n\n```typescript\nfunction parse(source: string): DocumentNode;\n```\n\n**Example:**\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\n\nconst doc = parse('{\"key\": \"value\"}');\nconsole.log(doc.type); // 'Document'\nconsole.log(doc.body.type); // 'Object'\n\n// Access data nodes\nconst dataNode = doc.body;\n```\n\n**Throws:** `ParseError` if source is invalid JSON\n\n#### `serialize(node, options?)`\n\nSerialize AST to JSON string:\n\n```typescript\nfunction serialize(\n  node: DocumentNode | DataNode,\n  options?: JSONSerializeOptions\n): string;\n```\n\n**Parameters:**\n- `node` - DocumentNode or DataNode to serialize\n- `options` - Optional serialization options:\n  - `indent?: number | string` - Indentation (number of spaces or string like `'\\t'`)\n  - `preserveRaw?: boolean` - Preserve raw number representations (e.g., `1.0` vs `1`)\n  - `lineEnding?: '\\n' | '\\r\\n'` - Line ending style (default: `'\\n'`)\n\n**Example:**\n\n```typescript\nimport { serialize } from '@bakes/dastardly-json';\n\n// Compact output\nserialize(ast); // {\"name\":\"Alice\"}\n\n// Pretty-print with 2 spaces\nserialize(ast, { indent: 2 });\n// {\n//   \"name\": \"Alice\"\n// }\n\n// Custom indent with tabs\nserialize(ast, { indent: '\\t' });\n\n// Preserve raw number format\nserialize(doc, { preserveRaw: true });\n// Numbers like 1.0 stay as 1.0 instead of becoming 1\n```\n\n## Types\n\n### `JSONSerializeOptions`\n\nOptions for JSON serialization:\n\n```typescript\ninterface JSONSerializeOptions {\n  /** Indentation (number of spaces or custom string) */\n  indent?: number | string;\n\n  /** Preserve raw number representations (e.g., \"1.0\" vs \"1\") */\n  preserveRaw?: boolean;\n\n  /** Line ending style */\n  lineEnding?: '\\n' | '\\r\\n';\n}\n```\n\n## Position Tracking\n\nEvery node in the AST includes position information:\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\n\nconst doc = parse('{\"name\": \"Alice\"}');\n\n// Position info on every node\nconsole.log(doc.loc);\n// {\n//   start: { line: 1, column: 0, offset: 0 },\n//   end: { line: 1, column: 18, offset: 18 }\n// }\n\nconsole.log(doc.body.properties[0].key.loc);\n// Position of the \"name\" key\n```\n\nThis enables precise error reporting and source mapping across format conversions.\n\n## Common Patterns\n\n### Working with DataNodes\n\nIf you only need the data and not the document wrapper:\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\n\nconst doc = parse('{\"users\": [{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]}');\n\n// Access the data directly\nconst data = doc.body;\nif (data.type === 'Object') {\n  const usersProperty = data.properties.find(p => p.key.value === 'users');\n  // ... work with data\n}\n```\n\n### Cross-format Conversion\n\n```typescript\nimport { parse as parseJSON, serialize as serializeJSON } from '@bakes/dastardly-json';\nimport { parse as parseYAML, serialize as serializeYAML } from '@bakes/dastardly-yaml';\n\n// JSON → YAML\nconst jsonDoc = parseJSON('{\"name\": \"Alice\", \"age\": 30}');\nconst yamlOutput = serializeYAML(jsonDoc);\n// name: Alice\n// age: 30\n\n// YAML → JSON\nconst yamlDoc = parseYAML('name: Alice\\nage: 30');\nconst jsonOutput = serializeJSON(yamlDoc, { indent: 2 });\n// {\n//   \"name\": \"Alice\",\n//   \"age\": 30\n// }\n```\n\n### Error Handling\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\nimport { ParseError } from '@bakes/dastardly-core';\n\ntry {\n  const doc = parse('{invalid json}');\n} catch (error) {\n  if (error instanceof ParseError) {\n    console.error(`Parse error at line ${error.line}, column ${error.column}`);\n    console.error(error.message);\n  }\n}\n```\n\n## Edge Cases\n\nThe parser handles all JSON edge cases correctly:\n\n```typescript\nimport { parse } from '@bakes/dastardly-json';\n\n// Empty structures\nparse('{}'); // Empty object\nparse('[]'); // Empty array\n\n// Deeply nested\nparse('{\"a\": {\"b\": {\"c\": 1}}}');\n\n// Unicode escapes\nparse('\"\\\\u0048\\\\u0065\\\\u006c\\\\u006c\\\\u006f\"'); // \"Hello\"\n\n// Large numbers\nparse('9007199254740991'); // Number.MAX_SAFE_INTEGER\nparse('1.7976931348623157e+308'); // Near Number.MAX_VALUE\n\n// Whitespace handling\nparse('  {  \"key\"  :  \"value\"  }  ');\n```\n\n## Related Packages\n\n- **[@bakes/dastardly-core](../core)** - Core AST types and utilities\n- **[@bakes/dastardly-yaml](../yaml)** - YAML parser and serializer\n- **[@bakes/dastardly-csv](../csv)** - CSV parser and serializer\n\n## License\n\nMIT\n","readmeFilename":"README.md","_rev":"1-854dde5c5ead618ee9f85cc854700f07"}