Skip to content

Commit

Permalink
Add SmartSerializer and make default
Browse files Browse the repository at this point in the history
SmartSerializer introspects the response headers to determine if
the response is JSON or text.  If the data is JSON, it is decoded.
Otherwise it is returned as a string.

The other serializers and interface had to be modified to allow passing
of headers.  The headers are not currently used
  • Loading branch information
polyfractal committed Feb 11, 2014
1 parent 5ec9257 commit 8bb8769
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Elasticsearch/Serializers/ArrayToJSONSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public function serialize($data)
* Deserialize JSON into an assoc array
*
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @return array
*/
public function deserialize($data)
public function deserialize($data, $headers)
{
return json_decode($data, true);

Expand Down
3 changes: 2 additions & 1 deletion src/Elasticsearch/Serializers/EverythingToJSONSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public function serialize($data)
* Deserialize JSON into an assoc array
*
* @param string $data JSON encoded string
* @param array $headers Response headers
*
* @return array
*/
public function deserialize($data)
public function deserialize($data, $headers)
{
return json_decode($data, true);

Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/Serializers/SerializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface SerializerInterface
public function serialize($data);


public function deserialize($data);
public function deserialize($data, $headers);
}
73 changes: 73 additions & 0 deletions src/Elasticsearch/Serializers/SmartSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* User: zach
* Date: 5/1/13
* Time: 10:00 PM
*/

namespace Elasticsearch\Serializers;

/**
* Class SmartSerializer
*
* @category Elasticsearch
* @package Elasticsearch\Serializers\JSONSerializer
* @author Zachary Tong <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
class SmartSerializer implements SerializerInterface
{


/**
* Serialize assoc array into JSON string
*
* @param string|array $data Assoc array to encode into JSON
*
* @return string
*/
public function serialize($data)
{
if (is_string($data) === true) {
return $data;
} else {
$data = json_encode($data);
if ($data === '[]') {
return '{}';
} else {
return $data;
}
}


}


/**
* Deserialize by introspecting content_type. Tries to deserialize JSON,
* otherwise returns string
*
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @return array
*/
public function deserialize($data, $headers)
{
if (isset($headers['content_type']) === true) {
if (strpos($headers['content_type'], 'json') !== false) {
return json_decode($data, true);
} else {
//Not json, return as string
return $data;
}

} else {
//No content headers, assume json
return json_decode($data, true);
}


}
}
2 changes: 1 addition & 1 deletion src/Elasticsearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function performRequest($method, $uri, $params = null, $body = null)
$connection->markAlive();
$this->retryAttempts = 0;

$data = $this->serializer->deserialize($response['text']);
$data = $this->serializer->deserialize($response['text'], $response['info']);

return array(
'status' => $response['status'],
Expand Down

0 comments on commit 8bb8769

Please sign in to comment.