Zend Framework: Zend_Json_Expr Component Proposal
| Proposed Component Name | Zend_Json_Expr |
|---|---|
| Developer Notes | http://framework.zend.com/wiki/display/ZFDEV/Zend_Json_Expr |
| Proposers | Oscar Reales |
| Zend Liaison | Matthew Weier O'Phinney |
| Revision | 1.0 - 10 August 2008: Initial Draft. (wiki revision: 12) |
Table of Contents
1. Overview
Add the possibility of including functions and native javascripts expressions to be encoded by Zend_Json, generating right syntax in the outputted json. The main idea is follow the Zend_Db_Expr pattern, so to include a Javascript Expression to be encoded in an object/array, will be enough to uses something similar to Zend_Json_Expr('javascript expression').
2. References
3. Component Requirements, Constraints, and Acceptance Criteria
- This component will allow include Functions and Javascript Expressions in arrays/object to be encoded with Zend_Json
- This component will require the creation of a new Zend_Json_Expr class, very similar to Zend_Db_Expr
- This component will need to re-code the Zend_Json encoding proccess and includes 2 private properties to the class that will hold some values temporarily during the encoding proccess
- This component will be functional with Array/Objects to be encoded. Encoding from XML will need to be handle maybe in other way
4. Dependencies on Other Framework Components
- Zend_Json
5. Theory of Operation
To include a native Javascript Expression in a PHP Array/Object will be required to use Zend_Json_Expr class, with the following syntax: Zend_Json_Expr('javascript expression').
This Zend_Json_Expr class will be used to find and temporarily replace pre-encoding proccess all the javascript expressions we want to encode.
The value to be encoded in json format will be pre-encoding parsed and all finded Zend_Json_Expr will be temporarily keeped in an array. Each matched Zend_Json_Expr will be replaced by a single id. In this way the encoding can be runned without errors. After encoding proccess finish, it is required to parse the result in the inverse way: replacing the single temporary "ids" by their corresponding original Javascript Expressions.
Because the result it is already a String after the encoding, it is possible to includes the original Javascript Expressions without "'".
6. Milestones / Tasks
- Milestone 1: [DONE] Create the classes skeleton
- Milestone 2: [DONE] Working prototype
- Milestone 3: [DONE] Review code to follow ZF coding conventions
- Milestone 4: Create - Modify Unit Testing
- Milestone 5: Documentation
7. Class Index
- Zend_Json_Expr
- Zend_Json
8. Use Cases
| UC-01 |
|---|
This is a sample of using the Zend_Json_Expr class in a controller that will returns a json encoded object with one simple javascript function:
<?php
class JsonUseCase extends Zend_Controller_Action
{
function indexAction()
{
Zend_Loader::loadClass('Zend_Json_Expr');
$encodedResult = array(
'integer'=>9,
'string'=>'test string',
'function'=>Zend_Json_Expr('function(){window.alert("javascript function encoded by Zend_Json")}')
);
$this->_helper->json->sendJson($resultado);
//it will returns json encoded string:
//{"integer":9,"string":"test string","function":function(){window.alert("javascript function encoded by Zend_Json")}}
}
}
9. Class Skeletons
Thanks Benjamin. It is my first proposal and I am not very "fluent" with the proccess. I want to "clean" the code and adjust it to the "coding standards" before move it for review. I am needing some help with the PHP Unit Testing because I never used it before. If someone can helps me on that would be good.
Anyway I will do an effort to place it for review asap.
Thanks again for your comments.
ZF Home Page
Code Browser
Wiki Dashboard
This is a great proposal. This is a great addition that could be perfectly integrated into my jQuery proposal (or any other javascript helper related helper/library).
I'd support you in pushing this through for 1.7
btw, you should move the proposal into ready for review section, since you already posted it on the mailinglist also.