From: <p_f...@us...> - 2007-12-11 20:27:38
|
Revision: 555 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=555&view=rev Author: p_frischmuth Date: 2007-12-11 12:27:34 -0800 (Tue, 11 Dec 2007) Log Message: ----------- [fixed] removed all newlines and whitespaces in order to follow the json spec; escaped all forbidden chars (see json spec for details) [modified] test cases updated in order to test special char cases Modified Paths: -------------- trunk/rdfapi-php/api/syntax/JsonParser.php trunk/rdfapi-php/api/syntax/JsonSerializer.php trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php Modified: trunk/rdfapi-php/api/syntax/JsonParser.php =================================================================== --- trunk/rdfapi-php/api/syntax/JsonParser.php 2007-12-05 08:18:50 UTC (rev 554) +++ trunk/rdfapi-php/api/syntax/JsonParser.php 2007-12-11 20:27:34 UTC (rev 555) @@ -19,7 +19,7 @@ $jsonModel = array(); $jsonModel = json_decode($jsonString, true); - + // throws an excpetion if json model was corrupt if (!is_array($jsonModel)) { throw new Exception('error in json string'); @@ -39,7 +39,9 @@ $dtype = (isset($obj['datatype'])) ? $obj['datatype'] : ''; $lang = (isset($obj['lang'])) ? $obj['lang'] : ''; - $o = new Literal($obj['value'], $lang); + $oVal = $obj['value']; + + $o = new Literal($oVal, $lang); $o->setDatatype($dtype); } Modified: trunk/rdfapi-php/api/syntax/JsonSerializer.php =================================================================== --- trunk/rdfapi-php/api/syntax/JsonSerializer.php 2007-12-05 08:18:50 UTC (rev 554) +++ trunk/rdfapi-php/api/syntax/JsonSerializer.php 2007-12-11 20:27:34 UTC (rev 555) @@ -4,7 +4,7 @@ * * @package syntax * @author Philipp Frischmuth <ph...@fr...> - * @version $Id:$ + * @version $Id$ */ class JsonSerializer extends Object { @@ -28,11 +28,7 @@ // sort alphabetically ksort($subjects); - - // echo '<pre>'; - // print_r($subjects); - // echo '</pre>'; - + // triples are sorted by subject now, each key is a subject uri, containing all triples with this subject uri $i = 0; foreach ($subjects as $predicatesArray) { @@ -47,9 +43,9 @@ // add special _: sequence for blank node only if ($subj instanceof BlankNode) { - $jsonString .= PHP_EOL . ' "_:' . $subj->getLabel() . '" : '; + $jsonString .= '"_:' . $this->_escapeValue($subj->getLabel()) . '":'; } else { - $jsonString .= PHP_EOL . ' "' . $subj->getLabel() . '" : '; + $jsonString .= '"' . $this->_escapeValue($subj->getLabel()) . '":'; } @@ -73,7 +69,7 @@ } $j++; - $jsonString .= PHP_EOL . ' "' . $valueArray[0]->getLabelPredicate() . '" : '; + $jsonString .= '"' . $this->_escapeValue($valueArray[0]->getLabelPredicate()) . '":'; // create a json array (value array) $jsonString .= '['; @@ -86,52 +82,82 @@ $k++; // create json value object (value object) - $jsonString .= PHP_EOL . ' {'; + $jsonString .= '{'; $obj = $triple->getObject(); // add special _: sequence for blank nodes only if ($obj instanceof BlankNode) { - $jsonString .= PHP_EOL . ' "value" : "_:' . $obj->getLabel() . '",'; + $jsonString .= '"value":"_:' . $this->_escapeValue($obj->getLabel()) . '",'; + } else if ($obj instanceof Literal) { + $jsonString .= '"value":"' . $this->_escapeValue($obj->getLabel()) . '",'; } else { - $jsonString .= PHP_EOL . ' "value" : "' . $obj->getLabel() . '",'; + $jsonString .= '"value":"' . $this->_escapeValue($obj->getLabel()) . '",'; } // add type of object if ($obj instanceof Literal) { - $jsonString .= PHP_EOL . ' "type" : "literal"'; + $jsonString .= '"type":"literal"'; } else if ($obj instanceof BlankNode) { - $jsonString .= PHP_EOL . ' "type" : "bnode"'; + $jsonString .= '"type":"bnode"'; } else { - $jsonString .= PHP_EOL . ' "type" : "uri"'; + $jsonString .= '"type":"uri"'; } if ($obj instanceof Literal) { if ($obj->getLanguage() != '') { - $jsonString .= ',' . PHP_EOL . ' "lang" : "' . $obj->getLanguage() . '"'; + $jsonString .= ',"lang":"' . $this->_escapeValue($obj->getLanguage()) . '"'; } if ($obj->getDatatype() != '') { - $jsonString .= ',' . PHP_EOL . ' "datatype" : "' . $obj->getDatatype() . '"'; + $jsonString .= ',"datatype":"' . $this->_escapeValue($obj->getDatatype()) . '"'; } } // close value object - $jsonString .= PHP_EOL . ' }'; + $jsonString .= '}'; } // close the value array - $jsonString .= PHP_EOL . ' ]'; + $jsonString .= ']'; } // close the json object (for the subject) (subject object) - $jsonString .= PHP_EOL . ' }'; + $jsonString .= '}'; } // close root json object (root object) - $jsonString .= PHP_EOL . '}'; + $jsonString .= '}'; return $jsonString; } + + /* + * Escapes the following chars as specified at json.org: + * + * " -> \" + * \ -> \\ + * / -> \/ + * \b -> \\b + * \f -> \\f + * \n -> \\n + * \r -> \\r + * \t -> \\t + * \uXXXX -> \\uXXXX + */ + protected function _escapeValue($value) { + + + $value = str_replace("\\", '\\\\', $value); + #$value = str_replace("/", '\/', $value); + $value = str_replace("\n", '\\n', $value); + $value = str_replace("\t", '\\t', $value); + $value = str_replace("\r", '\\r', $value); + $value = str_replace("\b", '\\b', $value); + $value = str_replace("\f", '\\f', $value); + $value = str_replace('"', '\"', $value); + + return $value; + } } ?> Modified: trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php 2007-12-05 08:18:50 UTC (rev 554) +++ trunk/rdfapi-php/test/unit/Syntax/jsonParser_test.php 2007-12-11 20:27:34 UTC (rev 555) @@ -19,43 +19,7 @@ GLOBAL $short_datatype; - $this->modelString = '{ "http://example.org/about" : { - "http://purl.org/dc/elements/1.1/creator" : [ - { - "value" : "Anna Wilder", - "type" : "literal" - } - ], - "http://purl.org/dc/elements/1.1/title" : [ - { - "value" : "Annas Homepage", - "type" : "literal", - "lang" : "en" - } - ], - "http://xmlns.com/foaf/0.1/maker" : [ - { - "value" : "_:person", - "type" : "bnode" - } - ], - "http://purl.org/dc/elements/1.1/title2" : [ - { - "value" : "Anns HP", - "type" : "literal", - "lang" : "en", - "datatype" : "' . $short_datatype['STRING'] . '" - } - ] - }, - "_:person" : { - "http://xmlns.com/foaf/0.1/homepage" : [ - { - "value" : "http://example.org/about", - "type" : "uri" - } - ] - }}'; + $this->modelString = '{"http://example.org/about":{"http://purl.org/dc/elements/1.1/creator":[{"value":"Anna Wilder","type":"literal"}],"http://purl.org/dc/elements/1.1/title":[{"value":"Annas Homepage","type":"literal","lang":"en"}],"http://xmlns.com/foaf/0.1/maker":[{"value":"_:person","type":"bnode"}],"http://purl.org/dc/elements/1.1/title2":[{"value":"Anns HP","type":"literal","lang":"en","datatype":"' . $short_datatype['STRING'] . '"}]},"_:person":{"http://xmlns.com/foaf/0.1/homepage":[{"value":"http://example.org/about","type":"uri"}],"http://example.com/testProp1":[{"value":"\\"double quote\\nnewline\\ttab\\rcarriage return\\\\reverse solidus"}]}}'; } function testGenerateModelFromString() { @@ -69,6 +33,9 @@ $this->fail($e->getMessage()); } + #echo "<pre>"; + #print_r($model); + GLOBAL $short_datatype; $model2 = new MemModel('http://example.com/'); @@ -96,6 +63,10 @@ $statement6 = new Statement(new Resource('http://example.org/about'), new Resource("http://purl.org/dc/elements/1.1/title2"), new Literal('Anns HP', 'en', $short_datatype['INTEGER'])); + + $statement7= new Statement(new BlankNode('person'), + new Resource("http://example.com/testProp1"), + new Literal("\"double quote\nnewline\ttab\rcarriage return\\reverse solidus")); $model2->add($statement1); @@ -103,6 +74,7 @@ $model2->add($statement3); $model2->add($statement4); $model2->add($statement5); + $model2->add($statement7); $this->assertTrue($model->containsAll($model2)); Modified: trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php 2007-12-05 08:18:50 UTC (rev 554) +++ trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php 2007-12-11 20:27:34 UTC (rev 555) @@ -26,14 +26,18 @@ $literal1 = new Literal('test literal'); $literal2 = new Literal('test literal', 'en'); - $literal3 = new Literal('test literal', 'de', 'http://www.w3.org/2001/XMLSchema#string'); + $literal3 = new Literal('test literal', null, 'http://www.w3.org/2001/XMLSchema#string'); + // test literals with tabs and newlines and double quotes + $literal4 = new Literal("test literal\ttab\nnewline\"double quote\rcarriage return\fformfeed\bbackspace\\reverse solidus", null, 'http://www.w3.org/2001/XMLSchema#string'); + $stm1 = new Statement($res1, $res2, $res3); $stm2 = new Statement($res1, $res2, $bn1); $stm3 = new Statement($res1, $res2, $literal1); $stm4 = new Statement($res1, $res2, $literal2); $stm5 = new Statement($res1, $res2, $literal3); $stm6 = new Statement($bn1, $res2, $bn2); + $stm7 = new Statement($res2, $res1, $literal4); $this->model->add($stm1); $this->model->add($stm2); @@ -41,6 +45,7 @@ $this->model->add($stm4); $this->model->add($stm5); $this->model->add($stm6); + $this->model->add($stm7); } function testSerialize() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |