Zend_Json-Objects.xml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 properties of that object
  9. 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 array.
  22. 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 as
  29. associative arrays. However, if you desire an object returned, you can
  30. specify this:
  31. </para>
  32. <programlisting language="php"><![CDATA[
  33. // Decode JSON objects as PHP objects
  34. $phpNative = Zend_Json::decode($encodedValue, Zend_Json::TYPE_OBJECT);
  35. ]]></programlisting>
  36. <para>
  37. Any objects thus decoded are returned as <code>StdClass</code> objects
  38. with properties corresponding to the key/value pairs in the <acronym>JSON</acronym>
  39. notation.
  40. </para>
  41. <para>
  42. The recommendation of Zend Framework is that the individual
  43. developer should decide how to decode <acronym>JSON</acronym> objects. If an object of a
  44. specified type should be created, it can be created in the developer
  45. code and populated with the values decoded using <classname>Zend_Json</classname>.
  46. </para>
  47. </sect2>
  48. <sect2 id="zend.json.advanced.objects2">
  49. <title>Encoding PHP objects</title>
  50. <para>
  51. If you are encoding <acronym>PHP</acronym> objects by default the encoding mechanism
  52. can only access public properties of these objects. When a method
  53. <methodname>toJson()</methodname> is implemented on an object to encode, <classname>Zend_Json</classname>
  54. calls this method and expects the object to return a <acronym>JSON</acronym> representation
  55. of its internal state.
  56. </para>
  57. </sect2>
  58. <sect2 id="zend.json.advanced.internal">
  59. <title>Internal Encoder/Decoder</title>
  60. <para>
  61. <classname>Zend_Json</classname> has two different modes depending if ext/json is enabled in
  62. your <acronym>PHP</acronym> installation or not. If ext/json is installed by default
  63. <methodname>json_encode()</methodname> and <methodname>json_decode()</methodname> functions
  64. are used for encoding and decoding <acronym>JSON</acronym>. If ext/json is not installed
  65. a Zend Framework implementation in <acronym>PHP</acronym> code is used for en-/decoding.
  66. This is considerably slower than using the php extension, but behaves
  67. exactly the same.
  68. </para>
  69. <para>
  70. Still sometimes you might want to use the internal encoder/decoder even
  71. if you have ext/json installed. You can achieve this by calling:
  72. </para>
  73. <programlisting language="php"><![CDATA[
  74. Zend_Json::$useBuiltinEncoderDecoder = true:
  75. ]]></programlisting>
  76. </sect2>
  77. <sect2 id="zend.json.advanced.expr">
  78. <title>JSON Expressions</title>
  79. <para>
  80. Javascript makes heavy use of anonymnous function callbacks, which
  81. can be saved within <acronym>JSON</acronym> object variables. Still they only work if not
  82. returned inside double qoutes, which <classname>Zend_Json</classname> naturally does.
  83. With the Expression support for <classname>Zend_Json</classname> support you can encode <acronym>JSON</acronym>
  84. objects with valid javascript callbacks. This works for both <methodname>json_encode()</methodname>
  85. or the internal encoder.
  86. </para>
  87. <para>
  88. A javascript callback is represented using the <classname>Zend_Json_Expr</classname> object.
  89. It implements the value object pattern and is immutable. You can set the javascript
  90. expression as the first constructor argument. By default <classname>Zend_Json::encode</classname>
  91. does not encode javascript callbacks, you have to pass the option <code>'enableJsonExprFinder' = true</code>
  92. into the <code>encode</code> function. If enabled the expression support works for all nested expressions
  93. in large object structures. A usage example would look like:
  94. </para>
  95. <programlisting language="php"><![CDATA[
  96. $data = array(
  97. 'onClick' => new Zend_Json_Expr('function() {'
  98. . 'alert("I am a valid javascript callback '
  99. . 'created by Zend_Json"); }'),
  100. 'other' => 'no expression',
  101. );
  102. $jsonObjectWithExpression = Zend_Json::encode(
  103. $data,
  104. false,
  105. array('enableJsonExprFinder' => true)
  106. );
  107. ]]></programlisting>
  108. </sect2>
  109. </sect1>
  110. <!--
  111. vim:se ts=4 sw=4 et:
  112. -->