Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json_encode for unicode(emoji) characters #856

Merged
merged 3 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Elasticsearch/Serializers/ArrayToJSONSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

use Elasticsearch\Common\Exceptions\RuntimeException;

if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
//PHP < 7.2 Define it as 0 so it does nothing
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
}

/**
* Class JSONSerializer
*
Expand All @@ -29,7 +34,7 @@ public function serialize($data)
if (is_string($data) === true) {
return $data;
} else {
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
if ($data === false) {
throw new RuntimeException("Failed to JSON encode: ".json_last_error());
}
Expand Down
7 changes: 6 additions & 1 deletion src/Elasticsearch/Serializers/EverythingToJSONSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

use Elasticsearch\Common\Exceptions\RuntimeException;

if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
//PHP < 7.2 Define it as 0 so it does nothing
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
}

/**
* Class EverythingToJSONSerializer
*
Expand All @@ -26,7 +31,7 @@ class EverythingToJSONSerializer implements SerializerInterface
*/
public function serialize($data)
{
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
if ($data === false) {
throw new RuntimeException("Failed to JSON encode: ".json_last_error());
}
Expand Down
7 changes: 6 additions & 1 deletion src/Elasticsearch/Serializers/SmartSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Elasticsearch\Common\Exceptions;
use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;

if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
//PHP < 7.2 Define it as 0 so it does nothing
define('JSON_INVALID_UTF8_SUBSTITUTE', 0);
}

/**
* Class SmartSerializer
*
Expand All @@ -30,7 +35,7 @@ public function serialize($data)
if (is_string($data) === true) {
return $data;
} else {
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE);
if ($data === false) {
throw new Exceptions\RuntimeException("Failed to JSON encode: ".json_last_error());
}
Expand Down