[GH-ISSUE #616] JSON Encode Failed #218

Closed
opened 2026-03-04 02:13:14 +03:00 by kerem · 4 comments
Owner

Originally created by @austinh on GitHub (Jul 24, 2015).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/616

JSON encoding failed: Malformed UTF-8 characters, possibly incorrectly encoded. Encoding: array (

I upgraded my symfony composer files lately, and got this error. This does not happen in 1.14.0. The latest change in "Fixed JSON encoding errors being silently suppressed (PHP 5.5+ only)" caused this error.

I am using this in conjunction with SNCRedis Bundle to store sessions in redis.

Is there any way sncredisbundle can utf8_encode the log message?
The session is saved fine and everything else works, its purely the logging.
I have also created a bug at SncRedisBundle git because it may be an issue with their code.

Originally created by @austinh on GitHub (Jul 24, 2015). Original GitHub issue: https://github.com/Seldaek/monolog/issues/616 JSON encoding failed: Malformed UTF-8 characters, possibly incorrectly encoded. Encoding: array ( I upgraded my symfony composer files lately, and got this error. This does not happen in 1.14.0. The latest change in "Fixed JSON encoding errors being silently suppressed (PHP 5.5+ only)" caused this error. I am using this in conjunction with SNCRedis Bundle to store sessions in redis. Is there any way sncredisbundle can utf8_encode the log message? The session is saved fine and everything else works, its purely the logging. I have also created a bug at SncRedisBundle git because it may be an issue with their code.
kerem closed this issue 2026-03-04 02:13:14 +03:00
Author
Owner

@Seldaek commented on GitHub (Jul 30, 2015):

I believe this is usually indicating a malformed UTF-8 message yes, but maybe I made a mistake, can you link to that SncRedis issue?

<!-- gh-comment-id:126522673 --> @Seldaek commented on GitHub (Jul 30, 2015): I believe this is usually indicating a malformed UTF-8 message yes, but maybe I made a mistake, can you link to that SncRedis issue?
Author
Owner

@austinh commented on GitHub (Jul 30, 2015):

https://github.com/snc/SncRedisBundle/issues/193

<!-- gh-comment-id:126523164 --> @austinh commented on GitHub (Jul 30, 2015): https://github.com/snc/SncRedisBundle/issues/193
Author
Owner

@ckmaresca commented on GitHub (Aug 12, 2015):

I don't know if this is relevant, but there are a bunch of options that you can use with json_encode to deal with odd characters that cause encoding to fail. They are described here: http://php.net/manual/en/json.constants.php - specifically JSON_HEX_* and JSON_UNESCAPED_*

I would note that this is a pain in PHP as it's not obvious that you need these options for things to work and json_encode has a habit of failing silently with no exception to trap....

<!-- gh-comment-id:130460855 --> @ckmaresca commented on GitHub (Aug 12, 2015): I don't know if this is relevant, but there are a bunch of options that you can use with `json_encode` to deal with odd characters that cause encoding to fail. They are described here: http://php.net/manual/en/json.constants.php - specifically `JSON_HEX_*` and `JSON_UNESCAPED_*` I would note that this is a pain in PHP as it's not obvious that you need these options for things to work and `json_encode` has a habit of failing silently with no exception to trap....
Author
Owner

@Seldaek commented on GitHub (Nov 18, 2015):

I'm gonna assume this gets fixed by https://github.com/Seldaek/monolog/pull/683 as there is no clear repro case I can't verify but it most likely is covered too.

@ckmaresca for the record, JSON_HEX and JSON_UNESCAPED do nothing to prevent these errors unfortunately, they merely tweak the way things are encoded by default, but that only concerns valid UTF-8 strings. Non-UTF-8 can not be encoded to json as the format mandates Unicode strings.

<!-- gh-comment-id:157803335 --> @Seldaek commented on GitHub (Nov 18, 2015): I'm gonna assume this gets fixed by https://github.com/Seldaek/monolog/pull/683 as there is no clear repro case I can't verify but it most likely is covered too. @ckmaresca for the record, JSON_HEX and JSON_UNESCAPED do nothing to prevent these errors unfortunately, they merely tweak the way things are encoded by default, but that only concerns _valid_ UTF-8 strings. Non-UTF-8 can not be encoded to json as the format mandates Unicode strings.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#218
No description provided.