Skip to content

Stdlib: json_encode and json_decode for API endpoints #61

@PurHur

Description

@PurHur

Problem

No JSON builtins in ext/standard/. Small web apps serving APIs need JSON responses. Blocks #270 (004-ApiJson example) and #67 /api/status route.

Goal

json_encode / json_decode for arrays and strings (VM + JIT + AOT when other stdlib builtins do).

Implementation hints

v1 scope (arrays only)

  • VM: ext/standard/json_encode.php — delegate to PHP's json_encode/json_decode via existing builtin wrapper pattern (see htmlspecialchars, header).
  • JIT: start with FFI/callout to libc is unnecessary; use Jit* helper calling PHP runtime or embed minimal encoder for array<string|int|bool|null> + string values only.
  • AOT: same module registration as JIT once VM path exists.
  • Register in ext/standard/Module.php; run script/capability-matrix.php and commit docs/capabilities.md diff.

Suggested files

Area Path
VM builtin ext/standard/json_encode.php, json_decode.php
JIT ext/standard/JitJsonEncode.php (new, mirror JitHeader.php)
Tests test/real/cases/json_encode_api.phpt, compliance mirror
Lint registry lib/Lint/UnsupportedRegistry.php — remove if mapped

PHPT sketch

--FILE--
<?php
header('Content-Type: application/json');
echo json_encode(['ok' => true, 'n' => 1]);
--EXPECT--
{"ok":true,"n":1}

Tasks

Scope v1

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  ./phpc run -r "header('Content-Type: application/json'); echo json_encode(['ok'=>true]);"

Output matches PHP 8. VM + JIT; AOT when bin/compile.php supports the builtin.

Dependencies

Verification (local only)

./script/ci-local.sh --filter json

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions