Skip to content

Commit

Permalink
Merge pull request #614 from idio/feature/json-compat
Browse files Browse the repository at this point in the history
JSON Compat Library
  • Loading branch information
ruflin committed May 15, 2014
2 parents 181ae4f + ac09e7c commit 58b0928
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 32 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-05-13
- Add JSON compat library; Elasticsearch JSON flags and nicer error handling

2014-05-12
- Update dev builds to phpunit 4.1.*

Expand Down
10 changes: 3 additions & 7 deletions lib/Elastica/Bulk/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace Elastica\Bulk;

if (!defined('JSON_UNESCAPED_UNICODE')) {
define('JSON_UNESCAPED_SLASHES', 64);
define('JSON_UNESCAPED_UNICODE', 256);
}

use Elastica\Bulk;
use Elastica\JSON;
use Elastica\Index;
use Elastica\Type;

Expand Down Expand Up @@ -185,15 +181,15 @@ public function toArray()
*/
public function toString()
{
$string = json_encode($this->getActionMetadata(), JSON_FORCE_OBJECT) . Bulk::DELIMITER;
$string = JSON::stringify($this->getActionMetadata(), JSON_FORCE_OBJECT) . Bulk::DELIMITER;
if ($this->hasSource()) {
$source = $this->getSource();
if (is_string($source)) {
$string.= $source;
} elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) {
$string.= '{"doc": ' . $source['doc'] . '}';
} else {
$string.= json_encode($source, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$string.= JSON::stringify($source, 'JSON_ELASTICSEARCH');
}
$string.= Bulk::DELIMITER;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Elastica/Exception/JSONParseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Elastica\Exception;

/**
* JSON Parse exception
*
* @package Elastica
*/
class JSONParseException extends \RuntimeException implements ExceptionInterface
{
}
3 changes: 2 additions & 1 deletion lib/Elastica/Exception/PartialShardFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica\Exception;

use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand All @@ -26,7 +27,7 @@ public function __construct(Request $request, Response $response)
parent::__construct($request, $response);

$shardsStatistics = $response->getShardsStatistics();
$this->message = json_encode($shardsStatistics['failed']);
$this->message = JSON::stringify($shardsStatistics['failed']);
}

}
67 changes: 67 additions & 0 deletions lib/Elastica/JSON.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Elastica;

use Elastica\Exception\JSONParseException;

/**
* Elastica JSON tools
*
* @package Elastica
*/
class JSON
{
/**
* Parse JSON string to an array
*
* @param string $json JSON string to parse
* @return array PHP array representation of JSON string
* @link http://php.net/manual/en/function.json-decode.php
* @link http://www.php.net/manual/en/function.json-last-error.php
*/
public static function parse(/* inherit from json_decode */)
{
// extract arguments
$args = func_get_args();

// default to decoding into an assoc array
if (sizeof($args) === 1) {
$args[] = true;
}

// run decode
$array = call_user_func_array('json_decode', $args);

// turn errors into exceptions for easier catching
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new JSONParseException($error);
}

// output
return $array;
}

/**
* Convert input to JSON string with standard options
*
* @param mixed check args for PHP function json_encode
* @return string Valid JSON representation of $input
* @link http://php.net/manual/en/function.json-encode.php
*/
public static function stringify(/* inherit from json_encode */)
{
// extract arguments
$args = func_get_args();

// allow special options value for Elasticsearch compatibility
if (sizeof($args) > 1 && $args[1] === 'JSON_ELASTICSEARCH') {
// Use built in JSON constants if available (php >= 5.4)
$args[1] = (defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_SLASHES : 64)
| (defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 256);
}

// run encode and output
return call_user_func_array('json_encode', $args);
}
}
3 changes: 2 additions & 1 deletion lib/Elastica/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica;

use Elastica\JSON;
use Psr\Log\AbstractLogger;

/**
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct($log = '')
public function log($level, $message, array $context = array())
{
$context['error_message'] = $message;
$this->_lastMessage = json_encode($context);
$this->_lastMessage = JSON::stringify($context);

if (!empty($this->_log) && is_string($this->_log)) {
error_log($this->_lastMessage . PHP_EOL, 3, $this->_log);
Expand Down
5 changes: 3 additions & 2 deletions lib/Elastica/Multi/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elastica\Multi;

use Elastica\Client;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Search as BaseSearch;

Expand Down Expand Up @@ -170,8 +171,8 @@ protected function _getSearchData(BaseSearch $search)
$header = (empty($header)) ? new \stdClass : $header;
$query = $search->getQuery();

$data = json_encode($header) . "\n";
$data.= json_encode($query->toArray()) . "\n";
$data = JSON::stringify($header) . "\n";
$data.= JSON::stringify($query->toArray()) . "\n";

return $data;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Elastica/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace Elastica\Query;

use Elastica\Exception\InvalidException;
use Elastica\Exception\JSONParseException;
use Elastica\JSON;

/**
* Query Builder.
Expand Down Expand Up @@ -59,13 +62,11 @@ public function __toString()
*/
public function toArray()
{
$array = json_decode($this->__toString(), true);

if (is_null($array)) {
try {
return JSON::parse($this->__toString());
} catch (JSONParseException $e) {
throw new InvalidException('The query produced is invalid');
}

return $array;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/Elastica/Request.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace Elastica;

use Elastica\Exception\InvalidException;
use Elastica\JSON;

/**
* Elastica Request object
Expand Down Expand Up @@ -185,7 +187,7 @@ public function toArray()
*/
public function toString()
{
return json_encode($this->toArray());
return JSON::stringify($this->toArray());
}

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/Elastica/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Elastica;

use Elastica\Exception\JSONParseException;
use Elastica\Exception\NotFoundException;
use Elastica\JSON;

/**
* Elastica Response object
Expand Down Expand Up @@ -177,11 +179,10 @@ public function getData()
if ($response === false) {
$this->_error = true;
} else {

$tempResponse = json_decode($response, true);
// Check if decoding went as expected. If error is returned, json_decode makes empty string of string
if (json_last_error() == JSON_ERROR_NONE) {
$response = $tempResponse;
try {
$response = JSON::parse($response);
} catch (JSONParseException $e) {
// leave reponse as is if parse fails
}
}

Expand Down
8 changes: 2 additions & 6 deletions lib/Elastica/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Elastica\Transport;

if (!defined('JSON_UNESCAPED_UNICODE')) {
define('JSON_UNESCAPED_SLASHES', 64);
define('JSON_UNESCAPED_UNICODE', 256);
}

use Elastica\Exception\Connection\HttpException;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand Down Expand Up @@ -103,7 +99,7 @@ public function exec(Request $request, array $params)
}

if (is_array($data)) {
$content = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$content = JSON::stringify($data, 'JSON_ELASTICSEARCH');
} else {
$content = $data;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Elastica/Transport/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Elastica\Exception\InvalidException;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand Down Expand Up @@ -40,7 +41,7 @@ public function exec(Request $request, array $params)

if (!empty($data)) {
if (is_array($data)) {
$content = json_encode($data);
$content = JSON::stringify($data);
} else {
$content = $data;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Elastica/Transport/Null.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica\Transport;

use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand Down Expand Up @@ -38,6 +39,6 @@ public function exec(Request $request, array $params)
"params" => $params
);

return new Response(json_encode($response));
return new Response(JSON::stringify($response));
}
}
3 changes: 2 additions & 1 deletion lib/Elastica/Transport/Thrift.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\Exception\RuntimeException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;
use Elastica\Connection;
Expand Down Expand Up @@ -136,7 +137,7 @@ public function exec(Request $request, array $params)
$data = $request->getData();
if (!empty($data)) {
if (is_array($data)) {
$content = json_encode($data);
$content = JSON::stringify($data);
} else {
$content = $data;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Elastica/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elastica;

use Elastica\JSON;

/**
* Elastica tools
*
Expand Down Expand Up @@ -162,7 +164,7 @@ public static function convertRequestToCurlCommand(Request $request)

$data = $request->getData();
if (!empty($data)) {
$message .= ' -d \'' . json_encode($data) . '\'';
$message .= ' -d \'' . JSON::stringify($data) . '\'';
}
return $message;
}
Expand Down

0 comments on commit 58b0928

Please sign in to comment.