Skip to content

json_encode() ignores JsonSerializable: the object encodes from its properties, jsonSerialize() is never called #94

Closed

Description

@blacktrs

Summary

json_encode() ignores JsonSerializable: the object encodes from its properties, jsonSerialize() is never called.

Repro

<?php
final class Money implements JsonSerializable {
    public function __construct(private int $cents, private string $cur) {}
    public function jsonSerialize(): mixed { return ['amount' => $this->cents / 100, 'currency' => $this->cur]; }
}
final class Tags implements JsonSerializable {
    public function jsonSerialize(): mixed { return ['a', 'b']; }
}
final class Id implements JsonSerializable {
    public function jsonSerialize(): mixed { return 42; }
}
echo json_encode(new Money(1250, 'EUR')), "\n";
echo json_encode(new Tags()), "\n";
echo json_encode(['id' => new Id(), 'list' => [new Tags(), new Id()]]), "\n";

Expected (php)

{"amount":12.5,"currency":"EUR"}
["a","b"]
{"id":42,"list":[["a","b"],42]}

Actual (manticore c7b1d623, Darwin arm64)

[]
{}
{"id":{},"list":[{},{}]}

Tracking

Known-bug repro: tests/aot/repro/wrong/json_encode_ignores_jsonserializable.php — bash tests/aot/xfail.sh -k json_encode_ignores_jsonserializable reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    wrong-answer
    on Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions