Skip to content

Commit

Permalink
[jan] Improve JSON serialization of invalid UTF-8 data.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Nov 10, 2017
1 parent dca058c commit 5f9130c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/Horde/Serialize/changelog.yml
Expand Up @@ -8,7 +8,9 @@
license:
identifier: LGPL-2.1
uri: http://www.horde.org/licenses/lgpl21
notes:
notes: |+
[jan] Improve JSON serialization of invalid UTF-8 data.
2.0.5:
api: 1.0.0
state:
Expand Down
3 changes: 3 additions & 0 deletions lib/Horde/Serialize.php
Expand Up @@ -244,6 +244,9 @@ protected static function _serialize($data, $mode, $params = null)
/* Basic error handling attempts.
* TODO: JSON_ERROR_UTF8 = 5; available as of PHP 5.3.3 */
if (json_last_error() === 5) {
if (!$params) {
$params = 'UTF-8';
}
$data = json_encode(Horde_String::convertCharset($data, $params, 'UTF-8', true));
} else {
$data = $tmp;
Expand Down
8 changes: 5 additions & 3 deletions package.xml
Expand Up @@ -22,7 +22,7 @@
<email>slusarz@horde.org</email>
<active>yes</active>
</developer>
<date>2017-10-30</date>
<date>2017-11-10</date>
<version>
<release>2.0.6</release>
<api>1.0.0</api>
Expand Down Expand Up @@ -61,6 +61,7 @@
<dir name="Serialize">
<dir name="fixtures">
<file name="badutf8.txt" role="test" />
<file name="badutf8_2.txt" role="test" />
</dir> <!-- /test/Horde/Serialize/fixtures -->
<file name="AllTests.php" role="test" />
<file name="bootstrap.php" role="test" />
Expand Down Expand Up @@ -147,9 +148,10 @@
<install as="Horde/Serialize/Exception.php" name="lib/Horde/Serialize/Exception.php" />
<install as="Horde/Serialize/AllTests.php" name="test/Horde/Serialize/AllTests.php" />
<install as="Horde/Serialize/bootstrap.php" name="test/Horde/Serialize/bootstrap.php" />
<install as="Horde/Serialize/fixtures/badutf8.txt" name="test/Horde/Serialize/fixtures/badutf8.txt" />
<install as="Horde/Serialize/fixtures/badutf8_2.txt" name="test/Horde/Serialize/fixtures/badutf8_2.txt" />
<install as="Horde/Serialize/JsonTest.php" name="test/Horde/Serialize/JsonTest.php" />
<install as="Horde/Serialize/phpunit.xml" name="test/Horde/Serialize/phpunit.xml" />
<install as="Horde/Serialize/fixtures/badutf8.txt" name="test/Horde/Serialize/fixtures/badutf8.txt" />
</filelist>
</phprelease>
<changelog>
Expand Down Expand Up @@ -400,7 +402,7 @@
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2017-10-30</date>
<date>2017-11-10</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
Expand Down
7 changes: 7 additions & 0 deletions test/Horde/Serialize/JsonTest.php
Expand Up @@ -104,6 +104,13 @@ public function testJsonInvalidUTF8Input()
Horde_Serialize::serialize(file_get_contents(__DIR__ . '/fixtures/badutf8.txt'), Horde_Serialize::JSON, 'iso-8859-1')
);
error_reporting($old_error_reporting);
$this->assertEquals(
'"This is b\u00e4d \u0080UTF-8.\n"',
Horde_Serialize::serialize(
file_get_contents(__DIR__ . '/fixtures/badutf8_2.txt'),
Horde_Serialize::JSON
)
);
}

// JSON encode/decode tests.
Expand Down
1 change: 1 addition & 0 deletions test/Horde/Serialize/fixtures/badutf8_2.txt
@@ -0,0 +1 @@
This is bäd €UTF-8.

0 comments on commit 5f9130c

Please sign in to comment.