-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SmartSerializer and make default
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
1 parent
5ec9257
commit 8bb8769
Showing
5 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
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
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); | ||
} | ||
|
||
|
||
} | ||
} |
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