// $params = Source character set
case self::JSON:
- if (!empty($params)) {
+ if (!empty($params) && is_string($data)) {
$data = Horde_String::convertCharset($data, $params, 'UTF-8');
}
- $data = json_encode($data);
+ $tmp = json_encode($data);
+
+ /* Basic error handling attempts. Requires PHP 5.3+.
+ * Error code 5, although not documented, indicates non UTF-8
+ * data. */
+ if (function_exists('json_last_error') &&
+ (json_last_error() == 5)) {
+ $data = json_encode(Horde_Util::convertToUtf8($data));
+ } else {
+ $data = $tmp;
+ }
break;
case self::LZF:
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial Horde 4 package.</notes>
+ <notes>* Add support for limited error handling for json encoding errors
+ (requires PHP 5.3+).
+ * Initial Horde 4 package.</notes>
<contents>
<dir name="/">
<dir name="lib">
--- /dev/null
+Note: To play video messages sent to email, QuickTime® 6.5 or higher is required.
--- /dev/null
+--TEST--
+JSON encode/decode tests (invalid UTF-8 input).
+--SKIPIF--
+<?php
+ if (version_compare(phpversion(), '5.3.0') == -1) {
+ echo "skip Test requires PHP 5.3+";
+ }
+?>
+--FILE--
+<?php
+
+error_reporting(E_ALL);
+
+require dirname(__FILE__) . '/../../../lib/Horde/Serialize.php';
+require dirname(__FILE__) . '/../../../../Util/lib/Horde/String.php';
+require dirname(__FILE__) . '/../../../../Util/lib/Horde/Util.php';
+
+echo Horde_Serialize::serialize(file_get_contents('./fixtures/badutf8.txt'), Horde_Serialize::JSON);
+
+?>
+--EXPECT--
+"Note: To play video messages sent to email, QuickTime\u00ae 6.5 or higher is required.\n"