Skip to content

Commit 10a9127

Browse files
committed
Merge branch 'najdanovicivan-json-encode-patch'
2 parents 3dcc900 + de3868f commit 10a9127

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Elasticsearch/Serializers/ArrayToJSONSerializer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Elasticsearch\Common\Exceptions\RuntimeException;
88

9+
if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
10+
//PHP < 7.2 Define it as 0 so it does nothing
11+
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
12+
}
13+
914
/**
1015
* Class JSONSerializer
1116
*
@@ -25,7 +30,7 @@ public function serialize($data): string
2530
if (is_string($data) === true) {
2631
return $data;
2732
} else {
28-
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
33+
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
2934
if ($data === false) {
3035
throw new RuntimeException("Failed to JSON encode: ".json_last_error());
3136
}

src/Elasticsearch/Serializers/EverythingToJSONSerializer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use Elasticsearch\Common\Exceptions\RuntimeException;
88

9+
if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
10+
//PHP < 7.2 Define it as 0 so it does nothing
11+
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
12+
}
13+
914
/**
1015
* Class EverythingToJSONSerializer
1116
*
@@ -22,7 +27,7 @@ class EverythingToJSONSerializer implements SerializerInterface
2227
*/
2328
public function serialize($data): string
2429
{
25-
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
30+
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
2631
if ($data === false) {
2732
throw new RuntimeException("Failed to JSON encode: ".json_last_error());
2833
}

src/Elasticsearch/Serializers/SmartSerializer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Elasticsearch\Common\Exceptions;
88
use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
99

10+
if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
11+
//PHP < 7.2 Define it as 0 so it does nothing
12+
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
13+
}
14+
1015
/**
1116
* Class SmartSerializer
1217
*
@@ -26,7 +31,7 @@ public function serialize($data): string
2631
if (is_string($data) === true) {
2732
return $data;
2833
} else {
29-
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
34+
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
3035
if ($data === false) {
3136
throw new Exceptions\RuntimeException("Failed to JSON encode: ".json_last_error());
3237
}

0 commit comments

Comments
 (0)