Skip to content

Commit 1381b14

Browse files
committed
Add JSON_Serializable interface
Objects implementing JSON_Serializable will have their ->jsonSerialize() method called Similar to serialize() and __sleep()
1 parent 2b4539f commit 1381b14

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Added command line option --rz to CLI. (Johannes)
2828
- Added closure $this support back. (Stas)
2929
- Added SplObjectStorage::getHash() hook. (Etienne)
30+
- Added JSON_Serializable interface (json_encode() calls $obj->jsonSerialize()). (Sara)
3031

3132
- default_charset if not specified is now UTF-8 instead of ISO-8859-1. (Rasmus)
3233

ext/json/json.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ static PHP_FUNCTION(json_last_error);
3737

3838
static const char digits[] = "0123456789abcdef";
3939

40+
zend_class_entry *php_json_serializable_ce;
41+
4042
#define PHP_JSON_HEX_TAG (1<<0)
4143
#define PHP_JSON_HEX_AMP (1<<1)
4244
#define PHP_JSON_HEX_APOS (1<<2)
@@ -73,9 +75,25 @@ static const zend_function_entry json_functions[] = {
7375
};
7476
/* }}} */
7577

78+
/* {{{ JSON_Serializable methods */
79+
ZEND_BEGIN_ARG_INFO(json_serialize_arginfo, 0)
80+
/* No arguments */
81+
ZEND_END_ARG_INFO();
82+
83+
static const zend_function_entry json_serializable_interface[] = {
84+
PHP_ABSTRACT_ME(JSON_Serializable, jsonSerialize, json_serialize_arginfo)
85+
{ NULL, NULL, NULL }
86+
};
87+
7688
/* {{{ MINIT */
7789
static PHP_MINIT_FUNCTION(json)
7890
{
91+
zend_class_entry ce;
92+
93+
INIT_CLASS_ENTRY(ce, "JSON_Serializable", json_serializable_interface);
94+
php_json_serializable_ce = zend_register_internal_interface(&ce TSRMLS_CC);
95+
/* Note: Consider adding: interface JSON\Serializable extends JSON_Serializable {} for futureproofing... */
96+
7997
REGISTER_LONG_CONSTANT("JSON_HEX_TAG", PHP_JSON_HEX_TAG, CONST_CS | CONST_PERSISTENT);
8098
REGISTER_LONG_CONSTANT("JSON_HEX_AMP", PHP_JSON_HEX_AMP, CONST_CS | CONST_PERSISTENT);
8199
REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | CONST_PERSISTENT);
@@ -413,6 +431,39 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
413431
}
414432
/* }}} */
415433

434+
435+
static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC)
436+
{
437+
zend_class_entry *ce = Z_OBJCE_P(val);
438+
zval *retval = NULL, fname;
439+
440+
ZVAL_STRING(&fname, "jsonSerialize", 0);
441+
442+
if (FAILURE == call_user_function_ex(EG(function_table), &val, &fname, &retval, 0, NULL, 1, NULL TSRMLS_CC) || !retval) {
443+
zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed calling %s::serialize()", ce->name);
444+
smart_str_appendl(buf, "null", sizeof("null") - 1);
445+
return;
446+
}
447+
448+
if (EG(exception)) {
449+
/* Error already raised */
450+
zval_ptr_dtor(&retval);
451+
smart_str_appendl(buf, "null", sizeof("null") - 1);
452+
return;
453+
}
454+
455+
if ((Z_TYPE_P(retval) == IS_OBJECT) &&
456+
(Z_OBJ_HANDLE_P(retval) == Z_OBJ_HANDLE_P(val))) {
457+
/* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
458+
json_encode_array(buf, &retval, options TSRMLS_CC);
459+
} else {
460+
/* All other types, encode as normal */
461+
php_json_encode(buf, retval, options TSRMLS_CC);
462+
}
463+
464+
zval_ptr_dtor(&retval);
465+
}
466+
416467
PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
417468
{
418469
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
@@ -455,8 +506,13 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
455506
json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
456507
break;
457508

458-
case IS_ARRAY:
459509
case IS_OBJECT:
510+
if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce TSRMLS_CC)) {
511+
json_encode_serializable_object(buf, val, options TSRMLS_CC);
512+
break;
513+
}
514+
/* fallthrough -- Non-serializable object */
515+
case IS_ARRAY:
460516
json_encode_array(buf, &val, options TSRMLS_CC);
461517
break;
462518

ext/json/php_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ZEND_END_MODULE_GLOBALS(json)
4949

5050
PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC);
5151
PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, long depth TSRMLS_DC);
52+
extern zend_class_entry *php_json_serializable_ce;
5253

5354
#endif /* PHP_JSON_H */
5455

ext/json/tests/serialize.phpt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--TEST--
2+
json_encode() Serialization tests
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
class NonSerializingTest
9+
{
10+
public $data;
11+
12+
public function __construct($data)
13+
{
14+
$this->data = $data;
15+
}
16+
}
17+
18+
class SerializingTest extends NonSerializingTest implements JSON_Serializable
19+
{
20+
public function jsonSerialize()
21+
{
22+
return $this->data;
23+
}
24+
}
25+
26+
class ValueSerializingTest extends SerializingTest
27+
{
28+
public function jsonSerialize()
29+
{
30+
return array_values(is_array($this->data) ? $this->data : get_object_vars($this->data));
31+
}
32+
}
33+
34+
class SelfSerializingTest extends SerializingTest
35+
{
36+
public function jsonSerialize()
37+
{
38+
return $this;
39+
}
40+
}
41+
42+
$adata = array(
43+
'str' => 'foo',
44+
'int' => 1,
45+
'float' => 2.3,
46+
'bool' => false,
47+
'nil' => null,
48+
'arr' => array(1,2,3),
49+
'obj' => new StdClass,
50+
);
51+
52+
$ndata = array_values($adata);
53+
54+
$odata = (object)$adata;
55+
56+
foreach(array('NonSerializingTest','SerializingTest','ValueSerializingTest','SelfSerializingTest') as $class) {
57+
echo "==$class==\n";
58+
echo json_encode(new $class($adata)), "\n";
59+
echo json_encode(new $class($ndata)), "\n";
60+
echo json_encode(new $class($odata)), "\n";
61+
}
62+
--EXPECT--
63+
==NonSerializingTest==
64+
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
65+
{"data":["foo",1,2.3,false,null,[1,2,3],{}]}
66+
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
67+
==SerializingTest==
68+
{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}
69+
["foo",1,2.3,false,null,[1,2,3],{}]
70+
{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}
71+
==ValueSerializingTest==
72+
["foo",1,2.3,false,null,[1,2,3],{}]
73+
["foo",1,2.3,false,null,[1,2,3],{}]
74+
["foo",1,2.3,false,null,[1,2,3],{}]
75+
==SelfSerializingTest==
76+
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
77+
{"data":["foo",1,2.3,false,null,[1,2,3],{}]}
78+
{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}}
79+
80+

0 commit comments

Comments
 (0)