Zend_Json-Objects.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.json.advanced">
  4. <title>Advanced Usage of Zend_Json</title>
  5. <sect2 id="zend.json.advanced.objects1">
  6. <title>JSON Objects</title>
  7. <para>
  8. When encoding <acronym>PHP</acronym> objects as <acronym>JSON</acronym>, all public
  9. properties of that object will be encoded in a <acronym>JSON</acronym> object.
  10. </para>
  11. <para>
  12. <acronym>JSON</acronym> does not allow object references, so care should be taken not to
  13. encode objects with recursive references. If you have issues with
  14. recursion, <methodname>Zend_Json::encode()</methodname> and
  15. <methodname>Zend_Json_Encoder::encode()</methodname> allow an optional second
  16. parameter to check for recursion; if an object is serialized twice, an
  17. exception will be thrown.
  18. </para>
  19. <para>
  20. Decoding <acronym>JSON</acronym> objects poses an additional difficulty, however, since
  21. Javascript objects correspond most closely to <acronym>PHP</acronym>'s associative
  22. array. Some suggest that a class identifier should be passed, and an object
  23. instance of that class should be created and populated with the
  24. key/value pairs of the <acronym>JSON</acronym> object; others feel this could pose a
  25. substantial security risk.
  26. </para>
  27. <para>
  28. By default, <classname>Zend_Json</classname> will decode <acronym>JSON</acronym> objects
  29. as associative arrays. However, if you desire an object returned, you can specify this:
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. // Decode JSON objects as PHP objects
  33. $phpNative = Zend_Json::decode($encodedValue, Zend_Json::TYPE_OBJECT);
  34. ]]></programlisting>
  35. <para>
  36. Any objects thus decoded are returned as <classname>StdClass</classname> objects
  37. with properties corresponding to the key/value pairs in the <acronym>JSON</acronym>
  38. notation.
  39. </para>
  40. <para>
  41. The recommendation of Zend Framework is that the individual
  42. developer should decide how to decode <acronym>JSON</acronym> objects. If an object of a
  43. specified type should be created, it can be created in the developer
  44. code and populated with the values decoded using <classname>Zend_Json</classname>.
  45. </para>
  46. </sect2>
  47. <sect2 id="zend.json.advanced.objects2">
  48. <title>Encoding PHP objects</title>
  49. <para>
  50. If you are encoding <acronym>PHP</acronym> objects by default the encoding mechanism
  51. can only access public properties of these objects. When a method
  52. <methodname>toJson()</methodname> is implemented on an object to encode,
  53. <classname>Zend_Json</classname> calls this method and expects the object to return a
  54. <acronym>JSON</acronym> representation of its internal state.
  55. </para>
  56. </sect2>
  57. <sect2 id="zend.json.advanced.internal">
  58. <title>Internal Encoder/Decoder</title>
  59. <para>
  60. <classname>Zend_Json</classname> has two different modes depending if ext/json is
  61. enabled in your <acronym>PHP</acronym> installation or not. If ext/json is installed by
  62. default <methodname>json_encode()</methodname> and
  63. <methodname>json_decode()</methodname> functions are used for encoding and decoding
  64. <acronym>JSON</acronym>. If ext/json is not installed a Zend Framework implementation in
  65. <acronym>PHP</acronym> code is used for en-/decoding. This is considerably slower than
  66. using the <acronym>PHP</acronym> extension, but behaves exactly the same.
  67. </para>
  68. <para>
  69. Still sometimes you might want to use the internal encoder/decoder even
  70. if you have ext/json installed. You can achieve this by calling:
  71. </para>
  72. <programlisting language="php"><![CDATA[
  73. Zend_Json::$useBuiltinEncoderDecoder = true:
  74. ]]></programlisting>
  75. </sect2>
  76. <sect2 id="zend.json.advanced.expr">
  77. <title>JSON Expressions</title>
  78. <para>
  79. Javascript makes heavy use of anonymnous function callbacks, which can be saved
  80. within <acronym>JSON</acronym> object variables. Still they only work if not
  81. returned inside double qoutes, which <classname>Zend_Json</classname> naturally does.
  82. With the Expression support for <classname>Zend_Json</classname> support you can encode
  83. <acronym>JSON</acronym> objects with valid javascript callbacks. This works for both
  84. <methodname>json_encode()</methodname> or the internal encoder.
  85. </para>
  86. <para>
  87. A javascript callback is represented using the <classname>Zend_Json_Expr</classname>
  88. object. It implements the value object pattern and is immutable. You can set the
  89. javascript expression as the first constructor argument. By default
  90. <classname>Zend_Json::encode</classname> does not encode javascript callbacks, you have
  91. to pass the option <property>enableJsonExprFinder</property> and set it to
  92. <constant>TRUE</constant> into the <methodname>encode()</methodname> function. If
  93. enabled the expression support works for all nested expressions in large object
  94. structures. A usage example would look like:
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. $data = array(
  98. 'onClick' => new Zend_Json_Expr('function() {'
  99. . 'alert("I am a valid javascript callback '
  100. . 'created by Zend_Json"); }'),
  101. 'other' => 'no expression',
  102. );
  103. $jsonObjectWithExpression = Zend_Json::encode(
  104. $data,
  105. false,
  106. array('enableJsonExprFinder' => true)
  107. );
  108. ]]></programlisting>
  109. </sect2>
  110. </sect1>
  111. <!--
  112. vim:se ts=4 sw=4 et:
  113. -->