Skip to content

Json array support#90

Merged
cs01 merged 3 commits intomainfrom
json-array-support
Mar 5, 2026
Merged

Json array support#90
cs01 merged 3 commits intomainfrom
json-array-support

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 5, 2026

Summary

c.json() previously failed for object array arguments in two ways:

  • Literal: return c.json([{ name: "Alice", age: 30 }]) → compile error: "JSON.stringify: unsupported argument type"
  • Variable: const items = [...]; return c.json(items) → garbage output like [2.140390922e-314]

Both cases now produce correct JSON: [{"name":"Alice","age":30.0}]

Root Causes

Literal arrays: generateStringifyArgWithSpaces had no handler for arg.type === "array", falling through to the unsupported-type error.

Variable arrays: resolveExpressionType defaulted object-element arrays to number[] type. This caused the variable to be allocated as SymbolKind.Array instead of SymbolKind.ObjectArray, so the JSON serializer read the object pointer as a double and printed it as a float.

Changes

  • type-inference.ts: detect firstElem.type === "object" in array literals and return getArrayType("object") so the variable allocator treats them as object arrays
  • variable-allocator.ts: when allocating an object array variable with no registered interface, extract field metadata from the inline array literal (extractInlineObjectArrayMeta) and store it in ObjectArrayMetadata
  • json.ts: add arg.type === "array" dispatch for literal object arrays; fall back to metadata-based serialization for variables without a registered interface
  • json-array.ts (new): standalone helpers stringifyObjectArrayLiteral and stringifyObjectArrayWithMeta — extracted to a separate file to keep json.ts from growing further, and implemented as free functions (not class methods) to avoid Stage 0 closure-over-this issues

Test

tests/fixtures/network/context-json-array.ts — covers both the literal and variable cases end-to-end through the HTTP router.

@cs01 cs01 force-pushed the json-array-support branch from 2fbc1f1 to 6643616 Compare March 5, 2026 01:55
@cs01 cs01 merged commit 020b49c into main Mar 5, 2026
12 checks passed
@cs01 cs01 deleted the json-array-support branch March 5, 2026 02:01
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