function JsonTest::testStructuredReversibility
Same name in other branches
- 9 core/tests/Drupal/Tests/Component/Serialization/JsonTest.php \Drupal\Tests\Component\Serialization\JsonTest::testStructuredReversibility()
- 10 core/tests/Drupal/Tests/Component/Serialization/JsonTest.php \Drupal\Tests\Component\Serialization\JsonTest::testStructuredReversibility()
- 11.x core/tests/Drupal/Tests/Component/Serialization/JsonTest.php \Drupal\Tests\Component\Serialization\JsonTest::testStructuredReversibility()
Test the reversibility of structured data
File
-
core/
tests/ Drupal/ Tests/ Component/ Serialization/ JsonTest.php, line 99
Class
- JsonTest
- @coversDefaultClass \Drupal\Component\Serialization\Json @group Serialization
Namespace
Drupal\Tests\Component\SerializationCode
public function testStructuredReversibility() {
// Verify reversibility for structured data. Also verify that necessary
// characters are escaped.
$source = [
TRUE,
FALSE,
0,
1,
'0',
'1',
$this->string,
[
'key1' => $this->string,
'key2' => [
'nested' => TRUE,
],
],
];
$json = Json::encode($source);
foreach ($this->htmlUnsafe as $char) {
$this->assertTrue(strpos($json, $char) === FALSE, sprintf('A JSON encoded string does not contain %s.', $char));
}
// Verify that JSON encoding escapes the HTML unsafe characters
foreach ($this->htmlUnsafeEscaped as $char) {
$this->assertTrue(strpos($json, $char) > 0, sprintf('A JSON encoded string contains %s.', $char));
}
$json_decoded = Json::decode($json);
$this->assertNotSame($source, $json, 'An array encoded in JSON is identical to the source.');
$this->assertSame($source, $json_decoded, 'Encoding structured data to JSON and decoding back not results in the original data.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.