Skip to content

Commit 805471e

Browse files
committed
Fix bug #81112: Implement JsonSerializable for SplFixedArray
This returns an array for SplFixedArray JSON encoding, which is more appropriate than an object with integer string keys. Closes GH-7117.
1 parent ff23a34 commit 805471e

File tree

8 files changed

+56
-8
lines changed

8 files changed

+56
-8
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PHP NEWS
1212
- phpdbg:
1313
. Fixed bug #81135 (unknown help topic causes assertion failure). (krakjoe)
1414

15+
- SPL:
16+
. Fixed buf #81112 (Special json_encode behavior for SplFixedArray). (Nikita)
17+
1518
10 Jun 2021, PHP 8.1.0alpha1
1619

1720
- Core:

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ PHP 8.1 UPGRADE NOTES
163163
- SPL:
164164
. SplFileObject::fputcsv() now accepts a new "eol" argument which allow to
165165
define a custom eol sequence, the default remains the same and is "\n".
166+
. SplFixedArray will now be JSON encoded like an array.
166167

167168
========================================
168169
2. New Features

ext/spl/config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c sp
22
PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h])
33
PHP_ADD_EXTENSION_DEP(spl, pcre, true)
44
PHP_ADD_EXTENSION_DEP(spl, standard, true)
5+
PHP_ADD_EXTENSION_DEP(spl, json)

ext/spl/php_spl.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,14 @@ PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
735735
return SUCCESS;
736736
} /* }}} */
737737

738-
/* {{{ spl_module_entry */
738+
static const zend_module_dep spl_deps[] = {
739+
ZEND_MOD_REQUIRED("json")
740+
ZEND_MOD_END
741+
};
742+
739743
zend_module_entry spl_module_entry = {
740-
STANDARD_MODULE_HEADER,
744+
STANDARD_MODULE_HEADER_EX, NULL,
745+
spl_deps,
741746
"SPL",
742747
ext_functions,
743748
PHP_MINIT(spl),
@@ -748,4 +753,3 @@ zend_module_entry spl_module_entry = {
748753
PHP_SPL_VERSION,
749754
STANDARD_MODULE_PROPERTIES
750755
};
751-
/* }}} */

ext/spl/spl_fixedarray.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "spl_fixedarray.h"
3232
#include "spl_exceptions.h"
3333
#include "spl_iterators.h"
34+
#include "ext/json/php_json.h"
3435

3536
zend_object_handlers spl_handler_SplFixedArray;
3637
PHPAPI zend_class_entry *spl_ce_SplFixedArray;
@@ -758,6 +759,18 @@ PHP_METHOD(SplFixedArray, getIterator)
758759
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
759760
}
760761

762+
PHP_METHOD(SplFixedArray, jsonSerialize)
763+
{
764+
ZEND_PARSE_PARAMETERS_NONE();
765+
766+
spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
767+
array_init_size(return_value, intern->array.size);
768+
for (zend_long i = 0; i < intern->array.size; i++) {
769+
zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]);
770+
Z_TRY_ADDREF(intern->array.elements[i]);
771+
}
772+
}
773+
761774
static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
762775
{
763776
zval_ptr_dtor(&iter->data);
@@ -838,7 +851,8 @@ zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *ob
838851

839852
PHP_MINIT_FUNCTION(spl_fixedarray)
840853
{
841-
spl_ce_SplFixedArray = register_class_SplFixedArray(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable);
854+
spl_ce_SplFixedArray = register_class_SplFixedArray(
855+
zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce);
842856
spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
843857
spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
844858
spl_ce_SplFixedArray->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;

ext/spl/spl_fixedarray.stub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/** @generate-class-entries */
44

5-
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable
5+
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable
66
{
77
public function __construct(int $size = 0) {}
88

@@ -49,4 +49,6 @@ public function offsetSet($index, mixed $value) {}
4949
public function offsetUnset($index) {}
5050

5151
public function getIterator(): Iterator {}
52+
53+
public function jsonSerialize(): array {}
5254
}

ext/spl/spl_fixedarray_arginfo.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: aeac254f38638c19a11f7d79ac2e5c2d40924e58 */
2+
* Stub hash: 115b2d974b18287654be925c4fdc2236674423eb */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplFixedArray___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, size, IS_LONG, 0, "0")
@@ -39,6 +39,9 @@ ZEND_END_ARG_INFO()
3939
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_SplFixedArray_getIterator, 0, 0, Iterator, 0)
4040
ZEND_END_ARG_INFO()
4141

42+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SplFixedArray_jsonSerialize, 0, 0, IS_ARRAY, 0)
43+
ZEND_END_ARG_INFO()
44+
4245

4346
ZEND_METHOD(SplFixedArray, __construct);
4447
ZEND_METHOD(SplFixedArray, __wakeup);
@@ -52,6 +55,7 @@ ZEND_METHOD(SplFixedArray, offsetGet);
5255
ZEND_METHOD(SplFixedArray, offsetSet);
5356
ZEND_METHOD(SplFixedArray, offsetUnset);
5457
ZEND_METHOD(SplFixedArray, getIterator);
58+
ZEND_METHOD(SplFixedArray, jsonSerialize);
5559

5660

5761
static const zend_function_entry class_SplFixedArray_methods[] = {
@@ -67,16 +71,17 @@ static const zend_function_entry class_SplFixedArray_methods[] = {
6771
ZEND_ME(SplFixedArray, offsetSet, arginfo_class_SplFixedArray_offsetSet, ZEND_ACC_PUBLIC)
6872
ZEND_ME(SplFixedArray, offsetUnset, arginfo_class_SplFixedArray_offsetUnset, ZEND_ACC_PUBLIC)
6973
ZEND_ME(SplFixedArray, getIterator, arginfo_class_SplFixedArray_getIterator, ZEND_ACC_PUBLIC)
74+
ZEND_ME(SplFixedArray, jsonSerialize, arginfo_class_SplFixedArray_jsonSerialize, ZEND_ACC_PUBLIC)
7075
ZEND_FE_END
7176
};
7277

73-
static zend_class_entry *register_class_SplFixedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_ArrayAccess, zend_class_entry *class_entry_Countable)
78+
static zend_class_entry *register_class_SplFixedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_ArrayAccess, zend_class_entry *class_entry_Countable, zend_class_entry *class_entry_JsonSerializable)
7479
{
7580
zend_class_entry ce, *class_entry;
7681

7782
INIT_CLASS_ENTRY(ce, "SplFixedArray", class_SplFixedArray_methods);
7883
class_entry = zend_register_internal_class_ex(&ce, NULL);
79-
zend_class_implements(class_entry, 3, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable);
84+
zend_class_implements(class_entry, 4, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable, class_entry_JsonSerializable);
8085

8186
return class_entry;
8287
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
json_encode() on SplFixedArray
3+
--FILE--
4+
<?php
5+
6+
echo json_encode(new SplFixedArray()) . "\n";
7+
echo json_encode(new SplFixedArray(1)) . "\n";
8+
9+
$a = new SplFixedArray(3);
10+
$a[0] = 0;
11+
$a[2] = 2;
12+
echo json_encode($a) . "\n";
13+
14+
?>
15+
--EXPECT--
16+
[]
17+
[null]
18+
[0,null,2]

0 commit comments

Comments
 (0)