27
27
#include "php.h"
28
28
#include "php_ini.h"
29
29
#include "ext/standard/info.h"
30
+ #include "ext/standard/php_var.h"
30
31
#include "php_jansson.h"
31
32
32
33
static inline void * jsson_malloc (size_t len ) {
@@ -37,12 +38,21 @@ static inline void jsson_free(void *block) {
37
38
efree (block );
38
39
}
39
40
41
+ #if PHP_VERSION_ID < 70000
40
42
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
41
46
json_t * json = NULL ;
42
47
HashTable * hash = NULL ;
43
48
HashPosition position ;
49
+ #if PHP_VERSION_ID < 70000
44
50
zval * * zivalue ;
45
-
51
+ #else
52
+ zval * zivalue ;
53
+ #endif
54
+
55
+ #if PHP_VERSION_ID < 70000
46
56
switch (Z_TYPE_PP (zvalue )) {
47
57
case IS_LONG :
48
58
return json_integer (Z_LVAL_PP (zvalue ));
@@ -74,16 +84,49 @@ static inline json_t* json_encode_zend(zval **zvalue TSRMLS_DC) {
74
84
zend_hash_get_current_data_ex (hash , (void * * )& zivalue , & position ) == SUCCESS ;
75
85
zend_hash_move_forward_ex (hash , & position )) {
76
86
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 ));
78
88
}
79
89
}
80
90
}
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 ();
81
123
}
82
-
124
+
83
125
return json ;
84
126
}
85
127
86
128
static inline zval * json_decode_zend (json_t * * json TSRMLS_DC ) {
129
+ #if PHP_VERSION_ID < 7000
87
130
zval * retval = EG (uninitialized_zval_ptr );
88
131
89
132
if (json && * json ) {
@@ -95,7 +138,6 @@ static inline zval* json_decode_zend(json_t **json TSRMLS_DC) {
95
138
case JSON_STRING :
96
139
ALLOC_INIT_ZVAL (retval );
97
140
ZVAL_STRING (retval , json_string_value (* json ), 1 );
98
-
99
141
break ;
100
142
101
143
case JSON_INTEGER :
@@ -146,8 +188,65 @@ static inline zval* json_decode_zend(json_t **json TSRMLS_DC) {
146
188
} else {
147
189
Z_ADDREF_P (retval );
148
190
}
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
151
250
}
152
251
153
252
/* {{{ proto string jsson_encode(mixed variable)
@@ -162,11 +261,15 @@ PHP_FUNCTION(jsson_encode)
162
261
163
262
{
164
263
json_t * json ;
165
- json_error_t error ;
166
264
HashTable * hash = NULL ;
167
265
HashPosition position ;
266
+
267
+ #if PHP_VERSION_ID < 70000
168
268
zval * * zvalue ;
169
-
269
+ #else
270
+ zval * zvalue ;
271
+ #endif
272
+
170
273
switch (Z_TYPE_P (variable )) {
171
274
case IS_NULL :
172
275
json = json_null ();
@@ -184,8 +287,12 @@ PHP_FUNCTION(jsson_encode)
184
287
hash = Z_ARRVAL_P (variable );
185
288
186
289
case IS_OBJECT : {
187
- char * key = NULL ;
290
+ #if PHP_VERSION_ID < 70000
291
+ char * key = NULL ;
188
292
zend_uint klen = 0 ;
293
+ #else
294
+ zend_string * key ;
295
+ #endif
189
296
zend_ulong kdx = 0L ;
190
297
191
298
if (!hash ) {
@@ -195,14 +302,21 @@ PHP_FUNCTION(jsson_encode)
195
302
json = json_object ();
196
303
197
304
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
199
310
zend_hash_move_forward_ex (hash , & position )) {
200
311
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
203
317
next = json_encode_zend (zvalue TSRMLS_CC );
204
318
if (next ) {
205
- json_object_set (json , key , next );
319
+ json_object_set (json , ZSTR_VAL ( key ) , next );
206
320
json_decref (next );
207
321
}
208
322
}
@@ -211,7 +325,11 @@ PHP_FUNCTION(jsson_encode)
211
325
}
212
326
213
327
if (json ) {
328
+ #if PHP_VERSION_ID < 70000
214
329
ZVAL_STRING (return_value , json_dumps (json , 0 ), 0 );
330
+ #else
331
+ ZVAL_STRING (return_value , json_dumps (json , 0 ));
332
+ #endif
215
333
json_decref (json );
216
334
}
217
335
}
@@ -222,8 +340,12 @@ PHP_FUNCTION(jsson_encode)
222
340
PHP_FUNCTION (jsson_decode )
223
341
{
224
342
char * str ;
343
+ #if PHP_VERSION_ID < 70000
225
344
zend_uint slen ;
226
-
345
+ #else
346
+ zend_long slen ;
347
+ #endif
348
+
227
349
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "s" , & str , & slen ) == FAILURE ) {
228
350
return ;
229
351
}
0 commit comments