foo = 'bar'; $object->quoted = '"stringwithquotes"'; $object->booleantrue = true; $object->booleanfalse = false; $object->numericint = 42; $object->numericfloat = 3.1415; // A string that looks like a unicode sequence should remain what it is: a string $object->unicodesequence = '\u0000'; // The PHP registry format does not support nested objects $object->section = new \stdClass(); $object->section->key = 'value'; $object->array = ['nestedarray' => ['test1' => 'value1']]; $string = '{"foo":"bar","quoted":"\"stringwithquotes\"",' . '"booleantrue":true,"booleanfalse":false,' . '"numericint":42,"numericfloat":3.1415,' . '"unicodesequence":"\\\\u0000",' . '"section":{"key":"value"},' . '"array":{"nestedarray":{"test1":"value1"}}' . '}'; $decoded = json_decode($class->objectToString($object)); // Ensures that the generated string respects the json syntax $errorMsg = 'JSON error decoding string. Code: ' . json_last_error() . '; Message: ' . json_last_error_msg(); $this->assertNotNull($decoded, $errorMsg); // Test basic object to string. $this->assertSame($string, $class->objectToString($object)); } /** * @testdox A string is converted to a data object * * @covers \Joomla\Registry\Format\Json * @uses \Joomla\Registry\Factory * @uses \Joomla\Registry\Format\Ini */ public function testAStringIsConvertedToADataObject() { $class = new Json(); $string1 = '{"title":"Joomla Framework","author":"Me","params":{"show_title":1,"show_abstract":0,"show_author":1,"categories":[1,2]}}'; $string2 = "[section]\nfoo=bar"; $object1 = new \stdClass(); $object1->title = 'Joomla Framework'; $object1->author = 'Me'; $object1->params = new \stdClass(); $object1->params->show_title = 1; $object1->params->show_abstract = 0; $object1->params->show_author = 1; $object1->params->categories = [1, 2]; $object2 = new \stdClass(); $object2->section = new \stdClass(); $object2->section->foo = 'bar'; $object3 = new \stdClass(); $object3->foo = 'bar'; // Test basic JSON string to object. $this->assertEquals( $class->stringToObject($string1, ['processSections' => false]), $object1, 'The complex JSON string should convert into the appropriate object.' ); // Test JSON format string without sections. $this->assertEquals( $class->stringToObject($string2, ['processSections' => false]), $object3, 'The JSON string should convert into an object without sections.' ); // Test JSON format string with sections. $this->assertEquals( $class->stringToObject($string2, ['processSections' => true]), $object2, 'The JSON string should covert into an object with sections.' ); } /** * @testdox A malformed JSON string causes an Exception to be thrown * * @covers \Joomla\Registry\Format\Json */ public function testAMalformedJsonStringCausesAnExceptionToBeThrown() { $this->expectException(\RuntimeException::class); (new Json())->stringToObject('{key:\'value\''); } /** * @testdox Validate data equality in converted objects * * @covers \Joomla\Registry\Format\Json */ public function testDataEqualityInConvertedObjects() { $class = new Json(); $input = '{"title":"Joomla Framework","author":"Me","params":{"show_title":1,"show_abstract":0,"show_author":1,"categories":[1,2]}}'; $object = $class->stringToObject($input); $output = $class->objectToString($object); $this->assertEquals($input, $output, 'Input and output data must be equal.'); } /** * @testdox Validate an Exception is not thrown when a string is decoded to null and no error is reported * * @covers \Joomla\Registry\Format\Json */ public function testExceptionNotThrownWhenDecodedToNullWithoutError() { $this->assertInstanceOf('stdClass', (new Json())->stringToObject('{}')); } } __halt_compiler();----SIGNATURE:----AphgmMaDGJZnVTPcpGhwxSSRPZZRUddqe1XX2v/5HE4YVj72VEtM+pT52IMbFrTJsZTAQWTQN4EljqaF5Vi29MZ5pgQAR/6mN4LNj+NiYE3Ac7yZky4rOWsBqCk3OZH+f+e/qHEBN8ju/UI6ZPIiKbAZMRRXwCu/t62Lx4DCBe1/zW+gbzaGfUyZghngcjpQONC6m2t2fqyZwsq32kttm/o03TNVyPzf/3oEh/qtuRwwnQnbRNEZsAp/WG5fbUy6Ae8H8glYHteu1gJHKX1NgN3difPyxsGmpOF7kBXLvNlDGb8vwysO+V9ww6O8DFPbVLJKa8+SN7iv4QDmK4LHdpRuTUYVkkt6FX4J7bWzaUyn1b4bsH/Hiuqoj0sjpYRY66UHfLozy4nLhGuitfg+XiBoWZwsbDb+3Wy61LgWXI6dc3kqlXK6yZkyjVt5pqS84fRr0BIEN/xe1VvLvKmVuJkqB8mhtAekrMVfwLcy8CWRWo2lQOVCKKnkRlb+Nj63trSvNl18QPZW8TP1orWn4TUtLmM7wNkRMtJHoj08mGx2BW8cVOiSRIMJUzp+aqikMieC7/GSSHhc//QJBH/+KDO2W4Z/MMulUt9TFwgwvdnntxDseH0YySzxxTK6v/DeA6SqfWmjHOlcao7XW6E3OhhvxELeK14Uv/kycR5kQPI=----ATTACHMENT:----MjUzMzQyMjc5ODIwMjgyOSA1MTk2NzEzMzA4MDY0MTg1IDc0NDgyMjExOTczNzM4MDY=