Skip to content

Commit 0d3d02b

Browse files
committed
Merge pull request #941 from oldskool/issue-717
Add JSON_BIGINT_AS_STRING solution for #717
2 parents cab96cf + 0cd36c0 commit 0d3d02b

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file based on the
1717
### Bugfixes
1818

1919
### Added
20+
- Connection option to convert JSON bigint results to strings can now be set [#717](https://github.com/ruflin/Elastica/issues/717)
2021

2122
### Improvements
2223

lib/Elastica/Client.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Client
1919
*
2020
* log: Set to true, to enable logging, set a string to log to a specific file
2121
* retryOnConflict: Use in \Elastica\Client::updateDocument
22+
* bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717)
2223
*
2324
* @var array
2425
*/
@@ -35,6 +36,7 @@ class Client
3536
'roundRobin' => false,
3637
'log' => false,
3738
'retryOnConflict' => 0,
39+
'bigintConversion' => false,
3840
);
3941

4042
/**

lib/Elastica/Response.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ class Response
5555
*/
5656
protected $_status = null;
5757

58+
/**
59+
* Whether or not to convert bigint results to string (see issue #717)
60+
*
61+
* @var bool
62+
*/
63+
protected $_jsonBigintConversion = false;
64+
5865
/**
5966
* Construct.
6067
*
@@ -183,7 +190,11 @@ public function getData()
183190
$this->_error = true;
184191
} else {
185192
try {
186-
$response = JSON::parse($response);
193+
if ($this->getJsonBigintConversion()) {
194+
$response = JSON::parse($response, false, 512, JSON_BIGINT_AS_STRING);
195+
} else {
196+
$response = JSON::parse($response);
197+
}
187198
} catch (JSONParseException $e) {
188199
// leave response as is if parse fails
189200
}
@@ -305,4 +316,24 @@ public function getScrollId()
305316

306317
return $data['_scroll_id'];
307318
}
319+
320+
/**
321+
* Sets whether or not to apply bigint conversion on the JSON result.
322+
*
323+
* @param bool $jsonBigintConversion
324+
*/
325+
public function setJsonBigintConversion($jsonBigintConversion)
326+
{
327+
$this->_jsonBigintConversion = $jsonBigintConversion;
328+
}
329+
330+
/**
331+
* Gets whether or not to apply bigint conversion on the JSON result.
332+
*
333+
* @return boolean
334+
*/
335+
public function getJsonBigintConversion()
336+
{
337+
return $this->_jsonBigintConversion;
338+
}
308339
}

lib/Elastica/Transport/Guzzle.php

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public function exec(Request $request, array $params)
103103

104104
$response = new Response((string) $res->getBody(), $res->getStatusCode());
105105
$response->setQueryTime($end - $start);
106+
if ($connection->hasConfig('bigintConversion')) {
107+
$response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
108+
}
106109

107110
$response->setTransferInfo(
108111
array(

lib/Elastica/Transport/Http.php

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public function exec(Request $request, array $params)
154154
$response = new Response($responseString, curl_getinfo($conn, CURLINFO_HTTP_CODE));
155155
$response->setQueryTime($end - $start);
156156
$response->setTransferInfo(curl_getinfo($conn));
157+
if ($connection->hasConfig('bigintConversion')) {
158+
$response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
159+
}
157160

158161
if ($response->hasError()) {
159162
throw new ResponseException($request, $response);

0 commit comments

Comments
 (0)