Skip to content

Commit

Permalink
Merge branch 'ZF-8618' of http://github.com/marc-mabe/zf2 into merges…
Browse files Browse the repository at this point in the history
…/mabe-zf8618
  • Loading branch information
weierophinney committed Nov 2, 2010
2 parents a2f0e7c + f7858ef commit 5417a4a
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion library/Zend/Json/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function __construct($source, $decodeType)
* {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
* @return mixed
*/
public static function decode($source, $objectDecodeType = Json::TYPE_ARRAY)
public static function decode($source, $objectDecodeType = Json::TYPE_OBJECT)
{
$decoder = new self($source, $objectDecodeType);
return $decoder->_decodeValue();
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class JSON
* @return mixed
* @throws Zend\Json\Exception\RuntimeException
*/
public static function decode($encodedValue, $objectDecodeType = self::TYPE_ARRAY)
public static function decode($encodedValue, $objectDecodeType = self::TYPE_OBJECT)
{
$encodedValue = (string) $encodedValue;
if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Json/Server/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getVersion()
*/
public function loadJson($json)
{
$options = Json\Json::decode($json);
$options = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
$this->setOptions($options);
}

Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Json/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @namespace
*/
namespace Zend\Json\Server;
use Zend\Server\Reflection,
use Zend\Server\Reflection\Reflection,
Zend\Server\Method;

/**
Expand Down
44 changes: 37 additions & 7 deletions tests/Zend/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function testObject()
$array = array('__className' => 'stdClass', 'one' => 1, 'two' => 2);

$encoded = Json\Encoder::encode($value);
$this->assertSame($array, Json\Decoder::decode($encoded));
$this->assertSame($array, Json\Decoder::decode($encoded, Json\Json::TYPE_ARRAY));
}

/**
Expand All @@ -242,7 +242,7 @@ public function testDecodeArrayOfObjects()
{
$value = '[{"id":1},{"foo":2}]';
$expect = array(array('id' => 1), array('foo' => 2));
$this->assertEquals($expect, Json\Decoder::decode($value));
$this->assertEquals($expect, Json\Decoder::decode($value, Json\Json::TYPE_ARRAY));
}

/**
Expand All @@ -260,7 +260,7 @@ public function testDecodeObjectOfArrays()
346 => array(64, 'francois'),
21 => array(12, 'paul')
);
$this->assertEquals($expect, Json\Decoder::decode($value));
$this->assertEquals($expect, Json\Decoder::decode($value, Json\Json::TYPE_ARRAY));
}

/**
Expand All @@ -271,10 +271,28 @@ protected function _testEncodeDecode($values)
{
foreach ($values as $value) {
$encoded = Json\Encoder::encode($value);
$this->assertEquals($value, Json\Decoder::decode($encoded));

if (is_array($value) || is_object($value)) {
$this->assertEquals($this->_toArray($value), Json\Decoder::decode($encoded, Json\Json::TYPE_ARRAY));
} else {
$this->assertEquals($value, Json\Decoder::decode($encoded));
}
}
}

protected function _toArray($value)
{
if (!is_array($value) || !is_object($value)) {
return $value;
}

$array = array();
foreach ((array)$value as $k => $v) {
$array[$k] = $this->_toArray($v);
}
return $array;
}

/**
* Test that version numbers such as 4.10 are encoded and decoded properly;
* See ZF-377
Expand All @@ -295,10 +313,10 @@ public function testEarlyLineBreak()
$expected = array('data' => array(1, 2, 3, 4));

$json = '{"data":[1,2,3,4' . "\n]}";
$this->assertEquals($expected, Json\Decoder::decode($json));
$this->assertEquals($expected, Json\Decoder::decode($json, Json\Json::TYPE_ARRAY));

$json = '{"data":[1,2,3,4 ]}';
$this->assertEquals($expected, Json\Decoder::decode($json));
$this->assertEquals($expected, Json\Decoder::decode($json, Json\Json::TYPE_ARRAY));
}

/**
Expand Down Expand Up @@ -732,8 +750,9 @@ public function testDecodingInvalidJSONShouldRaiseAnException()
}

/**
* @group ZF-9416
* Encoding an iterator using the internal encoder should handle undefined keys
*
* @group ZF-9416
*/
public function testIteratorWithoutDefinedKey()
{
Expand All @@ -742,6 +761,17 @@ public function testIteratorWithoutDefinedKey()
$expectedDecoding = '{"__className":"ArrayIterator",0:"foo"}';
$this->assertEquals($expectedDecoding, $encoded);
}

/**
* The default json decode type should be TYPE_OBJECT
*
* @group ZF-8618
*/
public function testDefaultTypeObject()
{
$this->assertType('stdClass', Json\Decoder::decode('{"var":"value"}'));
}

}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Zend/Json/JsonXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testUsingXML1()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 1 is NULL");
// Test for one of the expected fields in the JSON result.
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testUsingXML2()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 2 is NULL");
// Test for one of the expected fields in the JSON result.
Expand Down Expand Up @@ -238,7 +238,7 @@ public function testUsingXML3()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 3 is NULL");
// Test for one of the expected fields in the JSON result.
Expand Down Expand Up @@ -344,7 +344,7 @@ public function testUsingXML4()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 4 is NULL");
// Test for one of the expected fields in the JSON result.
Expand Down Expand Up @@ -389,7 +389,7 @@ public function testUsingXML5()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 5 is NULL");
// Test for one of the expected CDATA fields in the JSON result.
Expand Down Expand Up @@ -464,7 +464,7 @@ public function testUsingXML6()
$jsonContents = Json\Json::fromXml($xmlStringContents, $ignoreXmlAttributes);

// Convert the JSON string into a PHP array.
$phpArray = Json\Json::decode($jsonContents);
$phpArray = Json\Json::decode($jsonContents, Json\Json::TYPE_ARRAY);
// Test if it is not a NULL object.
$this->assertNotNull($phpArray, "JSON result for XML input 6 is NULL");
// Test for one of the expected fields in the JSON result.
Expand Down
4 changes: 2 additions & 2 deletions tests/Zend/Json/Server/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ public function testShouldBeAbleToCastToJSON()
{
$this->setupError();
$json = $this->error->toJSON();
$this->validateArray(Json\Json::decode($json));
$this->validateArray(Json\Json::decode($json, Json\Json::TYPE_ARRAY));
}

public function testCastingToStringShouldCastToJSON()
{
$this->setupError();
$json = $this->error->__toString();
$this->validateArray(Json\Json::decode($json));
$this->validateArray(Json\Json::decode($json, Json\Json::TYPE_ARRAY));
}

public function setupError()
Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Json/Server/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function getOptions()

public function validateJSON($json, array $options)
{
$test = Json\Json::decode($json);
$test = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
$this->assertTrue(is_array($test), var_export($json, 1));

$this->assertTrue(array_key_exists('id', $test));
Expand Down
6 changes: 3 additions & 3 deletions tests/Zend/Json/Server/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testResponseShouldBeAbleToCastToJSON()
->setId('foo')
->setVersion('2.0');
$json = $this->response->toJSON();
$test = Json\Json::decode($json);
$test = Json\Json::decode($json, Json\Json::TYPE_ARRAY);

$this->assertTrue(is_array($test));
$this->assertTrue(array_key_exists('result', $test));
Expand All @@ -136,7 +136,7 @@ public function testResponseShouldCastErrorToJSONIfIsError()
->setResult(true)
->setError($error);
$json = $this->response->toJSON();
$test = Json\Json::decode($json);
$test = Json\Json::decode($json, Json\Json::TYPE_ARRAY);

$this->assertTrue(is_array($test));
$this->assertTrue(array_key_exists('result', $test));
Expand All @@ -153,7 +153,7 @@ public function testCastToStringShouldCastToJSON()
$this->response->setResult(true)
->setId('foo');
$json = $this->response->__toString();
$test = Json\Json::decode($json);
$test = Json\Json::decode($json, Json\Json::TYPE_ARRAY);

$this->assertTrue(is_array($test));
$this->assertTrue(array_key_exists('result', $test));
Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Json/Server/Smd/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function testTojsonShouldEmitJSON()
{
$this->setupSmdValidationObject();
$json = $this->service->toJSON();
$smd = \Zend\Json\Json::decode($json);
$smd = \Zend\Json\Json::decode($json, \Zend\Json\Json::TYPE_ARRAY);

$this->assertTrue(array_key_exists('foo', $smd));
$this->assertTrue(is_array($smd['foo']));
Expand Down
4 changes: 2 additions & 2 deletions tests/Zend/Json/Server/SmdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function testShouldBeAbleToRenderAsJSON()
$options = $this->getOptions();
$this->smd->setOptions($options);
$json = $this->smd->toJSON();
$smd = Json\Json::decode($json);
$smd = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
$this->validateServiceArray($smd, $options);
}

Expand All @@ -349,7 +349,7 @@ public function testToStringImplementationShouldProxyToJSON()
$options = $this->getOptions();
$this->smd->setOptions($options);
$json = $this->smd->__toString();
$smd = Json\Json::decode($json);
$smd = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
$this->validateServiceArray($smd, $options);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Json/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function testHandleShouldEmitResponseByDefault()
$this->server->handle();
$buffer = ob_get_clean();

$decoded = Json\Json::decode($buffer);
$decoded = Json\Json::decode($buffer, Json\Json::TYPE_ARRAY);
$this->assertTrue(is_array($decoded));
$this->assertTrue(array_key_exists('result', $decoded));
$this->assertTrue(array_key_exists('id', $decoded));
Expand Down

0 comments on commit 5417a4a

Please sign in to comment.