ZF-8618: Zend_Json_Decoder returns incompatible return types compared to ext/json
Description
Zend_Json_Decoder (the native alternative to ext/json) returns types which are incompatible with ext/json, thus for the same JSON input ext/json may emit a stdClass object whereas Zend_Json_Decoder emits an Array instead. Given the access differences this incompatibility may break applications moving from an ext/json system to one (e.g. CentOS) where ext/json is not available and Zend_Json_Decoder is utilised instead.
Reproduce code:
'));
var_dump(Zend_Json_Decoder::decode('{"zero": 2}'));
Expected:
object(stdClass)#1 (1) { ["zero"]=> int(2) } object(stdClass)#1 (1) { ["zero"]=> int(2) }
Actual:
object(stdClass)#1 (1) { ["zero"]=> int(2) } array(1) { ["zero"]=> int(2) }
Comments
Posted by Jair Henrique de Carvalho Souza (jairhenrique) on 2009-12-29T12:11:02.000+0000
you must pass the second parameter of the function.
var_dump(Zend_Json_Decoder::decode('{"zero": 2}',TYPE_OBJECT));
Posted by Pádraic Brady (padraic) on 2009-12-29T13:29:10.000+0000
ext/json doesn't require the extra parameter - so it is an incompatible behaviour ;).
Posted by Joel Clermont (jclermont) on 2010-05-20T18:54:35.000+0000
json_decode defaults to returning an object of type stdClass, but Zend_Json::decode defaults to returning an array whether it uses json_decode or its own internal decoder. I can see how this is confusing and inconsistent. However, if we change the default behavior, wouldn't that break backwards compatibility? This behavior has been in place since they added support for ext/json. Thoughts?
Posted by Chris Boyce (chrisboyce) on 2010-08-30T19:40:21.000+0000
Perhaps in ZF2 the default could change? I can't see a way to change the default behavior without breaking backwards compatibility.
Posted by Marc Bennewitz (private) (mabe) on 2010-11-03T00:40:37.000+0000
fixed on zf2 branch: https://github.com/zendframework/zf2/…