Skip to content

Commit 86a3ec9

Browse files
committed
Feat(compatible): PHP 7.X
1 parent 3751b4d commit 86a3ec9

File tree

1 file changed

+136
-14
lines changed

1 file changed

+136
-14
lines changed

jansson.c

Lines changed: 136 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "php.h"
2828
#include "php_ini.h"
2929
#include "ext/standard/info.h"
30+
#include "ext/standard/php_var.h"
3031
#include "php_jansson.h"
3132

3233
static inline void* jsson_malloc(size_t len) {
@@ -37,12 +38,21 @@ static inline void jsson_free(void *block) {
3738
efree(block);
3839
}
3940

41+
#if PHP_VERSION_ID < 70000
4042
static inline json_t* json_encode_zend(zval **zvalue TSRMLS_DC) {
43+
#else
44+
static inline json_t* json_encode_zend(zval *zvalue TSRMLS_DC) {
45+
#endif
4146
json_t *json = NULL;
4247
HashTable *hash = NULL;
4348
HashPosition position;
49+
#if PHP_VERSION_ID < 70000
4450
zval **zivalue;
45-
51+
#else
52+
zval *zivalue;
53+
#endif
54+
55+
#if PHP_VERSION_ID < 70000
4656
switch (Z_TYPE_PP(zvalue)) {
4757
case IS_LONG:
4858
return json_integer(Z_LVAL_PP(zvalue));
@@ -74,16 +84,49 @@ static inline json_t* json_encode_zend(zval **zvalue TSRMLS_DC) {
7484
zend_hash_get_current_data_ex(hash, (void**)&zivalue, &position) == SUCCESS;
7585
zend_hash_move_forward_ex(hash, &position)) {
7686
if (zend_hash_get_current_key_ex(hash, &key, &klen, &kdx, 0, &position) == HASH_KEY_IS_STRING) {
77-
json_obect_set(json, key, json_encode_zend(zivalue TSRMLS_CC));
87+
json_object_set(json, key, json_encode_zend(zivalue TSRMLS_CC));
7888
}
7989
}
8090
}
91+
#else
92+
switch (Z_TYPE_P(zvalue)) {
93+
case IS_LONG:
94+
return json_integer(Z_LVAL_P(zvalue));
95+
96+
case IS_DOUBLE:
97+
return json_real(Z_DVAL_P(zvalue));
98+
99+
case IS_ARRAY:
100+
hash = Z_ARRVAL_P(zvalue);
101+
102+
case IS_OBJECT: {
103+
zend_string *key = NULL;
104+
zend_ulong klen = 0;
105+
106+
if (!hash) {
107+
hash = Z_OBJPROP_P(zvalue);
108+
}
109+
110+
json = json_object();
111+
112+
for (zend_hash_internal_pointer_reset_ex(hash, &position);
113+
(zivalue = zend_hash_get_current_data_ex(hash, &position));
114+
zend_hash_move_forward_ex(hash, &position)) {
115+
if (zend_hash_get_current_key_ex(hash, &key, &klen, &position) == HASH_KEY_IS_STRING) {
116+
json_object_set(json, ZSTR_VAL(key), json_encode_zend(zivalue TSRMLS_CC));
117+
}
118+
}
119+
}
120+
#endif
121+
case IS_NULL:
122+
return json_null();
81123
}
82-
124+
83125
return json;
84126
}
85127

86128
static inline zval* json_decode_zend(json_t **json TSRMLS_DC) {
129+
#if PHP_VERSION_ID < 7000
87130
zval* retval = EG(uninitialized_zval_ptr);
88131

89132
if (json && *json) {
@@ -95,7 +138,6 @@ static inline zval* json_decode_zend(json_t **json TSRMLS_DC) {
95138
case JSON_STRING:
96139
ALLOC_INIT_ZVAL(retval);
97140
ZVAL_STRING(retval, json_string_value(*json), 1);
98-
99141
break;
100142

101143
case JSON_INTEGER:
@@ -146,8 +188,65 @@ static inline zval* json_decode_zend(json_t **json TSRMLS_DC) {
146188
} else {
147189
Z_ADDREF_P(retval);
148190
}
149-
150-
return retval;
191+
192+
return retval;
193+
#else
194+
zval* retval = emalloc(sizeof(zval));
195+
196+
if (json && *json) {
197+
switch (json_typeof(*json)) {
198+
case JSON_NULL:
199+
ZVAL_NULL(retval);
200+
break;
201+
202+
case JSON_STRING:
203+
ZVAL_STRINGL(retval, json_string_value(*json), 1);
204+
break;
205+
206+
case JSON_INTEGER:
207+
ZVAL_LONG(retval, json_integer_value(*json));
208+
break;
209+
210+
case JSON_REAL:
211+
ZVAL_DOUBLE(retval, json_real_value(*json));
212+
break;
213+
214+
case JSON_TRUE:
215+
case JSON_FALSE:
216+
ZVAL_BOOL(retval, json_boolean_value(*json));
217+
break;
218+
219+
case JSON_OBJECT: {
220+
const char *jkey;
221+
json_t *jvalue;
222+
223+
array_init(retval);
224+
225+
json_object_foreach(*json, jkey, jvalue) {
226+
add_assoc_zval(retval, jkey, json_decode_zend(&jvalue TSRMLS_CC));
227+
}
228+
} break;
229+
230+
case JSON_ARRAY: {
231+
size_t jkey;
232+
json_t *jvalue;
233+
234+
array_init(retval);
235+
236+
json_array_foreach(*json, jkey, jvalue) {
237+
add_index_zval(retval, jkey, json_decode_zend(&jvalue TSRMLS_CC));
238+
}
239+
} break;
240+
241+
default:
242+
zend_error(E_ERROR, "unknown tyep : %d\n", json_typeof(*json));
243+
}
244+
} else {
245+
Z_ADDREF_P(retval);
246+
}
247+
248+
return retval;
249+
#endif
151250
}
152251

153252
/* {{{ proto string jsson_encode(mixed variable)
@@ -162,11 +261,15 @@ PHP_FUNCTION(jsson_encode)
162261

163262
{
164263
json_t *json;
165-
json_error_t error;
166264
HashTable *hash = NULL;
167265
HashPosition position;
266+
267+
#if PHP_VERSION_ID < 70000
168268
zval **zvalue;
169-
269+
#else
270+
zval *zvalue;
271+
#endif
272+
170273
switch (Z_TYPE_P(variable)) {
171274
case IS_NULL:
172275
json = json_null();
@@ -184,8 +287,12 @@ PHP_FUNCTION(jsson_encode)
184287
hash = Z_ARRVAL_P(variable);
185288

186289
case IS_OBJECT: {
187-
char *key = NULL;
290+
#if PHP_VERSION_ID < 70000
291+
char *key = NULL;
188292
zend_uint klen = 0;
293+
#else
294+
zend_string *key;
295+
#endif
189296
zend_ulong kdx = 0L;
190297

191298
if (!hash) {
@@ -195,14 +302,21 @@ PHP_FUNCTION(jsson_encode)
195302
json = json_object();
196303

197304
for (zend_hash_internal_pointer_reset_ex(hash, &position);
198-
zend_hash_get_current_data_ex(hash, (void**)&zvalue, &position) == SUCCESS;
305+
#if PHP_VERSION_ID < 70000
306+
zend_hash_get_current_data_ex(hash, (void**)&zvalue, &position) == SUCCESS;
307+
#else
308+
(zvalue = zend_hash_get_current_data_ex(hash, &position));
309+
#endif
199310
zend_hash_move_forward_ex(hash, &position)) {
200311
json_t *next = NULL;
201-
if (zend_hash_get_current_key_ex(
202-
hash, &key, &klen, &kdx, 0, &position) == HASH_KEY_IS_STRING) {
312+
#if PHP_VERSION_ID < 70000
313+
if (zend_hash_get_current_key_ex(hash, &key, &klen, &kdx, 0, &position) == HASH_KEY_IS_STRING) {
314+
#else
315+
if (zend_hash_get_current_key_ex(hash, &key, &kdx, &position) == HASH_KEY_IS_STRING) {
316+
#endif
203317
next = json_encode_zend(zvalue TSRMLS_CC);
204318
if (next) {
205-
json_object_set(json, key, next);
319+
json_object_set(json, ZSTR_VAL(key), next);
206320
json_decref(next);
207321
}
208322
}
@@ -211,7 +325,11 @@ PHP_FUNCTION(jsson_encode)
211325
}
212326

213327
if (json) {
328+
#if PHP_VERSION_ID < 70000
214329
ZVAL_STRING(return_value, json_dumps(json, 0), 0);
330+
#else
331+
ZVAL_STRING(return_value, json_dumps(json, 0));
332+
#endif
215333
json_decref(json);
216334
}
217335
}
@@ -222,8 +340,12 @@ PHP_FUNCTION(jsson_encode)
222340
PHP_FUNCTION(jsson_decode)
223341
{
224342
char *str;
343+
#if PHP_VERSION_ID < 70000
225344
zend_uint slen;
226-
345+
#else
346+
zend_long slen;
347+
#endif
348+
227349
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &slen) == FAILURE) {
228350
return;
229351
}

0 commit comments

Comments
 (0)