Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native functions #836

Merged
merged 18 commits into from Jul 12, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use native json_encode() and json_decode()
  • Loading branch information
sjinks committed Jul 12, 2013
commit 98c70369b12272ea17c951540f8150dee986d2de
10 changes: 2 additions & 8 deletions ext/cache/frontend/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,9 @@ PHP_METHOD(Phalcon_Cache_Frontend_Json, beforeStore){

zval *data;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &data);

phalcon_call_func_p1(return_value, "json_encode", data);
RETURN_MM();
phalcon_json_encode(return_value, data, 0 TSRMLS_CC);
}

/**
Expand All @@ -198,11 +195,8 @@ PHP_METHOD(Phalcon_Cache_Frontend_Json, afterRetrieve){

zval *data;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &data);

phalcon_call_func_p1(return_value, "json_decode", data);
RETURN_MM();
phalcon_json_decode(return_value, data, 0 TSRMLS_CC);
}

2 changes: 1 addition & 1 deletion ext/http/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ PHP_METHOD(Phalcon_Http_Request, getJsonRawBody){
PHALCON_INIT_VAR(raw_body);
phalcon_call_method(raw_body, this_ptr, "getrawbody");
if (Z_TYPE_P(raw_body) == IS_STRING) {
phalcon_call_func_p1(return_value, "json_decode", raw_body);
phalcon_json_decode(return_value, raw_body, 0 TSRMLS_CC);
RETURN_MM();
}

Expand Down
8 changes: 4 additions & 4 deletions ext/http/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,18 +606,18 @@ PHP_METHOD(Phalcon_Http_Response, setContent){
PHP_METHOD(Phalcon_Http_Response, setJsonContent){

zval *content, *json_options = NULL, *json_content;
int options = 0;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &content, &json_options);

if (!json_options) {
PHALCON_INIT_VAR(json_options);
ZVAL_LONG(json_options, 0);
if (json_options) {
options = phalcon_get_intval(json_options);
}

PHALCON_INIT_VAR(json_content);
phalcon_call_func_p2(json_content, "json_encode", content, json_options);
phalcon_json_encode(json_content, content, options TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_content"), json_content TSRMLS_CC);
RETURN_THIS();
}
Expand Down
58 changes: 58 additions & 0 deletions ext/kernel/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include "ext/pcre/php_pcre.h"
#endif

#if HAVE_JSON
#include "ext/json/php_json.h"
#endif

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -1218,3 +1222,57 @@ void phalcon_preg_match(zval *return_value, zval *regex, zval *subject, zval *ma
}

#endif

#if HAVE_JSON

void phalcon_json_encode(zval *return_value, zval *v, int opts TSRMLS_DC)
{
smart_str buf = { NULL, 0, 0 };

php_json_encode(&buf, v, opts TSRMLS_CC);
ZVAL_STRINGL(return_value, buf.c, buf.len, 0);
}

void phalcon_json_decode(zval *return_value, zval *v, zend_bool assoc TSRMLS_DC)
{
long int options = assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0;
zval copy;
int use_copy;

if (unlikely(Z_TYPE_P(v) != IS_STRING)) {
zend_make_printable_zval(v, &copy, &use_copy);
if (use_copy) {
v = ©
}
}

php_json_decode_ex(return_value, Z_STRVAL_P(v), Z_STRLEN_P(v), options, 512 /* JSON_PARSER_DEFAULT_DEPTH */ TSRMLS_CC);

if (unlikely(use_copy)) {
zval_dtor(v);
}
}

#else

void phalcon_json_encode(zval *return_value, zval *v, int opts TSRMLS_DC)
{
zval *zopts;

ALLOC_INIT_ZVAL(zopts);
ZVAL_LONG(zopts, opts)
phalcon_call_func_two_params(return_value, ZEND_STRL("json_encode"), v, opts, 1 TSRMLS_CC);
zval_ptr_dtor(zassoc);
}

void phalcon_json_decode(zval *return_value, zval *v, zend_bool assoc TSRMLS_DC)
{
zval *zassoc;

ALLOC_INIT_ZVAL(zassoc);
ZVAL_BOOL(zassoc, assoc);
phalcon_call_func_two_params(return_value, ZEND_STRL("json_decode"), v, zassoc, 1 TSRMLS_CC);
zval_ptr_dtor(zassoc);
}

#endif
4 changes: 4 additions & 0 deletions ext/kernel/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ extern void phalcon_base64_decode(zval *return_value, zval *data);

/** Hash */
void phalcon_md5(zval *return_value, zval *str);

/** JSON */
void phalcon_json_encode(zval *return_value, zval *v, int opts TSRMLS_DC);
void phalcon_json_decode(zval *return_value, zval *v, zend_bool assoc TSRMLS_DC);
2 changes: 1 addition & 1 deletion ext/logger/formatter/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ PHP_METHOD(Phalcon_Logger_Formatter_Json, format){
phalcon_array_update_string(&log, SL("type"), &type_str, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&log, SL("message"), &message, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&log, SL("timestamp"), &timestamp, PH_COPY | PH_SEPARATE);
phalcon_call_func_p1(return_value, "json_encode", log);
phalcon_json_encode(return_value, log, 0 TSRMLS_CC);
RETURN_MM();
}