function JsonTest::testStructuredReversibility
Same name in other branches
- 9 core/tests/Drupal/Tests/Component/Serialization/JsonTest.php \Drupal\Tests\Component\Serialization\JsonTest::testStructuredReversibility()
- 8.9.x 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()
Tests the reversibility of structured data.
File
-
core/
tests/ Drupal/ Tests/ Component/ Serialization/ JsonTest.php, line 102
Class
- JsonTest
- @coversDefaultClass \Drupal\Component\Serialization\Json @group Serialization
Namespace
Drupal\Tests\Component\SerializationCode
public function testStructuredReversibility() : void {
// 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->assertStringNotContainsString($char, $json, 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->assertStringContainsString($char, $json, 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.