From: <p_f...@us...> - 2007-11-23 09:12:14
|
Revision: 552 http://rdfapi-php.svn.sourceforge.net/rdfapi-php/?rev=552&view=rev Author: p_frischmuth Date: 2007-11-23 01:12:11 -0800 (Fri, 23 Nov 2007) Log Message: ----------- [added] a serializer class for rdf/json format [added] a unit test class for the json serializer in order to test the serialize method [propset] set svn:keyword Id on new files [modified] added the new test classes to the allTest environment Modified Paths: -------------- trunk/rdfapi-php/api/syntax/SyntaxJSON.php trunk/rdfapi-php/test/unit/allTests.php Added Paths: ----------- trunk/rdfapi-php/api/syntax/JsonSerializer.php trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php Added: trunk/rdfapi-php/api/syntax/JsonSerializer.php =================================================================== --- trunk/rdfapi-php/api/syntax/JsonSerializer.php (rev 0) +++ trunk/rdfapi-php/api/syntax/JsonSerializer.php 2007-11-23 09:12:11 UTC (rev 552) @@ -0,0 +1,137 @@ +<?php +/** + * This class provides capabilities to serialize MemModels to json strings. + * + * @package syntax + * @author Philipp Frischmuth <ph...@fr...> + * @version $Id:$ + */ +class JsonSerializer extends Object { + + /** + * This method takes a MemModel object ad parameter and serializes all contained triples to rdf/json format. + * + * @see http://n2.talis.com/wiki/RDF_JSON_Specification#rdf.2Fjson + * @param MemModel $model + * @return string Returns a string containing the serialized model. + */ + public function serialize(MemModel $model) { + + // create the root json object (root object) + $jsonString = '{'; + $subjects = array(); + + // sort triples by subject + foreach ($model->triples as $triple) { + $subjects[$triple->toStringSubject()][] = $triple; + } + + // 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) { + $predicates = array(); + + if ($i > 0) { + $jsonString .= ','; + } + $i++; + + $subj = $predicatesArray[0]->getSubject(); + + // add special _: sequence for blank node only + if ($subj instanceof BlankNode) { + $jsonString .= PHP_EOL . ' "_:' . $subj->getLabel() . '" : '; + } else { + $jsonString .= PHP_EOL . ' "' . $subj->getLabel() . '" : '; + } + + + + // create a json object for each subject (subject object) + $jsonString .= '{'; + + // sort triples with current subject by predicate + foreach ($predicatesArray as $triple) { + $predicates[$triple->toStringPredicate()][] = $triple; + } + + // sort alphabetically + ksort($predicates); + + $j = 0; + foreach ($predicates as $valueArray) { + + if ($j > 0) { + $jsonString .= ','; + } + $j++; + + $jsonString .= PHP_EOL . ' "' . $valueArray[0]->getLabelPredicate() . '" : '; + + // create a json array (value array) + $jsonString .= '['; + + $k = 0; + foreach ($valueArray as $triple) { + if ($k > 0) { + $jsonString .= ','; + } + $k++; + + // create json value object (value object) + $jsonString .= PHP_EOL . ' {'; + + $obj = $triple->getObject(); + + // add special _: sequence for blank nodes only + if ($obj instanceof BlankNode) { + $jsonString .= PHP_EOL . ' "value" : "_:' . $obj->getLabel() . '",'; + } else { + $jsonString .= PHP_EOL . ' "value" : "' . $obj->getLabel() . '",'; + } + + // add type of object + if ($obj instanceof Literal) { + $jsonString .= PHP_EOL . ' "type" : "literal"'; + } else if ($obj instanceof BlankNode) { + $jsonString .= PHP_EOL . ' "type" : "bnode"'; + } else { + $jsonString .= PHP_EOL . ' "type" : "uri"'; + } + + if ($obj instanceof Literal) { + if ($obj->getLanguage() != '') { + $jsonString .= ',' . PHP_EOL . ' "lang" : "' . $obj->getLanguage() . '"'; + } + if ($obj->getDatatype() != '') { + $jsonString .= ',' . PHP_EOL . ' "datatype" : "' . $obj->getDatatype() . '"'; + } + + } + + // close value object + $jsonString .= PHP_EOL . ' }'; + } + + // close the value array + $jsonString .= PHP_EOL . ' ]'; + } + + // close the json object (for the subject) (subject object) + $jsonString .= PHP_EOL . ' }'; + } + + // close root json object (root object) + $jsonString .= PHP_EOL . '}'; + + return $jsonString; + } +} +?> Property changes on: trunk/rdfapi-php/api/syntax/JsonSerializer.php ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/rdfapi-php/api/syntax/SyntaxJSON.php =================================================================== --- trunk/rdfapi-php/api/syntax/SyntaxJSON.php 2007-11-22 19:58:35 UTC (rev 551) +++ trunk/rdfapi-php/api/syntax/SyntaxJSON.php 2007-11-23 09:12:11 UTC (rev 552) @@ -2,4 +2,5 @@ // Include Syntax classes // $Id$ require_once( RDFAPI_INCLUDE_DIR . 'syntax/JsonParser.php' ); +require_once( RDFAPI_INCLUDE_DIR . 'syntax/JsonSerializer.php' ); ?> \ No newline at end of file Added: trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php =================================================================== --- trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php (rev 0) +++ trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php 2007-11-23 09:12:11 UTC (rev 552) @@ -0,0 +1,57 @@ +<?php +require_once RDFAPI_INCLUDE_DIR.PACKAGE_SYNTAX_JSON; +/** + * Tests the JsonSerializer + * + * @version $Id $ + * @author Philipp Frischmuth <ph...@fr...> + * + * @package unittests + * @access public + */ +class testJsonSerializer extends UnitTestCase { + + var $model; + + function testJsonSerializer() { + + $this->model = new MemModel(); + + $res1 = new Resource('http://example.com/res1'); + $res2 = new Resource('http://example.com/res2'); + $res3 = new Resource('http://example.com/res3'); + + $bn1 = new BlankNode('test1'); + $bn2 = new BlankNode('test2'); + + $literal1 = new Literal('test literal'); + $literal2 = new Literal('test literal', 'en'); + $literal3 = new Literal('test literal', 'de', '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); + + $this->model->add($stm1); + $this->model->add($stm2); + $this->model->add($stm3); + $this->model->add($stm4); + $this->model->add($stm5); + $this->model->add($stm6); + } + + function testSerialize() { + + $ser = new JsonSerializer(); + + $jsonString = $ser->serialize($this->model); + $memModel = new MemModel(); + $memModel->loadFromString($jsonString, 'json'); + + $this->assertTrue($this->model->equals($memModel)); + } +} +?> Property changes on: trunk/rdfapi-php/test/unit/Syntax/jsonSerializer_test.php ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/rdfapi-php/test/unit/allTests.php =================================================================== --- trunk/rdfapi-php/test/unit/allTests.php 2007-11-22 19:58:35 UTC (rev 551) +++ trunk/rdfapi-php/test/unit/allTests.php 2007-11-23 09:12:11 UTC (rev 552) @@ -119,6 +119,8 @@ $test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Syntax/n3Serializer_test.php'); $test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Syntax/rdf_Parser_tests.php'); $test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Syntax/rdf_Serializer_tests.php'); +$test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Syntax/jsonParser_test.php'); +$test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/Syntax/jsonSerializer_test.php'); //$test_syntax->addTestFile(RDFAPI_TEST_INCLUDE_DIR. 'test/unit/rdf/rdf_test_cases.php'); $test_syntax->run(new $runnerClass()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |