jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/379509 )
Change subject: Minor code structure clean ups in JsonLdRdfWriter ...................................................................... Minor code structure clean ups in JsonLdRdfWriter This is a direct follow up for the review I did in I77d6a77. Instead of leaving comments there, I fixed everything I can myself here in this follow up patch. Bug: T44063 Change-Id: Ice0706146d9acbe97140f6f0336db521ba8a9b53 --- M README.md M composer.json M src/JsonLdRdfWriter.php 3 files changed, 46 insertions(+), 39 deletions(-) Approvals: C. Scott Ananian: Looks good to me, approved jenkins-bot: Verified diff --git a/README.md b/README.md index edd0618..5dbdcd2 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ The concrete classes implementing the common `RdfWriter` interface are: * `TurtleRdfWriter` outputs Turtle +* `JsonLdRdfWriter` outputs JSON-LD * `XmlRdfWriter` outputs XML/RDF * `NTriplesRdfWriter` outputs N-Triples -* `JsonLdRdfWriter` outputs JSON-LD The PHP code would look something like this: diff --git a/composer.json b/composer.json index d714be3..4291bb6 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,10 @@ "type": "library", "description": "Fast streaming RDF serializer", "keywords": [ + "JSON-LD", "RDF", "Serializer", - "Turtle", - "JSON-LD" + "Turtle" ], "homepage": "https://mediawiki.org/wiki/Purtle", "license": "GPL-2.0+", diff --git a/src/JsonLdRdfWriter.php b/src/JsonLdRdfWriter.php index 2366a98..48dde9c 100644 --- a/src/JsonLdRdfWriter.php +++ b/src/JsonLdRdfWriter.php @@ -14,18 +14,24 @@ class JsonLdRdfWriter extends RdfWriterBase { /** - * The JSON-LD "@context" object, which maps terms to IRIs. - * This object is shared with all sub-writers, and a single - * context is emitted when the writer is finalized. + * The JSON-LD "@context", which maps terms to IRIs. This is shared with all sub-writers, and a + * single context is emitted when the writer is finalized. + * * @see https://www.w3.org/TR/json-ld/#the-context + * * @var string[] */ protected $context = []; /** - * The JSON-LD "@graph" array, which lists all the nodes - * described by this JSON-LD object. + * The JSON-LD "@graph", which lists all the nodes described by this JSON-LD object. + * We apply an optimization eliminating the "@graph" entry if it consists + * of a single node; in that case we will set $this->graph to null in + * #finishJson() to ensure that the deferred callback in #finishDocument() + * doesn't later emit "@graph". + * * @see https://www.w3.org/TR/json-ld/#named-graphs + * * @var array[]|null */ private $graph = []; @@ -34,7 +40,9 @@ * A collection of predicates about a specific subject. The * subject is identified by the "@id" key in this array; the other * keys identify JSON-LD properties. + * * @see https://www.w3.org/TR/json-ld/#dfn-edge + * * @var array */ private $predicates = []; @@ -42,13 +50,16 @@ /** * A sequence of zero or more IRIs, nodes, or values, which are the * destination targets of the current predicates. + * * @see https://www.w3.org/TR/json-ld/#dfn-list + * * @var array */ private $values = []; /** * True iff we have written the opening of the "@graph" field. + * * @var bool */ private $wroteGraph = false; @@ -57,6 +68,7 @@ * JSON-LD objects describing a single node can omit the "@graph" field; * this variable remains false only so long as we can guarantee that * only a single node has been described. + * * @var bool */ private $disableGraphOpt = false; @@ -96,9 +108,12 @@ public function encode( $val, $indent ) { $str = json_encode( $val, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); // Strip outermost open/close braces/brackets - $str = preg_replace( '/(^[\[{]\n?)|(\n?[}\]]$)/', '', $str ); - // add extra indentation - $str = preg_replace( '/^/m', str_repeat( " ", $indent ), $str ); + $str = preg_replace( '/^[[{]\n?|\n?[}\]]$/', '', $str ); + + if ( $indent > 0 ) { + // add extra indentation + $str = preg_replace( '/^/m', str_repeat( ' ', $indent ), $str ); + } return $str; } @@ -178,6 +193,7 @@ $this->write( "\n ]" ); } } + if ( count( $this->context ) ) { // Write @context field. $this->write( ",\n" ); @@ -185,8 +201,8 @@ "@context" => $this->context ], 0 ) ); } - $this->write( "\n" ); - $this->write( "}" ); + + $this->write( "\n}" ); } protected function finishDocument() { @@ -250,10 +266,7 @@ * @param string|null $language */ protected function writeText( $text, $language = null ) { - if ( - $language === null || - !$this->isValidLanguageCode( $language ) - ) { + if ( !$this->isValidLanguageCode( $language ) ) { $this->values[] = $text; } else { $this->values[] = [ @@ -271,23 +284,20 @@ public function writeValue( $literal, $typeBase, $typeLocal = null ) { if ( $typeBase === null && $typeLocal === null ) { $this->values[] = $literal; - } elseif ( $typeLocal === null ) { - throw new InvalidArgumentException( "Got IRI: $typeBase" ); - } else { - $typeIRI = $this->toIRI( $typeBase, $typeLocal ); - if ( $typeIRI === 'http://www.w3.org/2001/XMLSchema#string' ) { + return; + } + + switch ( $this->toIRI( $typeBase, $typeLocal ) ) { + case 'http://www.w3.org/2001/XMLSchema#string': $this->values[] = strval( $literal ); return; - } - if ( $typeIRI === 'http://www.w3.org/2001/XMLSchema#integer' ) { + case 'http://www.w3.org/2001/XMLSchema#integer': $this->values[] = intval( $literal ); return; - } - if ( $typeIRI === 'http://www.w3.org/2001/XMLSchema#boolean' ) { - $this->values[] = ( $literal === 'true' ); + case 'http://www.w3.org/2001/XMLSchema#boolean': + $this->values[] = $literal === 'true'; return; - } - if ( $typeIRI === 'http://www.w3.org/2001/XMLSchema#double' ) { + case 'http://www.w3.org/2001/XMLSchema#double': $v = doubleval( $literal ); // Only "numbers with fractions" are xsd:double. We need // to verify that the JSON string will contain a decimal @@ -299,12 +309,12 @@ $this->values[] = $v; return; } - } - $this->values[] = [ - "@type" => $this->compactify( $typeBase, $typeLocal ), - "@value" => strval( $literal ) - ]; } + + $this->values[] = [ + "@type" => $this->compactify( $typeBase, $typeLocal ), + "@value" => strval( $literal ) + ]; } protected function finishPredicate() { @@ -323,12 +333,9 @@ $name = $this->compactify( $base, $local ); } if ( isset( $this->predicates[$name] ) ) { - $was = $this->predicates[$name]; - if ( !is_array( $was ) ) { - $was = [ $was ]; - } - $this->values = array_merge( $was, $this->values ); + $this->values = array_merge( (array)$this->predicates[$name], $this->values ); } + $cnt = count( $this->values ); if ( $cnt === 0 ) { throw new LogicException( "finishPredicate can't be called without at least one value" ); -- To view, visit https://gerrit.wikimedia.org/r/379509 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ice0706146d9acbe97140f6f0336db521ba8a9b53 Gerrit-PatchSet: 2 Gerrit-Project: purtle Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: C. Scott Ananian <canan...@wikimedia.org> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits