|
| 1 | +--TEST-- |
| 2 | +json_encode() stops encoding after an object or array key fails to encode |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +class Value implements JsonSerializable |
| 7 | +{ |
| 8 | + public function jsonSerialize(): mixed |
| 9 | + { |
| 10 | + echo "jsonSerialize() called\n"; |
| 11 | + return 1; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +eval('class Declared { public $' . "\xff" . ' = 1; public $next; }'); |
| 16 | + |
| 17 | +$dynamic = new stdClass; |
| 18 | +$dynamic->{"\xff"} = 1; |
| 19 | +$dynamic->next = new Value; |
| 20 | + |
| 21 | +$declared = new Declared; |
| 22 | +$declared->next = new Value; |
| 23 | + |
| 24 | +$cases = [ |
| 25 | + 'array' => ["\xff" => 1, 'next' => new Value], |
| 26 | + 'array, later NAN' => ["\xff" => 1, 'next' => NAN], |
| 27 | + 'dynamic property' => $dynamic, |
| 28 | + 'declared property' => $declared, |
| 29 | +]; |
| 30 | + |
| 31 | +foreach ($cases as $name => $value) { |
| 32 | + echo "$name:\n"; |
| 33 | + var_dump(json_encode($value), json_last_error_msg()); |
| 34 | +} |
| 35 | + |
| 36 | +echo "partial output:\n"; |
| 37 | +var_dump(json_encode(["\xff" => 1, 'next' => 2], JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error_msg()); |
| 38 | + |
| 39 | +?> |
| 40 | +--EXPECT-- |
| 41 | +array: |
| 42 | +bool(false) |
| 43 | +string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" |
| 44 | +array, later NAN: |
| 45 | +bool(false) |
| 46 | +string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" |
| 47 | +dynamic property: |
| 48 | +bool(false) |
| 49 | +string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" |
| 50 | +declared property: |
| 51 | +bool(false) |
| 52 | +string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" |
| 53 | +partial output: |
| 54 | +string(15) "{"":1,"next":2}" |
| 55 | +string(56) "Malformed UTF-8 characters, possibly incorrectly encoded" |
0 commit comments