diff --git a/Classes/Common/XmlDocument.php b/Classes/Common/XmlDocument.php index f53a070..d14b0d8 100644 --- a/Classes/Common/XmlDocument.php +++ b/Classes/Common/XmlDocument.php @@ -81,10 +81,17 @@ public function toJson() { $result = []; $xmlArray = $this->toArray(); + + /* + * Convert every key value pair to valid json, + * then decode it, so it stays valid when the whole + * array is encoded + */ foreach ($xmlArray as $id => $value) { - $result[$id] = json_encode($value, JSON_PRETTY_PRINT); + $result[$id] = json_encode($value); + $result[$id] = json_decode($result[$id], true); } - return trim(implode($result)); + return trim(json_encode($result)); } protected function convert(SimpleXMLElement $node): array @@ -124,7 +131,7 @@ protected function convert(SimpleXMLElement $node): array if ($node->getName() == 'p' && $node->count() > 0 && !empty($node)) { // Add literal string, to store the node order $literal = str_replace(array("\n", "\r"), '', trim($node->asXML())); - $literal = str_replace("", '', $literal); + $literal = str_replace('', '', $literal); $result['@literal'] = $literal; } } diff --git a/Tests/Functional/Common/Fixtures/minimal.json b/Tests/Functional/Common/Fixtures/minimal.json index c1c3597..5d49724 100644 --- a/Tests/Functional/Common/Fixtures/minimal.json +++ b/Tests/Functional/Common/Fixtures/minimal.json @@ -1,11 +1 @@ -{ - "subroot": [ - { - "subsubroot": [ - { - "@value": "entry" - } - ] - } - ] -} +{"minimal_root":{"@xml:id":"minimal_root","subroot":[{"subsubroot":[{"@value":"entry"}]}]}} diff --git a/Tests/Functional/Common/Fixtures/minimal.xml b/Tests/Functional/Common/Fixtures/minimal.xml index 6be0088..a4bec39 100644 --- a/Tests/Functional/Common/Fixtures/minimal.xml +++ b/Tests/Functional/Common/Fixtures/minimal.xml @@ -1,5 +1,5 @@ - + entry diff --git a/Tests/Unit/Common/XmlDocumentTest.php b/Tests/Unit/Common/XmlDocumentTest.php index 07b145e..c89b43f 100644 --- a/Tests/Unit/Common/XmlDocumentTest.php +++ b/Tests/Unit/Common/XmlDocumentTest.php @@ -74,10 +74,10 @@ public function xmlIdIsIncluded() */ public function testMixedContentIsIncluded() { - $mixedContentString = '

I am mixed content

'; + $mixedContentString = '

I am mixed content

'; $subject = XmlDocument::from($mixedContentString); $subject->setLiteralString(true); - $expected = '"@literal": "

I am mixed <\/b> content <\/p>"'; + $expected = '"@literal":"

I am mixed <\/b> content <\/p>"'; self::assertStringContainsString($expected, $subject->toJson()); } @@ -95,16 +95,11 @@ public function testMixedContentIsNotIncluded() */ public function testConvertedAttribute() { - $subject = XmlDocument::from(''); + $subject = XmlDocument::from(''); - $expected = '{ - "@attributes": { - "id": "1", - "name": "ExampleItem" - } - }'; + $expected = '{"item_test":{"@attributes":{"id":"1","name":"ExampleItem"},"@xml:id":"item_test"}}'; - self::assertSame(json_decode($expected, true), json_decode($subject->toJson(), true)); + self::assertSame($expected, $subject->toJson()); } /** @@ -112,9 +107,13 @@ public function testConvertedAttribute() */ public function testPlainText() { - $subject = XmlDocument::from('

I am plain text

'); - $expected = '{"@value": "I am plain text"}'; - self::assertSame(json_decode($subject->toJson(), true), json_decode($expected, true)); + $subject = XmlDocument::from('

I am plain text

'); + $expected = '{"testid": { + "@value": "I am plain text", + "@xml:id": "testid" } + }'; + + self::assertJsonStringEqualsJsonString($expected, $subject->toJson()); } /** @@ -123,7 +122,7 @@ public function testPlainText() public function testSplitSymbols() { $xmlString = ' - + @@ -142,61 +141,10 @@ public function testSplitSymbols() '; - $jsonString = '{ - "@xml:id": "TC-01", - "measure": [ - { - "@attributes": { - "n": "1" - }, - "staff": [ - { - "@attributes": { - "n": "1" - }, - "layer": [ - { - "@attributes": { - "n": "1" - }, - "note": [ - { - "@attributes": { - "pname": "e", - "oct": "4", - "dur": "4" - }, - "@xml:id": "N2" - } - ] - } - ] - } - ] - } - ] - }{ - "music": [ - { - "body": [ - { - "@link": "TC-01" - } - ] - } - ] - }'; + $expected = '{"TC-01":{"@xml:id":"TC-01","measure":[{"@attributes":{"n":"1"},"staff":[{"@attributes":{"n":"1"},"layer":[{"@attributes":{"n":"1"},"note":[{"@attributes":{"pname":"e","oct":"4","dur":"4"},"@xml:id":"N2"}]}]}]}]},"mei_head":{"@xml:id":"mei_head","music":[{"body":[{"@link":"TC-01"}]}]}}'; $subject = XmlDocument::from($xmlString)->setSplitSymbols(['mdiv']); - //TODO: Should compare Json to Json, but splitsymbol Strings are not valid - //->Discuss - self::assertEquals(trim($this->processString($jsonString)),trim($this->processString($subject->toJson()))); - } - - private function processString(String $str) { - - return str_replace(array("\n"," "),'',$str); - + self::assertJsonStringEqualsJsonString($expected, $subject->toJson()); } }