Skip to content

Commit 57b3279

Browse files
committed
Add inputFormat option to normalize.
- Allows the input to be in N-Quads format (application/nquads) and use N-Quads parsing instead of a toRDF step on JSON-LD.
1 parent e08fe87 commit 57b3279

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

jsonld.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,15 @@ function jsonld_link($input, $ctx, $options) {
131131
};
132132

133133
/**
134-
* Performs RDF dataset normalization on the given JSON-LD input. The output
135-
* is an RDF dataset unless the 'format' option is used.
134+
* Performs RDF dataset normalization on the given input. The input is
135+
* JSON-LD unless the 'inputFormat' option is used. The output is an RDF
136+
* dataset unless the 'format' option is used.
136137
*
137138
* @param mixed $input the JSON-LD object to normalize.
138139
* @param assoc [$options] the options to use:
139140
* [base] the base IRI to use.
141+
* [intputFormat] the format if input is not JSON-LD:
142+
* 'application/nquads' for N-Quads.
140143
* [format] the format if output is a string:
141144
* 'application/nquads' for N-Quads.
142145
* [documentLoader(url)] the document loader.
@@ -1229,6 +1232,8 @@ public function frame($input, $frame, $options) {
12291232
* @param assoc $options the options to use:
12301233
* [base] the base IRI to use.
12311234
* [expandContext] a context to expand with.
1235+
* [inputFormat] the format if input is not JSON-LD:
1236+
* 'application/nquads' for N-Quads.
12321237
* [format] the format if output is a string:
12331238
* 'application/nquads' for N-Quads.
12341239
* [documentLoader(url)] the document loader.
@@ -1241,18 +1246,26 @@ public function normalize($input, $options) {
12411246
'base' => is_string($input) ? $input : '',
12421247
'documentLoader' => $jsonld_default_load_document));
12431248

1244-
try {
1245-
// convert to RDF dataset then do normalization
1246-
$opts = $options;
1247-
if(isset($opts['format'])) {
1248-
unset($opts['format']);
1249+
if(isset($options['inputFormat'])) {
1250+
if($options['inputFormat'] != 'application/nquads') {
1251+
throw new JsonLdException(
1252+
'Unknown normalization input format.', 'jsonld.NormalizeError');
1253+
}
1254+
$dataset = $this->parseNQuads($input);
1255+
} else {
1256+
try {
1257+
// convert to RDF dataset then do normalization
1258+
$opts = $options;
1259+
if(isset($opts['format'])) {
1260+
unset($opts['format']);
1261+
}
1262+
$opts['produceGeneralizedRdf'] = false;
1263+
$dataset = $this->toRDF($input, $opts);
1264+
} catch(Exception $e) {
1265+
throw new JsonLdException(
1266+
'Could not convert input to RDF dataset before normalization.',
1267+
'jsonld.NormalizeError', null, null, $e);
12491268
}
1250-
$opts['produceGeneralizedRdf'] = false;
1251-
$dataset = $this->toRDF($input, $opts);
1252-
} catch(Exception $e) {
1253-
throw new JsonLdException(
1254-
'Could not convert input to RDF dataset before normalization.',
1255-
'jsonld.NormalizeError', null, null, $e);
12561269
}
12571270

12581271
// do normalization

0 commit comments

Comments
 (0)