10.3. JSON Objects

When encoding PHP objects as JSON, all public properties of that object will be encoded in a JSON object.

Decoding JSON objects poses a slight difficulty, however, since Javascript objects correspond most closesly to PHP's associative array. Some suggest that a class identifier should be passed, and an object instance of that class should be created and populated with the key/value pairs of the JSON object; others feel this could pose a substantial security risk.

By default, Zend_Json will decode JSON objects as associative arrays. However, if you desire an object returned, you can specify this:

<?php
// Decode objects as objects
$phpNative = Zend_Json::decode($encodedValue, Zend_Json::TYPE_OBJECT);
?>

Any objects thus decoded are returned as StdClass objects with properties corresponding to the key/value pairs in the JSON notation.

The recommendation of the Zend Framework is that the individual developer should decide how to decode JSON objects. If an object of a specified type should be created, it can be created in the developer code and populated with the values decoded using Zend_Json.