Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98bd288

Browse files
committed
JSON serializer
1 parent 8a45d18 commit 98bd288

5 files changed

Lines changed: 49 additions & 15 deletions

File tree

common.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ typedef enum _PUBSUB_TYPE {
8585
#define REDIS_FAILOVER_DISTRIBUTE 2
8686
#define REDIS_FAILOVER_DISTRIBUTE_SLAVES 3
8787
/* serializers */
88-
#define REDIS_SERIALIZER_NONE 0
89-
#define REDIS_SERIALIZER_PHP 1
90-
#define REDIS_SERIALIZER_IGBINARY 2
91-
#define REDIS_SERIALIZER_MSGPACK 3
88+
typedef enum {
89+
REDIS_SERIALIZER_NONE,
90+
REDIS_SERIALIZER_PHP,
91+
REDIS_SERIALIZER_IGBINARY,
92+
REDIS_SERIALIZER_MSGPACK,
93+
REDIS_SERIALIZER_JSON
94+
} redis_serializer;
9295
/* compression */
9396
#define REDIS_COMPRESSION_NONE 0
9497
#define REDIS_COMPRESSION_LZF 1
@@ -252,7 +255,7 @@ typedef struct {
252255
int watching;
253256
zend_string *persistent_id;
254257

255-
int serializer;
258+
redis_serializer serializer;
256259
int compression;
257260
long dbNumber;
258261

library.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "php_redis.h"
2626
#include "library.h"
2727
#include "redis_commands.h"
28+
#include <ext/json/php_json.h>
2829
#include <ext/standard/php_rand.h>
2930

3031
#define UNSERIALIZE_NONE 0
@@ -2165,7 +2166,6 @@ redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len
21652166
switch(redis_sock->serializer) {
21662167
case REDIS_SERIALIZER_NONE:
21672168
switch(Z_TYPE_P(z)) {
2168-
21692169
case IS_STRING:
21702170
*val = Z_STRVAL_P(z);
21712171
*val_len = Z_STRLEN_P(z);
@@ -2205,8 +2205,8 @@ redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len
22052205
case REDIS_SERIALIZER_MSGPACK:
22062206
#ifdef HAVE_REDIS_MSGPACK
22072207
php_msgpack_serialize(&sstr, z TSRMLS_CC);
2208-
*val = estrndup(sstr.s->val, sstr.s->len);
2209-
*val_len = sstr.s->len;
2208+
*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
2209+
*val_len = ZSTR_LEN(sstr.s);
22102210
smart_str_free(&sstr);
22112211

22122212
return 1;
@@ -2221,6 +2221,13 @@ redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len
22212221
}
22222222
#endif
22232223
break;
2224+
case REDIS_SERIALIZER_JSON:
2225+
php_json_encode(&sstr, z, PHP_JSON_OBJECT_AS_ARRAY);
2226+
*val = estrndup(ZSTR_VAL(sstr.s), ZSTR_LEN(sstr.s));
2227+
*val_len = ZSTR_LEN(sstr.s);
2228+
smart_str_free(&sstr);
2229+
return 1;
2230+
EMPTY_SWITCH_DEFAULT_CASE()
22242231
}
22252232

22262233
return 0;
@@ -2235,6 +2242,9 @@ redis_unserialize(RedisSock* redis_sock, const char *val, int val_len,
22352242
int ret = 0;
22362243

22372244
switch(redis_sock->serializer) {
2245+
case REDIS_SERIALIZER_NONE:
2246+
/* Nothing to do */
2247+
break;
22382248
case REDIS_SERIALIZER_PHP:
22392249
PHP_VAR_UNSERIALIZE_INIT(var_hash);
22402250

@@ -2280,6 +2290,10 @@ redis_unserialize(RedisSock* redis_sock, const char *val, int val_len,
22802290
ret = !igbinary_unserialize((const uint8_t *)val, (size_t)val_len, z_ret TSRMLS_CC);
22812291
#endif
22822292
break;
2293+
case REDIS_SERIALIZER_JSON:
2294+
ret = !php_json_decode(z_ret, (char *)val, val_len, 1, PHP_JSON_PARSER_DEFAULT_DEPTH);
2295+
break;
2296+
EMPTY_SWITCH_DEFAULT_CASE()
22832297
}
22842298

22852299
return ret;

redis.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ static void add_class_constants(zend_class_entry *ce, int is_cluster TSRMLS_DC)
673673
#ifdef HAVE_REDIS_IGBINARY
674674
zend_declare_class_constant_long(ce, ZEND_STRL("SERIALIZER_IGBINARY"), REDIS_SERIALIZER_IGBINARY TSRMLS_CC);
675675
#endif
676+
#ifdef HAVE_REDIS_MSGPACK
677+
zend_declare_class_constant_long(ce, ZEND_STRL("SERIALIZER_MSGPACK"), REDIS_SERIALIZER_MSGPACK TSRMLS_CC);
678+
#endif
679+
zend_declare_class_constant_long(ce, ZEND_STRL("SERIALIZER_JSON"), REDIS_SERIALIZER_JSON TSRMLS_CC);
676680

677681
/* compression */
678682
zend_declare_class_constant_long(ce, ZEND_STRL("COMPRESSION_NONE"), REDIS_COMPRESSION_NONE TSRMLS_CC);
@@ -694,10 +698,6 @@ static void add_class_constants(zend_class_entry *ce, int is_cluster TSRMLS_DC)
694698
zend_declare_class_constant_long(ce, ZEND_STRL("FAILOVER_DISTRIBUTE_SLAVES"), REDIS_FAILOVER_DISTRIBUTE_SLAVES TSRMLS_CC);
695699
}
696700

697-
#ifdef HAVE_REDIS_MSGPACK
698-
zend_declare_class_constant_long(ce, ZEND_STRL("SERIALIZER_MSGPACK"), REDIS_SERIALIZER_MSGPACK TSRMLS_CC);
699-
#endif
700-
701701
zend_declare_class_constant_stringl(ce, "AFTER", 5, "after", 5 TSRMLS_CC);
702702
zend_declare_class_constant_stringl(ce, "BEFORE", 6, "before", 6 TSRMLS_CC);
703703
}

redis_commands.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3882,7 +3882,9 @@ void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
38823882
switch(option) {
38833883
case REDIS_OPT_SERIALIZER:
38843884
val_long = zval_get_long(val);
3885-
if (val_long == REDIS_SERIALIZER_NONE || val_long == REDIS_SERIALIZER_PHP
3885+
if (val_long == REDIS_SERIALIZER_NONE
3886+
|| val_long == REDIS_SERIALIZER_PHP
3887+
|| val_long == REDIS_SERIALIZER_JSON
38863888
#ifdef HAVE_REDIS_IGBINARY
38873889
|| val_long == REDIS_SERIALIZER_IGBINARY
38883890
#endif

tests/RedisTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4244,6 +4244,16 @@ public function testSerializerMsgPack() {
42444244
}
42454245
}
42464246

4247+
public function testSerializerJSON()
4248+
{
4249+
$this->checkSerializer(Redis::SERIALIZER_JSON);
4250+
4251+
// with prefix
4252+
$this->redis->setOption(Redis::OPT_PREFIX, "test:");
4253+
$this->checkSerializer(Redis::SERIALIZER_JSON);
4254+
$this->redis->setOption(Redis::OPT_PREFIX, "");
4255+
}
4256+
42474257
private function checkSerializer($mode) {
42484258

42494259
$this->redis->del('key');
@@ -4452,8 +4462,13 @@ private function checkSerializer($mode) {
44524462
$this->redis->set('x', [new stdClass, new stdClass]);
44534463
$x = $this->redis->get('x');
44544464
$this->assertTrue(is_array($x));
4455-
$this->assertTrue(is_object($x[0]) && get_class($x[0]) === 'stdClass');
4456-
$this->assertTrue(is_object($x[1]) && get_class($x[1]) === 'stdClass');
4465+
if ($mode === Redis::SERIALIZER_JSON) {
4466+
$this->assertTrue(is_array($x[0]));
4467+
$this->assertTrue(is_array($x[1]));
4468+
} else {
4469+
$this->assertTrue(is_object($x[0]) && get_class($x[0]) === 'stdClass');
4470+
$this->assertTrue(is_object($x[1]) && get_class($x[1]) === 'stdClass');
4471+
}
44574472

44584473
// revert
44594474
$this->assertTrue($this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE) === TRUE); // set ok

0 commit comments

Comments
 (0)