-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework JSON Serializer to Throw Useful Exceptions
Closes #175 Previously `SmartSerializer` was the only class that sometimes threw exceptions when something went wrong during JSON deserialization. This changes that so all serializers throw. - `JsonSerializationError` is thrown when something goes wrong during `json_encode` - `JsonDeserializationError` is thrown when something goes wrong during `json_decode` Both are subclasses of `JsonErrorException`, so anyone already catching that should be able to continue doing so. There's a also a bit of restructuring here and some additional tests to cover some more paths in the serializers.
- Loading branch information
1 parent
a273a95
commit 850240a
Showing
9 changed files
with
190 additions
and
103 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/Elasticsearch/Common/Exceptions/Serializer/JsonDeserializationError.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Elasticsearch\Common\Exceptions\Serializer; | ||
|
||
/** | ||
* Thrown when there's an error unserializing a JSON string to a PHP array. | ||
* | ||
* @author Christopher Davis <[email protected]> | ||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 | ||
* @link http://elasticsearch.org | ||
*/ | ||
final class JsonDeserializationError extends JsonErrorException | ||
{ | ||
// noop | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Elasticsearch/Common/Exceptions/Serializer/JsonSerializationError.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Elasticsearch\Common\Exceptions\Serializer; | ||
|
||
/** | ||
* Thrown when there's an error serialization a PHP object or array to a JSON | ||
* string. | ||
* | ||
* @author Christopher Davis <[email protected]> | ||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 | ||
* @link http://elasticsearch.org | ||
*/ | ||
final class JsonSerializationError extends JsonErrorException | ||
{ | ||
// noop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Elasticsearch\Serializers; | ||
|
||
use Elasticsearch\Common\Exceptions\Serializer\JsonSerializationError; | ||
use Elasticsearch\Common\Exceptions\Serializer\JsonDeserializationError; | ||
|
||
/** | ||
* An abstract base class for serializers that go to/from JSON. | ||
* | ||
* @author Christopher Davis <[email protected]> | ||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 | ||
* @link http://elasticsearch.org | ||
*/ | ||
abstract class AbstractJsonSerializer implements SerializerInterface | ||
{ | ||
/** | ||
* Encode a php object or array to a JSON string | ||
* | ||
* @param object|array $value | ||
* @throws JsonSerializationError if something goes wrong during encoding | ||
* @return string | ||
*/ | ||
protected static function jsonEncode($value) | ||
{ | ||
$result = json_encode($value); | ||
|
||
if (static::hasJsonError()) { | ||
throw new JsonSerializationError(json_last_error(), $value, $result); | ||
} | ||
|
||
return '[]' === $result ? '{}' : $result; | ||
} | ||
|
||
/** | ||
* Decode a JSON string to a PHP array. | ||
* | ||
* @param string $json | ||
* @throws JsonDeserializationError if something goes wrong during decoding | ||
* @return array | ||
*/ | ||
protected static function jsonDecode($json) | ||
{ | ||
$result = json_decode($json, true); | ||
|
||
if (static::hasJsonError()) { | ||
throw new JsonDeserializationError(json_last_error(), $json, $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Check to see if the last `json_{encode,decode}` call produced an error. | ||
* | ||
* @return boolean | ||
*/ | ||
protected static function hasJsonError() | ||
{ | ||
return json_last_error() !== JSON_ERROR_NONE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.