Skip to content

Commit

Permalink
InvalidArgumentException for Encoder & Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Sep 30, 2010
1 parent e37523b commit 288c56c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
43 changes: 18 additions & 25 deletions library/Zend/Json/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@
*/
class Decoder
{

/**
* Parse tokens used to decode the JSON object. These are not
* for public consumption, they are just used internally to the
* class.
*/
const EOF = 0;
const DATUM = 1;
const EOF = 0;
const DATUM = 1;
const LBRACE = 2;
const LBRACKET = 3;
const RBRACE = 4;
const RBRACKET = 5;
const COMMA = 6;
const COLON = 7;
const LBRACKET = 3;
const RBRACE = 4;
const RBRACKET = 5;
const COMMA = 6;
const COLON = 7;

/**
* Use to maintain a "pointer" to the source being decoded
Expand Down Expand Up @@ -101,22 +102,20 @@ class Decoder
*/
protected function __construct($source, $decodeType)
{
if (!is_string($source)) {
throw new InvalidArgumentException('Can only decode JSON encoded strings');
}

// Set defaults
$this->_source = self::decodeUnicodeString($source);
$this->_sourceLength = strlen($this->_source);
$this->_token = self::EOF;
$this->_offset = 0;

// Normalize and set $decodeType
if (!in_array($decodeType, array(Json::TYPE_ARRAY, Json::TYPE_OBJECT)))
{
$decodeType = Json::TYPE_ARRAY;
switch ($decodeType) {
case Json::TYPE_ARRAY:
case Json::TYPE_OBJECT:
$this->_decodeType = $decodeType;
break;
default:
throw new InvalidArgumentException("Unknown decode type '{$decodeType}', please use one of the constants Json::TYPE_*");
}
$this->_decodeType = $decodeType;

// Set pointer at first token
$this->_getNextToken();
Expand Down Expand Up @@ -146,19 +145,13 @@ protected function __construct($source, $decodeType)
* either or {@link Zend_Json::TYPE_ARRAY} or
* {@link Zend_Json::TYPE_OBJECT}; defaults to TYPE_ARRAY
* @return mixed
* @throws Zend\Json\Exception\JsonException if the source string is null
*/
public static function decode($source = null, $objectDecodeType = Json::TYPE_ARRAY)
public static function decode($source, $objectDecodeType = Json::TYPE_ARRAY)
{
if (null === $source) {
throw new JsonException('Must specify JSON encoded source for decoding');
}

$decoder = new self($source, $objectDecodeType);
return $decoder->_decodeValue();
}


/**
* Recursive driving rountine for supported toplevel tops
*
Expand Down Expand Up @@ -279,7 +272,7 @@ protected function _decodeArray()
}

$this->_getNextToken();
return($result);
return $result;
}


Expand Down Expand Up @@ -454,7 +447,7 @@ protected function _getNextToken()
throw new JsonException('Illegal Token');
}

return($this->_token);
return $this->_token;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions library/Zend/Json/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*/
namespace Zend\Json;

use Zend\Json\Exception\JsonException;
use Zend\Json\Exception\JsonException,
Zend\Json\Exception\InvalidArgumentException;

/**
* Encode PHP constructs to JSON
Expand Down Expand Up @@ -410,7 +411,7 @@ public static function encodeClass($className, $package = '')
{
$cls = new \ReflectionClass($className);
if (! $cls->isInstantiable()) {
throw new JsonException("$className must be instantiable");
throw new InvalidArgumentException("'{$className}' must be instantiable");
}

return "Class.create('$package$className',{"
Expand Down

0 comments on commit 288c56c

Please sign in to comment.