Skip to content

Router and JSON auto-serialization fixes#82

Merged
cs01 merged 6 commits intomainfrom
worktree-router-json-fix
Mar 4, 2026
Merged

Router and JSON auto-serialization fixes#82
cs01 merged 6 commits intomainfrom
worktree-router-json-fix

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 3, 2026

Router & JSON improvements: Accept any type, auto-stringify

Summary

Enhances the router's JSON response handling to accept any type and automatically stringify before serialization. Fixes route matching edge cases with explicit null/undefined checks and refactors the semantic union-type checker for better code clarity.

Changes

Router JSON Method

  • lib/router.ts: Changed Context.json() to accept data: any instead of requiring pre-stringified strings
  • Removed explicit JSON.stringify() call — now relies on codegen to handle stringification
  • Enables seamless serialization of objects: c.json({ message: "hello" })

Route Matching Safety

  • lib/router.ts: Enhanced outer group matching with explicit null/undefined checks
  • Old: if (outerGroup !== "")
  • New: if (outerGroup !== undefined && outerGroup !== null && outerGroup !== "")
  • Prevents potential matching failures on certain regex patterns

Semantic Pass Refactoring

  • src/semantic/union-type-checker.ts: Converted from class-based to function-based approach
  • Removes unnecessary instance state, making logic more straightforward
  • Maintains identical behavior for union type safety validation

JSON Codegen

  • src/codegen/stdlib/json.ts: Added support for auto-stringifying in c.json() calls
  • Handles object-to-string conversion at code generation time
  • Propagates type information through generator context

Type System & Codegen Infrastructure

  • src/codegen/infrastructure/generator-context.ts: Added new context methods for JSON handling
  • src/codegen/llvm-generator.ts: Integrated JSON stringification logic
  • src/codegen/types/objects/class.ts: Updated for proper field handling

Parser Updates

  • src/parser-ts/handlers/declarations.ts: Minor adjustments for accurate parsing
  • src/parser-native/transformer.ts: Matching updates in native parser
  • src/parser-ts/transformer.ts: Parser consistency fixes

Type Definitions

  • chadscript.d.ts: Updated Router and Context type signatures
  • src/ast/types.ts: Added optional json support flags

Documentation & Tests

  • docs/language/classes.md: Added class method documentation
  • docs/language/features.md: Updated feature list with JSON enhancements
  • tests/fixtures/router/context-json-any.ts: New test for c.json() with object literals
  • tests/fixtures/router/router-named-handlers.ts: Test coverage for named handler patterns

Test Coverage

  • context-json-any.ts: Validates c.json({ message: "hello" }) produces correct JSON output
  • router-named-handlers.ts: Tests named handler patterns with Context

Verification

All changes verified through:

  • Unit tests for JSON serialization
  • Router integration tests
  • Semantic analysis validation
  • Type system correctness

@cs01 cs01 changed the title Worktree router json fix router json fix Mar 3, 2026
@cs01 cs01 changed the title router json fix Router and JSON auto-serialization fixes Mar 3, 2026
@cs01 cs01 force-pushed the worktree-router-json-fix branch from b251799 to 9456859 Compare March 4, 2026 05:57
@cs01 cs01 merged commit 6ed9f8f into main Mar 4, 2026
12 checks passed
@cs01 cs01 deleted the worktree-router-json-fix branch March 4, 2026 06:04
@cs01 cs01 mentioned this pull request Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant