📜 ⬆️ ⬇️

JSON function transfer

From this topic, you will learn how to send JavaScript functions via JSON using PHP (the concept itself can be applied to other languages).

PHP, starting with version 5.2.0, includes json_encode () and json_decode () functions. These functions encode the data in JSON format and decode JSON into associative arrays. The json_encode () function cannot be encoded. In some cases it is pretty darn awkward.
  1. Added example implementation in Zend Framework.
  2. Question to karmavampiram - you know how to give a handler to create an object differently?
  3. Comment on why and who needs it.

Problem

//
$foo = array(
'number' => 1,
'float' => 1.5,
'array' => array(1,2),
'string' => 'bar' ,
'function' => 'function(){return "foo bar";}'
);
// JSON
$json = json_encode($foo);
//
echo $json;


* This source code was highlighted with Source Code Highlighter .

Result

{
"number" :1,
"float" :1.5,
"array" :[1,2],
"string" : "bar" ,
"function" : "function(){return \"foo bar\";}"
}

* This source code was highlighted with Source Code Highlighter .


Since if you do not enclose the definition of a function in quotes, that is, you do not define it as a string, the code will not be executed. So, in principle, jscon_encode () is not suitable for implementing this functionality.
')

Decision

  1. We pass through the array which will be encoded.
  2. We check the coded value for the presence of a function definition.
  3. Remember the value and replace it with a unique label.
  4. We code the modified array using json_encode ().
  5. Replace the unique label with the original value.
  6. We give JSON to the client.
// .
$foo = array(
'number' => 1,
'float' => 1.5,
'array' => array(1,2),
'string' => 'bar' ,
'function' => 'function(){return "foo bar";}'
);

$value_arr = array();
$replace_keys = array();
foreach ($foo as $key => &$ value ){
//
if (strpos($ value , 'function(' )===0){
// .
$value_arr[] = $ value ;
// '' ..
$ value = '%' . $key . '%' ;
// .
$replace_keys[] = '"' . $ value . '"' ;
}
}

// JSON
$json = json_encode($foo);

// $json :
{
"number" :1,
"float" :1.5,
"array" :[1,2],
"string" : "bar" ,
"function" : "%function%"
}

// .
$json = str_replace($replace_keys, $value_arr, $json);

// .
echo $json;

// JSON :
{
"number" :1,
"float" :1.5,
"array" :[1,2],
"string" : "bar" ,
"function" :function(){ return "foo bar" ;}
}


* This source code was highlighted with Source Code Highlighter .
Now, in the resulting object, “function” is not a string, but a function. Actually the problem is solved. If you use this solution with Prototype, it will look something like this:
new Ajax.Request( 'json_server.php' , {
method: 'get' ,
onSuccess: function(transport){
var json = transport.responseText.evalJSON();
alert(json.function()); // => alert 'foo bar'
}
});


* This source code was highlighted with Source Code Highlighter .


Implementation in the Zend Framework:


$foo = array(
'integer' =>9,
'string' => 'test string' ,
'function' => Zend_Json_Expr(
'function(){ window.alert("javascript function encoded by Zend_Json") }'
),
);

Zend_Json::encode($foo, false , array( 'enableJsonExprFinder' => true ));
// it will returns json encoded string:
{
"integer" :9,
"string" : "test string" ,
"function" :function(){
window.alert( "javascript function encoded by Zend_Json" )
}
}


* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/63057/


All Articles